Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { ParserRule } from "../types.js"
import { BaseRuleVisitor } from "./rule-utils.js"
import { isERBNode, isERBOutputNode } from "@herb-tools/core"
import { getTagLocalName, isERBOpenTagNode } from "@herb-tools/core"

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

const JAVASCRIPT_TAG_PATTERN = /\bjavascript_tag\b/
const JAVASCRIPT_TAG_ELEMENT_SOURCE = "ActionView::Helpers::JavaScriptHelper#javascript_tag"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should maybe be a constant somewhere? 🤔

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started this here in the language server package: https://github.com/marcoroth/herb/blob/a85a10d9856dfa2ccc815b2b72b287e1033ece31/javascript/packages/language-server/src/action_view_helpers.ts

But maybe we should move it somewhere else so you can check against the registery which Action View helpers are supported etc.


class ERBNoJavascriptTagHelperVisitor extends BaseRuleVisitor {
visitDocumentNode(node: DocumentNode): void {
for (const child of node.children || []) {
if (!isERBNode(child)) continue
if (!isERBOutputNode(child)) continue

const content = child.content?.value || ""

if (JAVASCRIPT_TAG_PATTERN.test(content)) {
this.addOffense(
"Avoid `javascript_tag`. Use inline `<script>` tags instead.",
child.location,
)
}
visitHTMLElementNode(node: HTMLElementNode): void {
if (this.isJavascriptTagHelper(node)) {
this.addOffense(
"Avoid `javascript_tag`. Use inline `<script>` tags instead.",
node.open_tag!.location,
)
}

super.visitDocumentNode(node)
super.visitHTMLElementNode(node)
}

private isJavascriptTagHelper(node: HTMLElementNode): node is HTMLElementNode {
if (getTagLocalName(node) !== "script") return false
if (!isERBOpenTagNode(node.open_tag)) return false

return node.element_source === JAVASCRIPT_TAG_ELEMENT_SOURCE
}
}

Expand All @@ -37,6 +37,12 @@ export class ERBNoJavascriptTagHelperRule extends ParserRule {
}
}

get parserOptions(): Partial<ParserOptions> {
return {
action_view_helpers: true,
}
}

check(result: ParseResult, context?: Partial<LintContext>): UnboundLintOffense[] {
const visitor = new ERBNoJavascriptTagHelperVisitor(this.ruleName, context)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ describe("ERBNoJavascriptTagHelperRule", () => {
</script>
`)
})

test("javascript_include_tag is ignored", () => {
expectNoOffenses(dedent`
<%= javascript_include_tag "application" %>
`)
})
})
Loading