-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy patherb-no-output-control-flow.ts
More file actions
63 lines (49 loc) · 1.91 KB
/
erb-no-output-control-flow.ts
File metadata and controls
63 lines (49 loc) · 1.91 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { BaseRuleVisitor } from "./utils/rule-utils.js"
import type { Node, ERBIfNode, ERBUnlessNode, ERBElseNode, ERBEndNode } from "@herb-tools/core"
import { ParserRule } from "../types.js"
import type { LintOffense, LintContext } from "../types.js"
class ERBNoOutputControlFlowRuleVisitor extends BaseRuleVisitor {
visitERBIfNode(node: ERBIfNode): void {
this.checkOutputControlFlow(node)
this.visitChildNodes(node)
}
visitERBUnlessNode(node: ERBUnlessNode): void {
this.checkOutputControlFlow(node)
this.visitChildNodes(node)
}
visitERBElseNode(node: ERBElseNode): void {
this.checkOutputControlFlow(node)
this.visitChildNodes(node)
}
visitERBEndNode(node: ERBEndNode): void {
this.checkOutputControlFlow(node)
this.visitChildNodes(node)
}
private checkOutputControlFlow(controlBlock: ERBIfNode | ERBUnlessNode | ERBElseNode | ERBEndNode): void {
const openTag = controlBlock.tag_opening;
if (!openTag) {
return
}
if (openTag.value === "<%="){
let controlBlockType: string = controlBlock.type
if (controlBlock.type === "AST_ERB_IF_NODE") controlBlockType = "if"
if (controlBlock.type === "AST_ERB_ELSE_NODE") controlBlockType = "else"
if (controlBlock.type === "AST_ERB_END_NODE") controlBlockType = "end"
if (controlBlock.type === "AST_ERB_UNLESS_NODE") controlBlockType = "unless"
this.addOffense(
`Control flow statements like \`${controlBlockType}\` should not be used with output tags. Use \`<% ${controlBlockType} ... %>\` instead.`,
openTag.location,
"error"
)
}
return
}
}
export class ERBNoOutputControlFlowRule extends ParserRule {
name = "erb-no-output-control-flow"
check(node: Node, context?: Partial<LintContext>): LintOffense[] {
const visitor = new ERBNoOutputControlFlowRuleVisitor(this.name, context)
visitor.visit(node)
return visitor.offenses
}
}