-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathhtml-no-duplicate-ids.ts
More file actions
39 lines (29 loc) · 1.07 KB
/
html-no-duplicate-ids.ts
File metadata and controls
39 lines (29 loc) · 1.07 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
import { AttributeVisitorMixin } from "./utils/rule-utils.js"
import { ParserRule } from "../types.js"
import type { Node } from "@herb-tools/core"
import type { LintOffense, LintContext } from "../types.js"
class NoDuplicateIdsVisitor extends AttributeVisitorMixin {
private documentIds: Set<string> = new Set<string>()
protected checkAttribute(attributeName: string, attributeValue: string | null, attributeNode: Node): void {
if (attributeName.toLowerCase() !== "id") return
if (!attributeValue) return
const id = attributeValue.trim()
if (this.documentIds.has(id)) {
this.addOffense(
`Duplicate ID \`${id}\` found. IDs must be unique within a document.`,
attributeNode.location,
"error"
)
return
}
this.documentIds.add(id)
}
}
export class HTMLNoDuplicateIdsRule extends ParserRule {
name = "html-no-duplicate-ids"
check(node: Node, context?: Partial<LintContext>): LintOffense[] {
const visitor = new NoDuplicateIdsVisitor(this.name, context)
visitor.visit(node)
return visitor.offenses
}
}