Skip to content
Permalink
Browse files

Disables the default self-closing tag linter and adds a custom one wh…

…ich only checks for modern HTML tags that actually should be closed
  • Loading branch information...
cdmo committed Oct 15, 2018
1 parent d9548b3 commit d6a03ffa8206512446cd9eee22434d41338140e4
Showing with 45 additions and 0 deletions.
  1. +43 −0 .erb-linters/self_closing_custom.rb
  2. +2 −0 niftany_erblint.yml
@@ -0,0 +1,43 @@
# 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
@@ -37,6 +37,8 @@ linters:
Rails/OutputSafety:
Enabled: false
SelfClosingTag:
enabled: false
SelfClosingTagCustom:
enabled: true
SpaceAroundErbTag:
enabled: true

0 comments on commit d6a03ff

Please sign in to comment.
You can’t perform that action at this time.