Sha256: 18c1dea319d5af72f8bc891c40a5f71031ca2c3089865406a143e653adac86bf

Contents?: true

Size: 1.37 KB

Versions: 142

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require "rubocop"

# :nocov:
module RuboCop
  module Cop
    module Primer
      # Prefer the `test_selector` argument over manually generating
      # a `data-test-selector` attribute.
      #
      # Bad:
      #
      # Primer::BaseComponent.new(data: { "test-selector": "the-component" })
      #
      # Good:
      #
      # Primer::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://primer.style/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

142 entries across 142 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.33.0 lib/rubocop/cop/primer/test_selector.rb
primer_view_components-0.25.1 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.32.1 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.32.0 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.29.1 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.29.0 lib/rubocop/cop/primer/test_selector.rb
primer_view_components-0.23.0 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.28.1 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.28.0 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.27.0 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.26.0 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.25.1 lib/rubocop/cop/primer/test_selector.rb
primer_view_components-0.22.0 lib/rubocop/cop/primer/test_selector.rb
primer_view_components-0.21.1 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.25.0 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.24.0 lib/rubocop/cop/primer/test_selector.rb
primer_view_components-0.21.0 lib/rubocop/cop/primer/test_selector.rb
primer_view_components-0.20.1 lib/rubocop/cop/primer/test_selector.rb
primer_view_components-0.20.0 lib/rubocop/cop/primer/test_selector.rb
openproject-primer_view_components-0.23.0 lib/rubocop/cop/primer/test_selector.rb