Skip to content

Commit 4527737

Browse files
authored
Merge pull request #412 from fisker/preprocess
Improve preprocess
2 parents af77c81 + 2ba22ef commit 4527737

File tree

3 files changed

+23
-48
lines changed

3 files changed

+23
-48
lines changed

src/parse/preprocess.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
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

93
export 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. */

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)