Sha256: fe7de630e29bbadea739c8c691b9f65d2f6aeba62ed10648ef597320c432ef99

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 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 FlashComponentMigrationCounter < BaseLinter
      include Autocorrectable

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

      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)

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
primer_view_components-0.0.55 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.54 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.53 lib/primer/view_components/linters/flash_component_migration_counter.rb