Sha256: a97728d635399ba1298bedcd113cad1a718d2b755d199c796ce50634606ff7a3

Contents?: true

Size: 1.23 KB

Versions: 173

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/beta/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

173 entries across 173 versions & 2 rubygems

Version Path
primer_view_components-0.0.121 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.120 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.119 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.118 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.117 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.116 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.115 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.114 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.113 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.112 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.111 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.110 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.109 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.108 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.107 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.106 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.105 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.104 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.103 lib/primer/view_components/linters/flash_migration_counter.rb
primer_view_components-0.0.102 lib/primer/view_components/linters/flash_migration_counter.rb