Sha256: 98d890119b29c8b2fe6fd6123aca5b85bf7d9d68b9153a825c77c129a1e6efdd

Contents?: true

Size: 1.58 KB

Versions: 197

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require "rubocop"
require "primer/classify/utilities"

# :nocov:
module RuboCop
  module Cop
    module Primer
      # This cop ensures that components use System Arguments instead of CSS classes.
      #
      # bad
      # Component.new(classes: "mr-1")
      #
      # good
      # Component.new(mr: 1)
      class SystemArgumentInsteadOfClass < BaseCop
        INVALID_MESSAGE = <<~STR
          Avoid using CSS classes when you can use System Arguments: https://primer.style/view-components/system-arguments.
        STR

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

          # we are looking for hash arguments and they are always last
          kwargs = node.arguments.last

          return unless kwargs.type == :hash

          # find classes pair
          classes_arg = kwargs.pairs.find { |kwarg| kwarg.key.value == :classes }

          return if classes_arg.nil?
          return unless classes_arg.value.type == :str

          # get actual classes
          classes = classes_arg.value.value

          system_arguments = ::Primer::Classify::Utilities.classes_to_hash(classes)

          # no classes are fixable
          return if system_arguments[:classes] == classes

          add_offense(classes_arg, message: INVALID_MESSAGE)
        end

        def autocorrect(node)
          lambda do |corrector|
            args = ::Primer::Classify::Utilities.classes_to_args(node.value.value)
            corrector.replace(node.loc.expression, args)
          end
        end
      end
    end
  end
end

Version data entries

197 entries across 197 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.50.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
primer_view_components-0.36.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.49.2 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
primer_view_components-0.35.2 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.49.1 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.49.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
primer_view_components-0.35.1 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
primer_view_components-0.35.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.48.2 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.48.1 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.48.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.47.1 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.47.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
primer_view_components-0.34.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.46.1 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.46.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.45.0 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.44.3 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.44.2 lib/rubocop/cop/primer/system_argument_instead_of_class.rb
openproject-primer_view_components-0.44.1 lib/rubocop/cop/primer/system_argument_instead_of_class.rb