-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy patherb-strict-locals-required.ts
More file actions
59 lines (45 loc) · 1.77 KB
/
erb-strict-locals-required.ts
File metadata and controls
59 lines (45 loc) · 1.77 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
import { ParserRule } from "../types.js"
import { Location, ERBStrictLocalsNode, createLiteral } from "@herb-tools/core"
import { BaseRuleVisitor } from "./rule-utils.js"
import { isPartialFile } from "./file-utils.js"
import type { UnboundLintOffense, LintOffense, LintContext, FullRuleConfig } from "../types.js"
import type { ParseResult, DocumentNode } from "@herb-tools/core"
class ERBStrictLocalsRequiredVisitor extends BaseRuleVisitor {
foundStrictLocals: boolean = false
visitERBStrictLocalsNode(_node: ERBStrictLocalsNode): void {
this.foundStrictLocals = true
}
}
export class ERBStrictLocalsRequiredRule extends ParserRule {
static unsafeAutocorrectable = true
static ruleName = "erb-strict-locals-required"
static introducedIn = this.version("0.8.8")
get parserOptions() {
return { strict_locals: true }
}
get defaultConfig(): FullRuleConfig {
return {
enabled: false,
severity: "error",
}
}
check(result: ParseResult, context?: Partial<LintContext>): UnboundLintOffense[] {
if (isPartialFile(context?.fileName) !== true) return []
const visitor = new ERBStrictLocalsRequiredVisitor(this.ruleName, context)
visitor.visit(result.value)
if (visitor.foundStrictLocals) return []
const document = result.value
const firstChild = document.children[0]
const end = firstChild ? firstChild.location.end : Location.zero.end
return [
this.createOffense(
"Partial is missing a strict locals declaration. Add `<%# locals: (...) %>` at the top of the file.",
Location.from(1, 0, end.line, end.column)
)
]
}
autofix(_offense: LintOffense, result: ParseResult): ParseResult | null {
result.value.children.unshift(createLiteral("<%# locals: () %>\n\n"))
return result
}
}