@@ -37,7 +37,25 @@ func PrintToTSX(sourcetext string, n *Node, opts TSXOptions, transformOpts trans
3737 return PrintResult {
3838 Output : p .output ,
3939 SourceMapChunk : p .builder .GenerateChunk (p .output ),
40- TSXRanges : p .ranges ,
40+ TSXRanges : finalizeRanges (string (p .output ), p .ranges ),
41+ }
42+ }
43+
44+ func finalizeRanges (content string , ranges TSXRanges ) TSXRanges {
45+ chunkBuilder := sourcemap .MakeChunkBuilder (nil , sourcemap .GenerateLineOffsetTables (content , len (strings .Split (content , "\n " ))))
46+
47+ return TSXRanges {
48+ Frontmatter : loc.TSXRange {
49+ Start : chunkBuilder .OffsetAt (loc.Loc {Start : ranges .Frontmatter .Start }),
50+ End : chunkBuilder .OffsetAt (loc.Loc {Start : ranges .Frontmatter .End }),
51+ },
52+ Body : loc.TSXRange {
53+ Start : chunkBuilder .OffsetAt (loc.Loc {Start : ranges .Body .Start }),
54+ End : chunkBuilder .OffsetAt (loc.Loc {Start : ranges .Body .End }),
55+ },
56+ // Scripts and styles are already using the proper positions
57+ Scripts : ranges .Scripts ,
58+ Styles : ranges .Styles ,
4159 }
4260}
4361
@@ -318,7 +336,7 @@ func renderTsx(p *printer, n *Node, o *TSXOptions) {
318336 props := js_scanner .GetPropsType (source )
319337 hasGetStaticPaths := js_scanner .HasGetStaticPaths (source )
320338 hasChildren := false
321- startLoc := len (p .output )
339+ startLen := len (p .output )
322340 for c := n .FirstChild ; c != nil ; c = c .NextSibling {
323341 // This checks for the first node that comes *after* the frontmatter
324342 // to ensure that the statement is properly closed with a `;`.
@@ -338,15 +356,15 @@ func renderTsx(p *printer, n *Node, o *TSXOptions) {
338356 p .print ("<Fragment>\n " )
339357
340358 // Update the start location of the body to the start of the first child
341- startLoc = len (p .output )
359+ startLen = len (p .output )
342360
343361 hasChildren = true
344362 }
345363 if c .PrevSibling == nil && c .Type != FrontmatterNode {
346364 p .addNilSourceMapping ()
347365 p .print ("<Fragment>\n " )
348366
349- startLoc = len (p .output )
367+ startLen = len (p .output )
350368
351369 hasChildren = true
352370 }
@@ -357,7 +375,7 @@ func renderTsx(p *printer, n *Node, o *TSXOptions) {
357375
358376 p .addNilSourceMapping ()
359377 p .setTSXBodyRange (loc.TSXRange {
360- Start : startLoc ,
378+ Start : startLen ,
361379 End : len (p .output ),
362380 })
363381
@@ -435,8 +453,6 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
435453 p .printTextWithSourcemap (n .Data , n .Loc [0 ])
436454 p .addNilSourceMapping ()
437455 p .print ("}}\n " )
438- } else {
439- p .collectMultiByteCharacters (n .Data )
440456 }
441457 p .addSourceMapping (loc.Loc {Start : n .Loc [0 ].Start + len (n .Data )})
442458 } else if textType == StyleText || textType == JsonScriptText || textType == RawText || textType == UnknownScriptText {
@@ -446,8 +462,6 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
446462 p .printTextWithSourcemap (escapeText (n .Data ), n .Loc [0 ])
447463 p .addNilSourceMapping ()
448464 p .print ("`}" )
449- } else {
450- p .collectMultiByteCharacters (n .Data )
451465 }
452466 p .addSourceMapping (loc.Loc {Start : n .Loc [0 ].Start + len (n .Data )})
453467 } else {
@@ -572,11 +586,12 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
572586 p .print (`"` )
573587 endLoc = a .ValLoc .Start
574588 }
589+
575590 if _ , ok := htmlEvents [a .Key ]; ok {
576- p .addTSXScript (a . ValLoc . Start - p . bytesToSkip , endLoc - p . bytesToSkip , a .Val , "event-attribute" )
591+ p .addTSXScript (p . builder . OffsetAt ( a . ValLoc ), p . builder . OffsetAt (loc. Loc { Start : endLoc }) , a .Val , "event-attribute" )
577592 }
578593 if a .Key == "style" {
579- p .addTSXStyle (a . ValLoc . Start - p . bytesToSkip , endLoc - p . bytesToSkip , a .Val , "style-attribute" , "css" )
594+ p .addTSXStyle (p . builder . OffsetAt ( a . ValLoc ), p . builder . OffsetAt (loc. Loc { Start : endLoc }) , a .Val , "style-attribute" , "css" )
580595 }
581596 case astro .EmptyAttribute :
582597 p .print (a .Key )
@@ -739,7 +754,7 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
739754 }
740755 p .print (">" )
741756
742- startTagEnd := endLoc - p . bytesToSkip
757+ startTagEndLoc := loc. Loc { Start : endLoc }
743758
744759 // Render any child nodes
745760 for c := n .FirstChild ; c != nil ; c = c .NextSibling {
@@ -760,11 +775,15 @@ declare const Astro: Readonly<import('astro').AstroGlobal<%s, typeof %s`, propsI
760775 }
761776
762777 if n .FirstChild != nil && (n .DataAtom == atom .Script || n .DataAtom == atom .Style ) {
778+ tagContentEndLoc := loc.Loc {Start : endLoc }
779+ if endLoc > len (p .sourcetext ) { // Sometimes, when tags are not closed properly, endLoc can be greater than the length of the source text, wonky stuff
780+ tagContentEndLoc .Start = len (p .sourcetext )
781+ }
763782 if n .DataAtom == atom .Script {
764- p .addTSXScript (startTagEnd , endLoc - p . bytesToSkip , n .FirstChild .Data , getScriptTypeFromAttrs (n .Attr ))
783+ p .addTSXScript (p . builder . OffsetAt ( startTagEndLoc ), p . builder . OffsetAt ( tagContentEndLoc ) , n .FirstChild .Data , getScriptTypeFromAttrs (n .Attr ))
765784 }
766785 if n .DataAtom == atom .Style {
767- p .addTSXStyle (startTagEnd , endLoc - p . bytesToSkip , n .FirstChild .Data , "tag" , getStyleLangFromAttrs (n .Attr ))
786+ p .addTSXStyle (p . builder . OffsetAt ( startTagEndLoc ), p . builder . OffsetAt ( tagContentEndLoc ) , n .FirstChild .Data , "tag" , getStyleLangFromAttrs (n .Attr ))
768787 }
769788 }
770789
0 commit comments