Class: RuboCop::Cop::Glyphs::PreferLibraryComponent

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, LibraryCallHelpers
Defined in:
lib/rubocop/cop/glyphs/prefer_library_component.rb

Overview

Prefers library-specific components over the generic Icon(...) kit call with a library: argument.

Examples:

# bad
Icon(:house, library: :lucide)

# good
LucideIcon(:house)

Constant Summary collapse

MSG =
"Use `%{component}(...)` instead of `Icon(..., library: ...)`."
MSG_DYNAMIC =
"Prefer a library-specific Glyphs component over `Icon(...)` with a dynamic library."

Constants included from LibraryCallHelpers

LibraryCallHelpers::LIBRARY_KEYS, LibraryCallHelpers::LIBRARY_TO_COMPONENT

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless node.receiver.nil? && node.method_name == :Icon && node.arguments.any?

  pair = library_pair(node)
  return unless pair

  unless literal_pair?(pair)
    add_offense(node.loc.selector, message: MSG_DYNAMIC)
    return
  end

  component = library_components[pair.value.value.to_s]
  return unless component

  message = format(MSG, component:)
  add_offense(node.loc.selector, message:) do |corrector|
    corrector.replace(node.loc.selector, component)
    remove_library_pair(corrector, pair)
  end
end