Class: RuboCop::Cop::Primer::DeprecatedLabelVariants

Inherits:
BaseCop
  • Object
show all
Defined in:
lib/rubocop/cop/primer/deprecated_label_variants.rb

Overview

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)

Instance Method Summary collapse

Methods inherited from BaseCop

#valid_node?

Instance Method Details

#autocorrect(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/primer/deprecated_label_variants.rb', line 47

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

#on_send(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/primer/deprecated_label_variants.rb', line 23

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