Sha256: 083e798ef398eb3a8d1478951e7205ce43747c5eaf71fdef11d4cd1c1a3d5a77

Contents?: true

Size: 1.98 KB

Versions: 87

Compression:

Stored size: 1.98 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::Beta::Label.new(variant: :large)
      #
      # good
      # Primer::Beta::Label.new(size: :large)
      #
      # bad
      # Primer::Beta::Label.new(variant: :inline)
      #
      # good
      # Primer::Beta::Label.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 false if node.nil?

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

Version data entries

87 entries across 87 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.43.1 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.43.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.32.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.42.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.31.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.41.1 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.41.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.40.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.30.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.39.1 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.39.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.38.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.29.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.28.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.37.1 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.37.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.36.2 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.36.1 lib/rubocop/cop/primer/deprecated_label_variants.rb
openproject-primer_view_components-0.36.0 lib/rubocop/cop/primer/deprecated_label_variants.rb
primer_view_components-0.27.0 lib/rubocop/cop/primer/deprecated_label_variants.rb