@@ -5,8 +5,8 @@ import type { ScopeManager } from "eslint-scope"
5
5
import { TemplateScopeManager } from "./template-scope-manager"
6
6
7
7
type ContextSourceCode = {
8
- svelte : string
9
- script : {
8
+ template : string
9
+ scripts : {
10
10
code : string
11
11
attrs : Record < string , string | undefined >
12
12
}
@@ -33,25 +33,30 @@ export class Context {
33
33
this . parserOptions = parserOptions
34
34
this . locs = new LinesAndColumns ( code )
35
35
36
- let svelteCode = ""
36
+ let templateCode = ""
37
37
let scriptCode = ""
38
38
let scriptAttrs : Record < string , string | undefined > = { }
39
39
40
40
let start = 0
41
- for ( const script of extractScriptBlocks ( code ) ) {
42
- const before = code . slice ( start , script . codeRange [ 0 ] )
43
- svelteCode += before + script . code . replace ( / [ ^ \n \r ] / g, " " )
44
- scriptCode += before . replace ( / [ ^ \n \r ] / g, " " ) + script . code
45
- scriptAttrs = Object . assign ( scriptAttrs , script . attrs )
46
- start = script . codeRange [ 1 ]
41
+ for ( const block of extractBlocks ( code ) ) {
42
+ const before = code . slice ( start , block . codeRange [ 0 ] )
43
+ const blankCode = block . code . replace ( / [ ^ \n \r ] / g, " " )
44
+ templateCode += before + blankCode
45
+ if ( block . tag === "script" ) {
46
+ scriptCode += before . replace ( / [ ^ \n \r ] / g, " " ) + block . code
47
+ scriptAttrs = Object . assign ( scriptAttrs , block . attrs )
48
+ } else {
49
+ scriptCode += before . replace ( / [ ^ \n \r ] / g, " " ) + blankCode
50
+ }
51
+ start = block . codeRange [ 1 ]
47
52
}
48
53
const before = code . slice ( start )
49
- svelteCode += before
54
+ templateCode += before
50
55
scriptCode += before . replace ( / [ ^ \n \r ] / g, " " )
51
56
52
57
this . sourceCode = {
53
- svelte : svelteCode ,
54
- script : {
58
+ template : templateCode ,
59
+ scripts : {
55
60
code : scriptCode ,
56
61
attrs : scriptAttrs ,
57
62
} ,
@@ -125,36 +130,43 @@ export class Context {
125
130
}
126
131
127
132
/** Extract <script> blocks */
128
- function * extractScriptBlocks ( code : string ) : IterableIterator < {
133
+ function * extractBlocks ( code : string ) : IterableIterator < {
129
134
code : string
130
135
codeRange : [ number , number ]
131
- tag : string
132
- tagRange : [ number , number ]
133
136
attrs : Record < string , string | undefined >
137
+ tag : "script" | "style"
134
138
} > {
135
- const scriptRe = / < s c r i p t ( \s [ \s \S ] * ?) ? > ( [ \s \S ] * ?) < \/ s c r i p t > / giu
136
- let res
137
- while ( ( res = scriptRe . exec ( code ) ) ) {
138
- const [ tag , attributes = "" , context ] = res
139
- const tagRange : [ number , number ] = [ res . index , scriptRe . lastIndex ]
140
- const codeRange : [ number , number ] = [
141
- tagRange [ 0 ] + 8 + attributes . length ,
142
- tagRange [ 1 ] - 9 ,
143
- ]
144
-
145
- const attrRe =
146
- // eslint-disable-next-line regexp/no-unused-capturing-group -- maybe bug
147
- / ( < k e y > [ ^ \s = ] + ) (?: = (?: " ( < v a l > [ ^ " ] * ) " | ' ( < v a l > [ ^ " ] * ) ' | ( < v a l > [ ^ \s = ] + ) ) ) ? / giu
148
- const attrs : Record < string , string | undefined > = { }
149
- while ( ( res = attrRe . exec ( attributes ) ) ) {
150
- attrs [ res . groups ! . key ] = res . groups ! . val
151
- }
152
- yield {
153
- code : context ,
154
- codeRange,
155
- tag,
156
- tagRange,
157
- attrs,
139
+ const startTagRe = / < ( s c r i p t | s t y l e ) ( \s [ \s \S ] * ?) ? > / giu
140
+ const endScriptTagRe = / < \/ s c r i p t (?: \s [ \s \S ] * ?) ? > / giu
141
+ const endStyleTagRe = / < \/ s t y l e (?: \s [ \s \S ] * ?) ? > / giu
142
+ let startTagRes
143
+ while ( ( startTagRes = startTagRe . exec ( code ) ) ) {
144
+ const [ startTag , tag , attributes = "" ] = startTagRes
145
+ const startTagStart = startTagRes . index
146
+ const startTagEnd = startTagStart + startTag . length
147
+ const endTagRe =
148
+ tag . toLowerCase ( ) === "script" ? endScriptTagRe : endStyleTagRe
149
+ endTagRe . lastIndex = startTagRe . lastIndex
150
+ const endTagRes = endTagRe . exec ( code )
151
+ if ( endTagRes ) {
152
+ const endTagStart = endTagRes . index
153
+ const codeRange : [ number , number ] = [ startTagEnd , endTagStart ]
154
+
155
+ const attrRe =
156
+ // eslint-disable-next-line regexp/no-unused-capturing-group -- maybe bug
157
+ / ( < k e y > [ ^ \s = ] + ) (?: = (?: " ( < v a l > [ ^ " ] * ) " | ' ( < v a l > [ ^ " ] * ) ' | ( < v a l > [ ^ \s = ] + ) ) ) ? / giu
158
+ const attrs : Record < string , string | undefined > = { }
159
+ let attrRes
160
+ while ( ( attrRes = attrRe . exec ( attributes ) ) ) {
161
+ attrs [ attrRes . groups ! . key ] = attrRes . groups ! . val
162
+ }
163
+ yield {
164
+ code : code . slice ( ...codeRange ) ,
165
+ codeRange,
166
+ attrs,
167
+ tag : tag as "script" | "style" ,
168
+ }
169
+ startTagRe . lastIndex = endTagRe . lastIndex
158
170
}
159
171
}
160
172
}
0 commit comments