11import { ParserRule } from "../types.js"
22import { BaseRuleVisitor } from "./rule-utils.js"
3- import { isERBNode , isERBOutputNode } from "@herb-tools/core"
3+ import { getTagLocalName , isERBOpenTagNode } from "@herb-tools/core"
44
55import 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 = / \b j a v a s c r i p t _ t a g \b /
8+ const JAVASCRIPT_TAG_ELEMENT_SOURCE = "ActionView::Helpers::JavaScriptHelper#javascript_tag"
99
1010class 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
0 commit comments