Sha256: 3086f1e2deac1a2684a860957e09b5dfed5145ec402da99687b7a25be4c7e53b

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require "rubocop"

# :nocov:
module RuboCop
  module Cop
    module Yattho
      # Prefer the `test_selector` argument over manually generating
      # a `data-test-selector` attribute.
      #
      # Bad:
      #
      # Yattho::BaseComponent.new(data: { "test-selector": "the-component" })
      #
      # Good:
      #
      # Yattho::BaseComponent.new(test_selector: "the-component")
      class TestSelector < BaseCop
        INVALID_MESSAGE = <<~STR
          Prefer the `test_selector` argument over manually generating a `data-test-selector` attribute: https://yattho.com/view-components/system-arguments.
        STR

        def on_send(node)
          return unless valid_node?(node)
          return unless node.arguments?

          kwargs = node.arguments.last
          return unless kwargs.type == :hash

          data_arg = kwargs.pairs.find { |kwarg| kwarg.key.value == :data }
          return if data_arg.nil?
          return unless data_arg.value.type == :hash

          hash = data_arg.child_nodes.find { |arg| arg.type == :hash }
          return unless hash

          test_selector = hash.pairs.find do |pair|
            pair.key.value == :"test-selector" || pair.key.value == "test-selector"
          end
          return unless test_selector

          add_offense(data_arg, message: INVALID_MESSAGE)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yattho_view_components-0.1.1 lib/rubocop/cop/yattho/test_selector.rb
yattho_view_components-0.0.1 lib/rubocop/cop/yattho/test_selector.rb