Class: RuboCop::Cop::Heatwave::CanonicalFaIconName

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/heatwave/canonical_fa_icon_name.rb

Overview

Rewrites fa_icon('legacy-name', ...) calls to use the canonical Font
Awesome Pro 7+ icon name. The legacy → canonical map is generated from
the bundled FA Pro metadata (shims.yml + icons.yml/aliases.names)
via bundle exec rake lint:regenerate_fa_aliases. See the JSON file
at lib/rubocop/cop/heatwave/fa_icon_aliases.json.

The cop:

  • Matches fa_icon('NAME', ...), fa_icon "NAME", and fa_icon :name
    in both ERB and Ruby files.
  • Skips dynamic arguments (variables, method calls, ERB
    interpolation) — only string and bare-symbol literals are
    considered.
  • Skips matches inside <script> blocks for the same reason
    Heatwave/NoInlineFaIcon does.
  • Symbols whose canonical replacement contains a - are converted
    to single-quoted strings (Ruby symbol literals can't include
    dashes without :'…' quoting; switching to a string keeps the
    call site idiomatic).

Examples:

# bad
<%= fa_icon('info-circle', class: 'me-2') %>
<%= fa_icon('cog') %>
fa_icon(:edit)

# good
<%= fa_icon('circle-info', class: 'me-2') %>
<%= fa_icon('gear') %>
fa_icon('pen-to-square')

Constant Summary collapse

ALIASES_PATH =
File.expand_path('fa_icon_aliases.json', __dir__)
ALIASES =
JSON.parse(File.read(ALIASES_PATH)).fetch('aliases').freeze
FA_PRO_VERSION =
JSON.parse(File.read(ALIASES_PATH))['fa_pro_version']
MSG =
'Use the canonical FA Pro %<version>s name `%<canonical>s` ' \
'instead of legacy `%<legacy>s`.'
STRING_CALL_REGEX =

fa_icon first-positional-argument literals. We deliberately keep the
regex strict so we never accidentally rewrite a non-icon argument:

  • fa_icon('NAME', fa_icon "NAME", fa_icon('NAME')
  • fa_icon(:name, fa_icon :name
    Dashes are allowed inside the string form; bare symbol literals are
    restricted to underscores (Ruby's symbol grammar) so fa_icon(:plus)
    matches but fa_icon(:'circle-info') does not — the latter is rare
    and rewriting :'…' literals adds parsing complexity for no benefit.
/\bfa_icon\s*\(?\s*(['"])([a-z][a-z0-9-]*)\1/
SYMBOL_CALL_REGEX =
/\bfa_icon\s*\(?\s*:([a-z_][a-z0-9_]*)/
QUOTED_SYMBOL_CALL_REGEX =
/\bfa_icon\s*\(?\s*:(['"])([a-z][a-z0-9-]*)\1/

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject



59
60
61
# File 'lib/rubocop/cop/heatwave/canonical_fa_icon_name.rb', line 59

def on_new_investigation
  scan_source
end

#on_other_fileObject



63
64
65
# File 'lib/rubocop/cop/heatwave/canonical_fa_icon_name.rb', line 63

def on_other_file
  scan_source
end