Class: RuboCop::Cop::Heatwave::NoInlineFaIcon

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

Overview

Flags raw <i class="fa-...">…</i> Font Awesome tags in ERB templates and
rewrites them as <%= fa_icon(...) %> calls against IconHelper#fa_icon.
The helper centralises family handling, custom SVG fallback, default
sharp-regular family and fa-kit inlining — see
app/helpers/icon_helper.rb.

Behaviour:

  • Recognises the modern (fa-solid, fa-regular, fa-sharp fa-solid,
    fa-brands, fa-kit) families and the legacy short-prefix families
    from FA4/FA5 (fa, fas, far, fab, fal, fad) plus the Pro-only
    fa-light / fa-thin / fa-duotone styles. Unsupported variants are
    collapsed onto the project's adopted sharp-regular default.
  • Forwards arbitrary HTML attributes (style, title, aria-*, data-*,
    id, role, …) through to the helper as keyword arguments, since
    fa_icon already splats them onto the underlying tag.i call.
  • Skips matches inside <script> blocks (those are JS string literals,
    not real markup).
  • Refuses to autocorrect — and therefore just flags — anything it cannot
    safely rewrite (multiple icon candidates, ERB interpolation in
    attribute values, unparseable attribute fragments).

Examples:

# bad
<i class="fa-regular fa-gear me-2"></i>
<i class="fa-brands fa-youtube"></i>
<i class="fas fa-trash" aria-hidden="true"></i>

# good
<%= fa_icon('gear', class: 'me-2') %>
<%= fa_icon('youtube', family: :brands) %>
<%= fa_icon('trash', family: :solid, 'aria-hidden': 'true') %>

Constant Summary collapse

MSG =
'Use the `fa_icon` helper instead of inline `<i class="fa-...">` ' \
'tags so icon styling stays consistent.'
TAG_REGEX =

Empty <i ...></i> tag with at least one fa-* class. Attribute string
may not contain < or > so we never accidentally match across an
embedded ERB tag — that case is handled by assert_no_offense in the
tests.

%r{<i\b([^<>]*?)>\s*</i>}
FAMILY_TOKENS =

Recognised Font Awesome family / style tokens. Includes both the modern
fa-<style> long form and the legacy short prefixes from FA4/FA5.

%w[
  fa fas far fab fal fad
  fa-regular fa-solid fa-light fa-thin fa-duotone
  fa-brands fa-sharp fa-kit
].freeze
SIZING_TOKENS =

Font Awesome utility classes that are NOT the icon name.

%w[
  fa-2xs fa-xs fa-sm fa-lg fa-xl fa-2xl
  fa-1x fa-2x fa-3x fa-4x fa-5x fa-6x fa-7x fa-8x fa-9x fa-10x
  fa-fw fa-spin fa-spin-pulse fa-spin-reverse fa-pulse
  fa-beat fa-bounce fa-fade fa-flip fa-shake
  fa-rotate-90 fa-rotate-180 fa-rotate-270
  fa-flip-horizontal fa-flip-vertical fa-flip-both
  fa-stack fa-stack-1x fa-stack-2x fa-inverse
  fa-border fa-pull-left fa-pull-right fa-li fa-ul
].freeze
FAMILY_MAP =

Family-token combinations → fa_icon family: option (or nil for the
helper's default of :sharp_regular). Keys are sorted Arrays.
Legacy/Pro-only families collapse onto the project's adopted standard:
fa (FA4 prefix-only) and fas/fa-solid map to :solid, fab /
fa-brands to :brands, fad/fa-duotone to :solid (closest
filled equivalent), and far/fal/fa-light/fa-thin to the
default sharp-regular.

{
  %w[fa]                       => :solid,
  %w[fas]                      => :solid,
  %w[far]                      => nil,
  %w[fab]                      => :brands,
  %w[fal]                      => nil,
  %w[fad]                      => :solid,
  %w[fa-regular]               => nil,
  %w[fa-sharp fa-regular].sort => nil,
  %w[fa-solid]                 => :solid,
  %w[fa-sharp fa-solid].sort   => :sharp_solid,
  %w[fa-brands]                => :brands,
  %w[fa-kit]                   => :kit,
  %w[fa-light]                 => nil,
  %w[fa-thin]                  => nil,
  %w[fa-duotone]               => :solid
}.freeze

Instance Method Summary collapse

Instance Method Details

#on_new_investigationObject

Commissioner#investigate calls on_new_investigation for files that
parse as Ruby and on_other_file for everything else (including ERB
templates, which are never valid Ruby). Hooking both means the cop
runs whether it's invoked via the rake task (ERB → on_other_file) or
plain RuboCop on a .rb fixture (on_new_investigation).



99
100
101
# File 'lib/rubocop/cop/heatwave/no_inline_fa_icon.rb', line 99

def on_new_investigation
  scan_source
end

#on_other_fileObject



103
104
105
# File 'lib/rubocop/cop/heatwave/no_inline_fa_icon.rb', line 103

def on_other_file
  scan_source
end