Class: ERBLint::Linters::FlashMigrationCounter

Inherits:
BaseLinter
  • Object
show all
Includes:
Autocorrectable
Defined in:
lib/yattho/view_components/linters/flash_migration_counter.rb

Overview

Counts the number of times a HTML flash is used instead of the component.

Constant Summary collapse

TAGS =
%w[div].freeze
CLASSES =
%w[flash].freeze
MESSAGE =
"We are migrating flashes to use [Yattho::Beta::Flash](https://yattho.com/view-components/components/beta/flash), please try to use that instead of raw HTML."
ARGUMENT_MAPPER =
ArgumentMappers::Flash
COMPONENT =
"Yattho::Beta::Flash"

Constants inherited from BaseLinter

BaseLinter::DISALLOWED_CLASSES, BaseLinter::DUMP_FILE, BaseLinter::REQUIRED_ARGUMENTS

Constants included from TagTreeHelpers

TagTreeHelpers::SELF_CLOSING_TAGS

Instance Method Summary collapse

Methods included from Autocorrectable

#correction, #message

Methods inherited from BaseLinter

#autocorrect, inherited, #run

Methods included from TagTreeHelpers

#build_tag_tree, #self_closing?

Instance Method Details

#map_arguments(tag, tag_tree) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yattho/view_components/linters/flash_migration_counter.rb', line 19

def map_arguments(tag, tag_tree)
  # We can only autocorrect elements with simple text as content.
  return nil if tag_tree[:children].size != 1
  # Hash children indicates that there are tags in the content.
  return nil if tag_tree[:children].first.is_a?(Hash)

  content = tag_tree[:children].first

  # Don't accept content with ERB blocks
  return nil if content.type != :text || content.children&.any? { |n| n.try(:type) == :erb }

  ARGUMENT_MAPPER.new(tag).to_s
rescue ArgumentMappers::ConversionError
  nil
end