-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathactionview-no-silent-helper.ts
More file actions
58 lines (45 loc) · 1.71 KB
/
actionview-no-silent-helper.ts
File metadata and controls
58 lines (45 loc) · 1.71 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
import { ParserRule } from "../types.js"
import { BaseRuleVisitor } from "./rule-utils.js"
import { isERBOpenTagNode, isERBOutputNode } from "@herb-tools/core"
import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js"
import type { ParseResult, HTMLElementNode, ParserOptions } from "@herb-tools/core"
class ActionViewNoSilentHelperVisitor extends BaseRuleVisitor {
visitHTMLElementNode(node: HTMLElementNode): void {
this.checkActionViewHelper(node)
super.visitHTMLElementNode(node)
}
private checkActionViewHelper(node: HTMLElementNode): void {
if (!node.element_source || node.element_source === "HTML") return
if (!isERBOpenTagNode(node.open_tag)) return
if (isERBOutputNode(node.open_tag)) return
const tagOpening = node.open_tag.tag_opening?.value
if (!tagOpening) return
const helperName = node.element_source.includes("#")
? node.element_source.split("#").pop()
: node.element_source
this.addOffense(
`Avoid using \`${tagOpening} %>\` with \`${helperName}\`. Use \`<%= %>\` to ensure the helper's output is rendered.`,
node.open_tag.location,
)
}
}
export class ActionViewNoSilentHelperRule extends ParserRule {
static ruleName = "actionview-no-silent-helper"
get defaultConfig(): FullRuleConfig {
return {
enabled: true,
severity: "error"
}
}
get parserOptions(): Partial<ParserOptions> {
return {
track_whitespace: true,
action_view_helpers: true,
}
}
check(result: ParseResult, context?: Partial<LintContext>): UnboundLintOffense[] {
const visitor = new ActionViewNoSilentHelperVisitor(this.ruleName, context)
visitor.visit(result.value)
return visitor.offenses
}
}