Skip to content

Commit e037569

Browse files
committed
f
1 parent f275f9f commit e037569

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

javascript/packages/linter/src/rules/html-disallow-inline-scripts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ParserRule } from "../types.js"
2-
import { BaseRuleVisitor, isJavaScriptTag } from "./rule-utils.js"
2+
import { BaseRuleVisitor, isJavaScriptTagElement } from "./rule-utils.js"
33
import { getTagLocalName, getAttributeName } from "@herb-tools/core"
44

55
import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js"
@@ -135,7 +135,7 @@ class HTMLDisallowInlineScriptsVisitor extends BaseRuleVisitor {
135135
}
136136

137137
private checkInlineScript(node: HTMLElementNode): void {
138-
if (!isJavaScriptTag(node)) return
138+
if (!isJavaScriptTagElement(node)) return
139139

140140
this.addOffense(
141141
"Avoid inline `<script>` tags. Use `javascript_include_tag` to include external JavaScript files instead.",

javascript/packages/linter/src/rules/html-require-script-nonce.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getTagLocalName, getStaticAttributeValue, hasAttributeValue } from "@herb-tools/core"
22
import type { ParseResult, ParserOptions, HTMLElementNode, HTMLAttributeNode } from "@herb-tools/core"
33

4-
import { BaseRuleVisitor, findElementAttribute, isJavaScriptTag } from "./rule-utils.js"
4+
import { BaseRuleVisitor, findElementAttribute, isJavaScriptTagElement } from "./rule-utils.js"
55
import { ParserRule } from "../types.js"
66
import type { UnboundLintOffense, LintContext, FullRuleConfig } from "../types.js"
77

@@ -20,7 +20,7 @@ class RequireScriptNonceVisitor extends BaseRuleVisitor {
2020
}
2121

2222
private checkScriptNonce(node: HTMLElementNode): void {
23-
if (!isJavaScriptTag(node)) return
23+
if (!isJavaScriptTagElement(node)) return
2424

2525
const nonceAttribute = findElementAttribute(node, "nonce")
2626

javascript/packages/linter/src/rules/rule-utils.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -499,32 +499,6 @@ export function isVoidElement(tagName: string): boolean {
499499
return HTML_VOID_ELEMENTS.has(tagName.toLowerCase())
500500
}
501501

502-
export function findElementAttribute(node: HTMLElementNode, name: string): HTMLAttributeNode | null {
503-
if (isERBOpenTagNode(node.open_tag)) {
504-
return findAttributeByName(node.open_tag.children, name)
505-
}
506-
507-
return getAttribute(node, name)
508-
}
509-
510-
const NON_JS_SCRIPT_TYPES = new Set([
511-
"application/json",
512-
"application/ld+json",
513-
"text/template",
514-
"text/html",
515-
"text/x-template",
516-
])
517-
518-
export function isJavaScriptTag(node: HTMLElementNode): boolean {
519-
const typeAttribute = findElementAttribute(node, "type")
520-
if (!typeAttribute) return true
521-
522-
const typeValue = getStaticAttributeValue(typeAttribute)
523-
if (typeValue === null) return true
524-
525-
return !NON_JS_SCRIPT_TYPES.has(typeValue.toLowerCase())
526-
}
527-
528502
/**
529503
* Attribute visitor that provides granular processing based on both
530504
* attribute name type (static/dynamic) and value type (static/dynamic)
@@ -1090,3 +1064,29 @@ export function findNodeAtPosition(root: Node, line: number, column: number, pre
10901064

10911065
return bestMatch
10921066
}
1067+
1068+
export function findElementAttribute(node: HTMLElementNode, name: string): HTMLAttributeNode | null {
1069+
if (isERBOpenTagNode(node.open_tag)) {
1070+
return findAttributeByName(node.open_tag.children, name)
1071+
}
1072+
1073+
return getAttribute(node, name)
1074+
}
1075+
1076+
const NON_JS_SCRIPT_TYPES = new Set([
1077+
"application/json",
1078+
"application/ld+json",
1079+
"text/template",
1080+
"text/html",
1081+
"text/x-template",
1082+
])
1083+
1084+
export function isJavaScriptTagElement(node: HTMLElementNode): boolean {
1085+
const typeAttribute = findElementAttribute(node, "type")
1086+
if (!typeAttribute) return true
1087+
1088+
const typeValue = getStaticAttributeValue(typeAttribute)
1089+
if (typeValue === null) return true
1090+
1091+
return !NON_JS_SCRIPT_TYPES.has(typeValue.toLowerCase())
1092+
}

0 commit comments

Comments
 (0)