Class: RuboCop::Cop::Yattho::DeprecatedLabelSchemes

Inherits:
BaseCop
  • Object
show all
Defined in:
lib/rubocop/cop/yattho/deprecated_label_schemes.rb

Overview

This cop ensures that components don’t use deprecated ‘Label` schemes.

bad Yattho::Beta::Label.new(scheme: :info)

good Yattho::Beta::Label.new(scheme: :accent)

Constant Summary collapse

INVALID_MESSAGE =
<<~STR
  Avoid using deprecated schemes: https://yattho.com/view-components/deprecated#labelcomponent.
STR
DEPRECATIONS =

This is a hash of deprecated schemes and their replacements.

{
  info: ":accent",
  warning: ":attention",
  orange: ":severe",
  purple: ":done"
}.freeze

Instance Method Summary collapse

Methods inherited from BaseCop

#valid_node?

Instance Method Details

#autocorrect(node) ⇒ Object



51
52
53
54
55
56
# File 'lib/rubocop/cop/yattho/deprecated_label_schemes.rb', line 51

def autocorrect(node)
  lambda do |corrector|
    replacement = DEPRECATIONS[node.value.to_sym]
    corrector.replace(node, replacement)
  end
end

#on_send(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/yattho/deprecated_label_schemes.rb', line 29

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