Sha256: 7902eba0077aaadf554734697515b3c15802779cda0463870e8619877d732148

Contents?: true

Size: 1.23 KB

Versions: 12

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require_relative "base_linter"
require_relative "autocorrectable"
require_relative "argument_mappers/flash"

module ERBLint
  module Linters
    # Counts the number of times a HTML flash is used instead of the component.
    class FlashMigrationCounter < BaseLinter
      include Autocorrectable

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

      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
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
primer_view_components-0.0.88 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.87 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.86 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.85 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.84 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.83 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.82 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.81 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.80 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.79 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.78 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.77 lib/primer/view_components/linters/flash_migration_counter.rb