Sha256: 49451446fd08961748f4ace1291b9a1d4bbe260084af179f8fcedd5ce614f522

Contents?: true

Size: 1.99 KB

Versions: 17

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require "rubocop"

# :nocov:
module RuboCop
  module Cop
    module Primer
      # This cop ensures that `LabelComponent`s don't use the old `variant` argument.
      #
      # bad
      # Primer::LabelComponent.new(variant: :large)
      #
      # good
      # Primer::LabelComponent.new(size: :large)
      #
      # bad
      # Primer::LabelComponent.new(variant: :inline)
      #
      # good
      # Primer::LabelComponent.new(inline: true)
      class DeprecatedLabelVariants < BaseCop
        def on_send(node)
          return unless label_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

          kwargs.pairs.each do |pair|
            # skip if we're not dealing with a symbol or string
            next if pair.key.type != :sym
            next unless pair.value.type == :sym || pair.value.type == :str
            next if pair.key.value != :variant

            case pair.value.value
            when :large, "large"
              add_offense(pair, message: "Avoid using `variant: :large` with `LabelComponent`. Use `size: :large` instead.")
            when :inline, "inline"
              add_offense(pair, message: "Avoid using `variant: :inline` with `LabelComponent`. Use `inline: true` instead.")
            end
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            replacement =
              case node.value.value
              when :large, "large"
                "size: :large"
              when :inline, "inline"
                "inline: true"
              end

            corrector.replace(node, replacement)
          end
        end

        private

        def label_node?(node)
          return if node.nil?

          node.method_name == :new && !node.receiver.nil? && node.receiver.const_name == "Primer::LabelComponent"
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
primer_view_components-0.0.104 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.103 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.102 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.101 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.100 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.99 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.98 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.97 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.96 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.95 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.94 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.93 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.92 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.91 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.90 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.89 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.0.88 lib/rubocop/cop/primer/deprecated_label_variants.rb