1- import {
2- getBuffer ,
3- parse ,
4- type Range ,
5- replaceContents ,
6- sliceByteRange ,
7- } from '../utils/content-tag.js' ;
1+ import { parse , type Range , sliceByteRange } from '../utils/content-tag.js' ;
82
93export interface Template {
104 contentRange : Range ;
@@ -17,8 +11,6 @@ export interface Template {
1711 } ;
1812}
1913
20- const PLACEHOLDER = '~' ;
21-
2214/**
2315 * Replace the template with a parsable placeholder that takes up the same
2416 * range.
@@ -27,6 +19,9 @@ export function preprocessTemplateRange(
2719 template : Template ,
2820 code : string ,
2921) : string {
22+ const { start, end } = template . utf16Range ;
23+ const after = code . slice ( end ) ;
24+
3025 let prefix : string ;
3126 let suffix : string ;
3227
@@ -39,7 +34,7 @@ export function preprocessTemplateRange(
3934 prefix = '{/*' ;
4035 suffix = '*/}' ;
4136
42- const nextToken = sliceByteRange ( code , template . range . endByte ) . match ( / \S + / ) ;
37+ const nextToken = after . match ( / \S + / ) ;
4338
4439 if ( nextToken && ( nextToken [ 0 ] === 'as' || nextToken [ 0 ] === 'satisfies' ) ) {
4540 // Replace with parenthesized ObjectExpression
@@ -48,18 +43,14 @@ export function preprocessTemplateRange(
4843 }
4944 }
5045
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 ) ;
46+ const before = code . slice ( 0 , start ) ;
47+ const spaces = code
48+ . slice ( start + prefix . length , end - suffix . length )
49+ // Replace everything except `\n` with space, so the line and column remain correct
50+ // Prettier normalized EOL to `\n`, so we don't need worry about `\r` and `\r\n`
51+ . replaceAll ( / [ ^ \n ] / g, ' ' ) ;
5452
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- } ) ;
53+ return before + prefix + spaces + suffix + after ;
6354}
6455
6556/** Pre-processes the template info, parsing the template content to Glimmer AST. */
0 commit comments