Sha256: 11cd2f6406c0f43965ce06d9f217611ba7eb38715ce76eb28c878865ba2e0d00

Contents?: true

Size: 1.24 KB

Versions: 21

Compression:

Stored size: 1.24 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)

        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

21 entries across 21 versions & 1 rubygems

Version Path
primer_view_components-0.0.76 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.75 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.74 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.73 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.72 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.71 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.70 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.69 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.68 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.67 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.66 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.65 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.64 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.63 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.62 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.61 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.60 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.59 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.58 lib/primer/view_components/linters/flash_component_migration_counter.rb
primer_view_components-0.0.57 lib/primer/view_components/linters/flash_component_migration_counter.rb