Sha256: 5fdd92fe447992fa236422578f8af8d3c5d209a2f9b93f199e7bbc9d8665dfb2

Contents?: true

Size: 1.13 KB

Versions: 10

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module ERBLint
  module Linters
    # Warns when a tag is not self-closed properly.
    class SelfClosingTagCustom < Linter
      include LinterRegistry

      SELF_CLOSING_TAGS = %w(
        area input param track img
      )

      def run(processed_source)
        processed_source.ast.descendants(:tag).each do |tag_node|
          tag = BetterHtml::Tree::Tag.from_node(tag_node)
          next unless SELF_CLOSING_TAGS.include?(tag.name)

          if tag.closing?
            start_solidus = tag_node.children.first
            add_offense(
                start_solidus.loc,
                "Tag `#{tag.name}` is self-closing, it must not start with `</`.",
                ''
            )
          end

          next if tag.self_closing?
          add_offense(
              tag_node.loc.end.offset(-1),
              "Tag `#{tag.name}` is self-closing, it must end with `/>`.",
              '/'
          )
        end
      end

      def autocorrect(_processed_source, offense)
        lambda do |corrector|
          corrector.replace(offense.source_range, offense.context)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
niftany-0.10.1 .erb-linters/self_closing_custom.rb
niftany-0.10.0 .erb-linters/self_closing_custom.rb
niftany-0.9.0 .erb-linters/self_closing_custom.rb
niftany-0.8.0 .erb-linters/self_closing_custom.rb
niftany-0.7.0 .erb-linters/self_closing_custom.rb
niftany-0.6.0 .erb-linters/self_closing_custom.rb
niftany-0.5.1 .erb-linters/self_closing_custom.rb
niftany-0.5.0 .erb-linters/self_closing_custom.rb
niftany-0.4.0 .erb-linters/self_closing_custom.rb
niftany-0.3.0 .erb-linters/self_closing_custom.rb