11import {
2- getBuffer ,
32 parse ,
43 type Range ,
5- replaceContents ,
64 sliceByteRange ,
75} from '../utils/content-tag.js' ;
86
@@ -17,8 +15,6 @@ export interface Template {
1715 } ;
1816}
1917
20- const PLACEHOLDER = '~' ;
21-
2218/**
2319 * Replace the template with a parsable placeholder that takes up the same
2420 * range.
@@ -27,6 +23,9 @@ export function preprocessTemplateRange(
2723 template : Template ,
2824 code : string ,
2925) : string {
26+ const { start, end } = template . utf16Range ;
27+ const after = code . slice ( end ) ;
28+
3029 let prefix : string ;
3130 let suffix : string ;
3231
@@ -39,7 +38,7 @@ export function preprocessTemplateRange(
3938 prefix = '{/*' ;
4039 suffix = '*/}' ;
4140
42- const nextToken = sliceByteRange ( code , template . range . endByte ) . match ( / \S + / ) ;
41+ const nextToken = after . match ( / \S + / ) ;
4342
4443 if ( nextToken && ( nextToken [ 0 ] === 'as' || nextToken [ 0 ] === 'satisfies' ) ) {
4544 // Replace with parenthesized ObjectExpression
@@ -48,18 +47,14 @@ export function preprocessTemplateRange(
4847 }
4948 }
5049
51- // We need to replace forward slash with _something else_, because
52- // forward slash breaks the parsed templates.
53- const contents = template . contents . replaceAll ( '/' , PLACEHOLDER ) ;
50+ const before = code . slice ( 0 , start ) ;
51+ const spaces = code
52+ . slice ( start + prefix . length , end - suffix . length )
53+ // Replace everything except `\n` with space, so the line and column remain correct
54+ // Prettier normalized EOL to `\n`, so we don't need worry about `\r` and `\r\n`
55+ . replaceAll ( / [ ^ \n ] / g, ' ' ) ;
5456
55- const templateLength = template . range . endByte - template . range . startByte ;
56- const spaces =
57- templateLength - getBuffer ( contents ) . length - prefix . length - suffix . length ;
58-
59- return replaceContents ( code , {
60- contents : [ prefix , contents , ' ' . repeat ( spaces ) , suffix ] . join ( '' ) ,
61- range : template . range ,
62- } ) ;
57+ return before + prefix + spaces + suffix + after ;
6358}
6459
6560/** Pre-processes the template info, parsing the template content to Glimmer AST. */
0 commit comments