-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathhtml-no-self-closing.ts
More file actions
41 lines (33 loc) · 1.21 KB
/
html-no-self-closing.ts
File metadata and controls
41 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { ParserRule } from "../types.js"
import { BaseRuleVisitor, isVoidElement } from "./rule-utils.js"
import { getTagName } from "@herb-tools/core"
import type { LintContext, LintOffense } from "../types.js"
import type { HTMLOpenTagNode, HTMLElementNode, ParseResult } from "@herb-tools/core"
class NoSelfClosingVisitor extends BaseRuleVisitor {
visitHTMLElementNode(node: HTMLElementNode): void {
if (getTagName(node) === "svg") {
this.visit(node.open_tag)
} else {
this.visitChildNodes(node)
}
}
visitHTMLOpenTagNode(node: HTMLOpenTagNode): void {
if (node.tag_closing?.value === "/>") {
const tagName = getTagName(node)
const instead = isVoidElement(tagName) ? `<${tagName}>` : `<${tagName}></${tagName}>`
this.addOffense(
`Use \`${instead}\` instead of self-closing \`<${tagName} />\` for HTML compatibility.`,
node.location,
"error"
)
}
}
}
export class HTMLNoSelfClosingRule extends ParserRule {
name = "html-no-self-closing"
check(result: ParseResult, context?: Partial<LintContext>): LintOffense[] {
const visitor = new NoSelfClosingVisitor(this.name, context)
visitor.visit(result.value)
return visitor.offenses
}
}