@@ -4,6 +4,8 @@ import type {
4
4
Comment ,
5
5
Locations ,
6
6
Position ,
7
+ SvelteElement ,
8
+ SvelteName ,
7
9
SvelteScriptElement ,
8
10
SvelteStyleElement ,
9
11
Token ,
@@ -145,6 +147,12 @@ export class Context {
145
147
146
148
let start = 0 ;
147
149
for ( const block of extractBlocks ( code ) ) {
150
+ if ( block . tag === "template" ) {
151
+ const lang = block . attrs . find ( ( attr ) => attr . key . name === "lang" ) ;
152
+ if ( ! lang || ! lang . value || lang . value . value === "html" ) {
153
+ continue ;
154
+ }
155
+ }
148
156
this . blocks . push ( block ) ;
149
157
templateCode +=
150
158
code . slice ( start , block . contentRange [ 0 ] ) +
@@ -183,6 +191,10 @@ export class Context {
183
191
} ;
184
192
}
185
193
194
+ public getIndexFromLoc ( loc : { line : number ; column : number } ) : number {
195
+ return this . locs . getIndexFromLoc ( loc ) ;
196
+ }
197
+
186
198
/**
187
199
* Get the location information of the given node.
188
200
* @param node The node.
@@ -278,9 +290,14 @@ export class Context {
278
290
}
279
291
280
292
public findBlock (
281
- element : SvelteScriptElement | SvelteStyleElement
293
+ element : SvelteScriptElement | SvelteStyleElement | SvelteElement
282
294
) : Block | undefined {
283
- const tag = element . type === "SvelteScriptElement" ? "script" : "style" ;
295
+ const tag =
296
+ element . type === "SvelteScriptElement"
297
+ ? "script"
298
+ : element . type === "SvelteStyleElement"
299
+ ? "style"
300
+ : ( element . name as SvelteName ) . name . toLowerCase ( ) ;
284
301
return this . blocks . find (
285
302
( block ) =>
286
303
block . tag === tag &&
@@ -291,16 +308,17 @@ export class Context {
291
308
}
292
309
293
310
type Block = {
294
- tag : "script" | "style" ;
311
+ tag : "script" | "style" | "template" ;
295
312
attrs : AttributeToken [ ] ;
296
313
contentRange : [ number , number ] ;
297
314
} ;
298
315
299
316
/** Extract <script> blocks */
300
317
function * extractBlocks ( code : string ) : IterableIterator < Block > {
301
- const startTagOpenRe = / < ! - - [ \s \S ] * ?- - > | < ( s c r i p t | s t y l e ) ( [ \s > ] ) / giu;
318
+ const startTagOpenRe = / < ! - - [ \s \S ] * ?- - > | < ( s c r i p t | s t y l e | t e m p l a t e ) ( [ \s > ] ) / giu;
302
319
const endScriptTagRe = / < \/ s c r i p t > / giu;
303
320
const endStyleTagRe = / < \/ s t y l e > / giu;
321
+ const endTemplateTagRe = / < \/ t e m p l a t e > / giu;
304
322
let startTagOpenMatch ;
305
323
while ( ( startTagOpenMatch = startTagOpenRe . exec ( code ) ) ) {
306
324
const [ , tag , nextChar ] = startTagOpenMatch ;
@@ -323,8 +341,13 @@ function* extractBlocks(code: string): IterableIterator<Block> {
323
341
continue ;
324
342
}
325
343
}
344
+ const lowerTag = tag . toLowerCase ( ) as "script" | "style" | "template" ;
326
345
const endTagRe =
327
- tag . toLowerCase ( ) === "script" ? endScriptTagRe : endStyleTagRe ;
346
+ lowerTag === "script"
347
+ ? endScriptTagRe
348
+ : lowerTag === "style"
349
+ ? endStyleTagRe
350
+ : endTemplateTagRe ;
328
351
endTagRe . lastIndex = startTagEnd ;
329
352
const endTagMatch = endTagRe . exec ( code ) ;
330
353
if ( endTagMatch ) {
@@ -333,7 +356,7 @@ function* extractBlocks(code: string): IterableIterator<Block> {
333
356
yield {
334
357
contentRange,
335
358
attrs,
336
- tag : tag as "script" | "style" ,
359
+ tag : lowerTag ,
337
360
} ;
338
361
startTagOpenRe . lastIndex = endTagRe . lastIndex ;
339
362
}
0 commit comments