Skip to content

Commit 9b9bd60

Browse files
committed
test(block-lang): Added support for multiple blocks of the same type
1 parent f8e4ee4 commit 9b9bd60

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/rules/block-lang.ts

+27-23
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,37 @@ export default createRule("block-lang", {
7272
const allowedScriptLangs: (string | null)[] = Array.isArray(scriptOption)
7373
? scriptOption
7474
: [scriptOption]
75-
let scriptLang: string | null = null
76-
let scriptNode: SvelteScriptElement | undefined = undefined
75+
const scriptNodes: SvelteScriptElement[] = []
7776

7877
const styleOption: string | null | (string | null)[] =
7978
context.options[0]?.style ?? null
8079
const allowedStyleLangs: (string | null)[] = Array.isArray(styleOption)
8180
? styleOption
8281
: [styleOption]
83-
let styleLang: string | null = null
84-
let styleNode: SvelteStyleElement | undefined = undefined
82+
const styleNodes: SvelteStyleElement[] = []
8583

8684
return {
8785
SvelteScriptElement(node) {
88-
scriptNode = node
89-
scriptLang = getLangValue(node)?.toLowerCase() ?? null
86+
scriptNodes.push(node)
9087
},
9188
SvelteStyleElement(node) {
92-
styleNode = node
93-
styleLang = getLangValue(node)?.toLowerCase() ?? null
89+
styleNodes.push(node)
9490
},
9591
"Program:exit"() {
96-
if (!allowedScriptLangs.includes(scriptLang)) {
97-
if (scriptNode !== undefined) {
92+
if (scriptNodes.length === 0 && enforceScriptPresent) {
93+
context.report({
94+
loc: { line: 1, column: 1 },
95+
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
96+
allowedScriptLangs,
97+
)}.`,
98+
})
99+
}
100+
for (const scriptNode of scriptNodes) {
101+
if (
102+
!allowedScriptLangs.includes(
103+
getLangValue(scriptNode)?.toLowerCase() ?? null,
104+
)
105+
) {
98106
context.report({
99107
node: scriptNode,
100108
message: `The lang attribute of the <script> block should be ${prettyPrintLangs(
@@ -103,16 +111,20 @@ export default createRule("block-lang", {
103111
})
104112
}
105113
}
106-
if (scriptNode === undefined && enforceScriptPresent) {
114+
if (styleNodes.length === 0 && enforceStylePresent) {
107115
context.report({
108116
loc: { line: 1, column: 1 },
109-
message: `The <script> block should be present and its lang attribute should be ${prettyPrintLangs(
110-
allowedScriptLangs,
117+
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(
118+
allowedStyleLangs,
111119
)}.`,
112120
})
113121
}
114-
if (!allowedStyleLangs.includes(styleLang)) {
115-
if (styleNode !== undefined) {
122+
for (const styleNode of styleNodes) {
123+
if (
124+
!allowedStyleLangs.includes(
125+
getLangValue(styleNode)?.toLowerCase() ?? null,
126+
)
127+
) {
116128
context.report({
117129
node: styleNode,
118130
message: `The lang attribute of the <style> block should be ${prettyPrintLangs(
@@ -121,14 +133,6 @@ export default createRule("block-lang", {
121133
})
122134
}
123135
}
124-
if (styleNode === undefined && enforceStylePresent) {
125-
context.report({
126-
loc: { line: 1, column: 1 },
127-
message: `The <style> block should be present and its lang attribute should be ${prettyPrintLangs(
128-
allowedStyleLangs,
129-
)}.`,
130-
})
131-
}
132136
},
133137
}
134138
},

0 commit comments

Comments
 (0)