Skip to content

Commit 8477bea

Browse files
authored
Linter: Make erb-no-javascript-tag-helper Action View helper aware (#1442)
closes #553 the rule was already implemented via #1330, this just makes the rule Action View Helper aware to bring it in line with similar recent changes, such as #1438
1 parent e9382a1 commit 8477bea

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

javascript/packages/linter/src/rules/erb-no-javascript-tag-helper.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import { ParserRule } from "../types.js"
22
import { BaseRuleVisitor } from "./rule-utils.js"
3-
import { isERBNode, isERBOutputNode } from "@herb-tools/core"
3+
import { getTagLocalName, isERBOpenTagNode } from "@herb-tools/core"
44

55
import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js"
6-
import type { ParseResult, DocumentNode } from "@herb-tools/core"
6+
import type { ParseResult, ParserOptions, HTMLElementNode } from "@herb-tools/core"
77

8-
const JAVASCRIPT_TAG_PATTERN = /\bjavascript_tag\b/
8+
const JAVASCRIPT_TAG_ELEMENT_SOURCE = "ActionView::Helpers::JavaScriptHelper#javascript_tag"
99

1010
class ERBNoJavascriptTagHelperVisitor extends BaseRuleVisitor {
11-
visitDocumentNode(node: DocumentNode): void {
12-
for (const child of node.children || []) {
13-
if (!isERBNode(child)) continue
14-
if (!isERBOutputNode(child)) continue
15-
16-
const content = child.content?.value || ""
17-
18-
if (JAVASCRIPT_TAG_PATTERN.test(content)) {
19-
this.addOffense(
20-
"Avoid `javascript_tag`. Use inline `<script>` tags instead.",
21-
child.location,
22-
)
23-
}
11+
visitHTMLElementNode(node: HTMLElementNode): void {
12+
if (this.isJavascriptTagHelper(node)) {
13+
this.addOffense(
14+
"Avoid `javascript_tag`. Use inline `<script>` tags instead.",
15+
node.open_tag!.location,
16+
)
2417
}
2518

26-
super.visitDocumentNode(node)
19+
super.visitHTMLElementNode(node)
20+
}
21+
22+
private isJavascriptTagHelper(node: HTMLElementNode): node is HTMLElementNode {
23+
if (getTagLocalName(node) !== "script") return false
24+
if (!isERBOpenTagNode(node.open_tag)) return false
25+
26+
return node.element_source === JAVASCRIPT_TAG_ELEMENT_SOURCE
2727
}
2828
}
2929

@@ -37,6 +37,12 @@ export class ERBNoJavascriptTagHelperRule extends ParserRule {
3737
}
3838
}
3939

40+
get parserOptions(): Partial<ParserOptions> {
41+
return {
42+
action_view_helpers: true,
43+
}
44+
}
45+
4046
check(result: ParseResult, context?: Partial<LintContext>): UnboundLintOffense[] {
4147
const visitor = new ERBNoJavascriptTagHelperVisitor(this.ruleName, context)
4248

javascript/packages/linter/test/rules/erb-no-javascript-tag-helper.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ describe("ERBNoJavascriptTagHelperRule", () => {
2525
</script>
2626
`)
2727
})
28+
29+
test("javascript_include_tag is ignored", () => {
30+
expectNoOffenses(dedent`
31+
<%= javascript_include_tag "application" %>
32+
`)
33+
})
2834
})

0 commit comments

Comments
 (0)