@@ -4,6 +4,8 @@ import type {
44 Comment ,
55 Locations ,
66 Position ,
7+ SvelteElement ,
8+ SvelteName ,
79 SvelteScriptElement ,
810 SvelteStyleElement ,
911 Token ,
@@ -145,6 +147,12 @@ export class Context {
145147
146148 let start = 0 ;
147149 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+ }
148156 this . blocks . push ( block ) ;
149157 templateCode +=
150158 code . slice ( start , block . contentRange [ 0 ] ) +
@@ -183,6 +191,10 @@ export class Context {
183191 } ;
184192 }
185193
194+ public getIndexFromLoc ( loc : { line : number ; column : number } ) : number {
195+ return this . locs . getIndexFromLoc ( loc ) ;
196+ }
197+
186198 /**
187199 * Get the location information of the given node.
188200 * @param node The node.
@@ -278,9 +290,14 @@ export class Context {
278290 }
279291
280292 public findBlock (
281- element : SvelteScriptElement | SvelteStyleElement
293+ element : SvelteScriptElement | SvelteStyleElement | SvelteElement
282294 ) : 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 ( ) ;
284301 return this . blocks . find (
285302 ( block ) =>
286303 block . tag === tag &&
@@ -291,16 +308,17 @@ export class Context {
291308}
292309
293310type Block = {
294- tag : "script" | "style" ;
311+ tag : "script" | "style" | "template" ;
295312 attrs : AttributeToken [ ] ;
296313 contentRange : [ number , number ] ;
297314} ;
298315
299316/** Extract <script> blocks */
300317function * 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;
302319 const endScriptTagRe = / < \/ s c r i p t > / giu;
303320 const endStyleTagRe = / < \/ s t y l e > / giu;
321+ const endTemplateTagRe = / < \/ t e m p l a t e > / giu;
304322 let startTagOpenMatch ;
305323 while ( ( startTagOpenMatch = startTagOpenRe . exec ( code ) ) ) {
306324 const [ , tag , nextChar ] = startTagOpenMatch ;
@@ -323,8 +341,13 @@ function* extractBlocks(code: string): IterableIterator<Block> {
323341 continue ;
324342 }
325343 }
344+ const lowerTag = tag . toLowerCase ( ) as "script" | "style" | "template" ;
326345 const endTagRe =
327- tag . toLowerCase ( ) === "script" ? endScriptTagRe : endStyleTagRe ;
346+ lowerTag === "script"
347+ ? endScriptTagRe
348+ : lowerTag === "style"
349+ ? endStyleTagRe
350+ : endTemplateTagRe ;
328351 endTagRe . lastIndex = startTagEnd ;
329352 const endTagMatch = endTagRe . exec ( code ) ;
330353 if ( endTagMatch ) {
@@ -333,7 +356,7 @@ function* extractBlocks(code: string): IterableIterator<Block> {
333356 yield {
334357 contentRange,
335358 attrs,
336- tag : tag as "script" | "style" ,
359+ tag : lowerTag ,
337360 } ;
338361 startTagOpenRe . lastIndex = endTagRe . lastIndex ;
339362 }
0 commit comments