@@ -72,29 +72,37 @@ export default createRule("block-lang", {
72
72
const allowedScriptLangs : ( string | null ) [ ] = Array . isArray ( scriptOption )
73
73
? scriptOption
74
74
: [ scriptOption ]
75
- let scriptLang : string | null = null
76
- let scriptNode : SvelteScriptElement | undefined = undefined
75
+ const scriptNodes : SvelteScriptElement [ ] = [ ]
77
76
78
77
const styleOption : string | null | ( string | null ) [ ] =
79
78
context . options [ 0 ] ?. style ?? null
80
79
const allowedStyleLangs : ( string | null ) [ ] = Array . isArray ( styleOption )
81
80
? styleOption
82
81
: [ styleOption ]
83
- let styleLang : string | null = null
84
- let styleNode : SvelteStyleElement | undefined = undefined
82
+ const styleNodes : SvelteStyleElement [ ] = [ ]
85
83
86
84
return {
87
85
SvelteScriptElement ( node ) {
88
- scriptNode = node
89
- scriptLang = getLangValue ( node ) ?. toLowerCase ( ) ?? null
86
+ scriptNodes . push ( node )
90
87
} ,
91
88
SvelteStyleElement ( node ) {
92
- styleNode = node
93
- styleLang = getLangValue ( node ) ?. toLowerCase ( ) ?? null
89
+ styleNodes . push ( node )
94
90
} ,
95
91
"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
+ ) {
98
106
context . report ( {
99
107
node : scriptNode ,
100
108
message : `The lang attribute of the <script> block should be ${ prettyPrintLangs (
@@ -103,16 +111,20 @@ export default createRule("block-lang", {
103
111
} )
104
112
}
105
113
}
106
- if ( scriptNode === undefined && enforceScriptPresent ) {
114
+ if ( styleNodes . length === 0 && enforceStylePresent ) {
107
115
context . report ( {
108
116
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 ,
111
119
) } .`,
112
120
} )
113
121
}
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
+ ) {
116
128
context . report ( {
117
129
node : styleNode ,
118
130
message : `The lang attribute of the <style> block should be ${ prettyPrintLangs (
@@ -121,14 +133,6 @@ export default createRule("block-lang", {
121
133
} )
122
134
}
123
135
}
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
- }
132
136
} ,
133
137
}
134
138
} ,
0 commit comments