Sha256: 6aa5d45dcaa21c0fdf6e920c7b9662058b1ed279768c224a8338e3e739dbd04c

Contents?: true

Size: 1.79 KB

Versions: 58

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require "rubocop"

# :nocov:
module RuboCop
  module Cop
    module Primer
      # This cop ensures that components don't use deprecated `Label` schemes.
      #
      # bad
      # Primer::Beta::Label.new(scheme: :info)
      #
      # good
      # Primer::Beta::Label.new(scheme: :accent)
      class DeprecatedLabelSchemes < BaseCop
        INVALID_MESSAGE = <<~STR
          Avoid using deprecated schemes: https://primer.style/view-components/deprecated#labelcomponent.
        STR

        # This is a hash of deprecated schemes and their replacements.
        DEPRECATIONS = {
          info: ":accent",
          warning: ":attention",
          orange: ":severe",
          purple: ":done"
        }.freeze

        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
            next if pair.key.type != :sym
            next unless pair.value.type == :sym || pair.value.type == :str

            value = pair.value.value.to_sym

            next unless DEPRECATIONS.key?(value)

            add_offense(pair.value, message: INVALID_MESSAGE)
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            replacement = DEPRECATIONS[node.value.to_sym]
            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::Beta::Label"
        end
      end
    end
  end
end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
openproject-primer_view_components-0.7.0 lib/rubocop/cop/primer/deprecated_label_schemes.rb
openproject-primer_view_components-0.6.3 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.6.0 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.5.1 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.5.0 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.4.0 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.3.1 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.3.0 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.2.0 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.9 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.8 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.7 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.6 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.5 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.4 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.3 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.2 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.1 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.1.0 lib/rubocop/cop/primer/deprecated_label_schemes.rb
primer_view_components-0.0.123 lib/rubocop/cop/primer/deprecated_label_schemes.rb