-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathhtml-anchor-require-href.ts
More file actions
40 lines (31 loc) · 1.05 KB
/
html-anchor-require-href.ts
File metadata and controls
40 lines (31 loc) · 1.05 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
import { BaseRuleVisitor, getTagName, hasAttribute } from "./utils/rule-utils.js"
import { ParserRule } from "../types.js"
import type { LintOffense, LintContext } from "../types.js"
import type { HTMLOpenTagNode, Node } from "@herb-tools/core"
class AnchorRechireHrefVisitor extends BaseRuleVisitor {
visitHTMLOpenTagNode(node: HTMLOpenTagNode): void {
this.checkATag(node)
super.visitHTMLOpenTagNode(node)
}
private checkATag(node: HTMLOpenTagNode): void {
const tagName = getTagName(node)
if (tagName !== "a") {
return
}
if (!hasAttribute(node, "href")) {
this.addOffense(
"Add an `href` attribute to `<a>` to ensure it is focusable and accessible.",
node.tag_name!.location,
"error",
)
}
}
}
export class HTMLAnchorRequireHrefRule extends ParserRule {
name = "html-anchor-require-href"
check(node: Node, context?: Partial<LintContext>): LintOffense[] {
const visitor = new AnchorRechireHrefVisitor(this.name, context)
visitor.visit(node)
return visitor.offenses
}
}