Class: RuboCop::Cop::Yattho::SystemArgumentInsteadOfClass

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

Overview

This cop ensures that components use System Arguments instead of CSS classes.

bad Component.new(classes: “mr-1”)

good Component.new(mr: 1)

Constant Summary collapse

INVALID_MESSAGE =
<<~STR
  Avoid using CSS classes when you can use System Arguments: https://yattho.com/view-components/system-arguments.
STR

Instance Method Summary collapse

Methods inherited from BaseCop

#valid_node?

Instance Method Details

#autocorrect(node) ⇒ Object



48
49
50
51
52
53
# File 'lib/rubocop/cop/yattho/system_argument_instead_of_class.rb', line 48

def autocorrect(node)
  lambda do |corrector|
    args = ::Yattho::Classify::Utilities.classes_to_args(node.value.value)
    corrector.replace(node.loc.expression, args)
  end
end

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless valid_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

  # find classes pair
  classes_arg = kwargs.pairs.find { |kwarg| kwarg.key.value == :classes }

  return if classes_arg.nil?
  return unless classes_arg.value.type == :str

  # get actual classes
  classes = classes_arg.value.value

  system_arguments = ::Yattho::Classify::Utilities.classes_to_hash(classes)

  # no classes are fixable
  return if system_arguments[:classes] == classes

  add_offense(classes_arg, message: INVALID_MESSAGE)
end