Skip to content

Commit b1ad807

Browse files
committed
Improve preprocess
1 parent af77c81 commit b1ad807

File tree

3 files changed

+22
-43
lines changed

3 files changed

+22
-43
lines changed

src/parse/preprocess.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
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. */

src/utils/content-tag.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,6 @@ export function parse(
2828
return preprocessor.parse(file, options);
2929
}
3030

31-
export function replaceContents(
32-
file: string,
33-
options: {
34-
contents: string;
35-
range: Range;
36-
},
37-
): string {
38-
const { contents, range } = options;
39-
40-
return [
41-
sliceByteRange(file, 0, range.startByte),
42-
contents,
43-
sliceByteRange(file, range.endByte),
44-
].join('');
45-
}
46-
4731
export function sliceByteRange(
4832
string_: string,
4933
indexStart: number,

tests/unit-tests/preprocess.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,40 @@ import {
88
const TEST_CASES = [
99
{
1010
code: '<template>hi</template>',
11-
expected: [`{/*hi */}`],
11+
expected: [`{/* */}`],
1212
},
1313
{
1414
code: '<template>/* hi */</template>',
15-
expected: [`{/*~* hi *~ */}`],
15+
expected: [`{/* */}`],
1616
},
1717
{
1818
code: '<template><div>hi</div></template>',
19-
expected: [`{/*<div>hi<~div> */}`],
19+
expected: [`{/* */}`],
2020
},
2121
{
2222
code: '<template>{{#if true}}hi{{/if}}</template>',
23-
expected: [`{/*{{#if true}}hi{{~if}} */}`],
23+
expected: [`{/* */}`],
2424
},
2525
{
26-
code: '<template>////////////////</template>',
27-
expected: [`{/*~~~~~~~~~~~~~~~~ */}`],
26+
code: '<template>////////\n////////</template>',
27+
expected: [`{/* \n */}`],
2828
},
2929
{
3030
code: '<template>💩</template>',
31-
expected: [`{/*💩 */}`],
31+
expected: [`{/* */}`],
3232
},
3333
{
3434
code: 'const a = <template>foo</template>; const b = <template>bar</template>;',
3535
expected: [
36-
`const a = {/*foo */}; const b = <template>bar</template>;`,
37-
`const a = <template>foo</template>; const b = {/*bar */};`,
36+
`const a = {/* */}; const b = <template>bar</template>;`,
37+
`const a = <template>foo</template>; const b = {/* */};`,
3838
],
3939
},
4040
{
4141
code: `const a = <template>💩💩💩💩💩💩💩</template>; const b = <template>💩</template>`,
4242
expected: [
43-
`const a = {/*💩💩💩💩💩💩💩 */}; const b = <template>💩</template>`,
44-
`const a = <template>💩💩💩💩💩💩💩</template>; const b = {/*💩 */}`,
43+
`const a = {/* */}; const b = <template>💩</template>`,
44+
`const a = <template>💩💩💩💩💩💩💩</template>; const b = {/* */}`,
4545
],
4646
},
4747
];

0 commit comments

Comments
 (0)