Skip to content

Commit e1b5da1

Browse files
committed
Fix bug where parsing template on newline caused syntax error
1 parent 9c34a49 commit e1b5da1

File tree

9 files changed

+154
-6
lines changed

9 files changed

+154
-6
lines changed

src/parse.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function desugarDefaultExportTemplates(preprocessed: string): string {
190190

191191
const lines = preprocessed.split(/\r?\n/);
192192
const desugaredLines: string[] = [];
193-
let previousLine: string | null = null;
193+
let previousLine = '';
194194
let blockLevel = 0;
195195

196196
for (let line of lines) {
@@ -202,17 +202,23 @@ function desugarDefaultExportTemplates(preprocessed: string): string {
202202
const dec = (line.match(/}/g) ?? []).length;
203203
blockLevel -= dec;
204204

205-
const previousLineIsPrettierIgnore =
206-
previousLine && squish(previousLine) === '// prettier-ignore';
205+
const squished = squish(line);
207206

208-
if (!previousLineIsPrettierIgnore && blockLevel === 0) {
207+
if (
208+
!squished.endsWith('// prettier-ignore') &&
209+
!squished.endsWith('/* prettier-ignore */') &&
210+
previousLine !== '// prettier-ignore' &&
211+
previousLine !== '/* prettier-ignore */' &&
212+
!previousLine.endsWith('=') &&
213+
blockLevel === 0
214+
) {
209215
line = line.replace(sugaredDefaultExport, desugaredDefaultExport);
210216
}
211217

212218
desugaredLines.push(line);
213219

214-
if (line.trim().length > 0) {
215-
previousLine = line;
220+
if (squished.length > 0) {
221+
previousLine = squished;
216222
}
217223
}
218224

tests/cases/gjs/mod-var.gjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33

44
const Private = <template> Private variable template. Private variable template. Private variable template. Private variable template. Private variable template. Private variable template. Private variable template. </template>
5+
/*AMBIGUOUS*/
6+
7+
const What =
8+
<template> Hi </template>
59
/*AMBIGUOUS*/

tests/unit-tests/__snapshots__/format.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ exports[`format > config > default > it formats ../cases/gjs/mod-var.gjs 1`] = `
128128
template. Private variable template. Private variable template. Private
129129
variable template. Private variable template.
130130
</template>;
131+
132+
const What = <template>Hi</template>;
131133
"
132134
`;
133135

tests/unit-tests/__snapshots__/preprocessed.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ exports[`format > config > with preprocessed code > it formats ../cases/gjs/mod-
141141
{ strictMode: true }
142142
),
143143
];
144+
145+
const What = [__GLIMMER_TEMPLATE(\`Hi\`, { strictMode: true })];
144146
"
145147
`;
146148

tests/unit-tests/ambiguous/__snapshots__/arrow-parens-avoid.test.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ exports[`ambiguous > config > arrowParens: "avoid" > (oh, no) => {} > with semi,
6161
variable template. Private variable template.
6262
</template>;
6363
(oh, no) => {};
64+
65+
const What = <template>Hi</template>;
66+
(oh, no) => {};
6467
"
6568
`;
6669

@@ -71,6 +74,9 @@ exports[`ambiguous > config > arrowParens: "avoid" > (oh, no) => {} > with semi,
7174
variable template. Private variable template.
7275
</template>;
7376
(oh, no) => {};
77+
78+
const What = <template>Hi</template>;
79+
(oh, no) => {};
7480
"
7581
`;
7682

@@ -713,6 +719,9 @@ exports[`ambiguous > config > arrowParens: "avoid" > (oops) => {} > with semi, w
713719
variable template. Private variable template.
714720
</template>;
715721
oops => {};
722+
723+
const What = <template>Hi</template>;
724+
oops => {};
716725
"
717726
`;
718727
@@ -723,6 +732,9 @@ exports[`ambiguous > config > arrowParens: "avoid" > (oops) => {} > with semi, w
723732
variable template. Private variable template.
724733
</template>;
725734
oops => {};
735+
736+
const What = <template>Hi</template>;
737+
oops => {};
726738
"
727739
`;
728740

tests/unit-tests/ambiguous/__snapshots__/index.test.ts.snap

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ exports[`ambiguous > config > default > (oh, no) => {} > with semi, with newline
6161
variable template. Private variable template.
6262
</template>;
6363
(oh, no) => {};
64+
65+
const What = <template>Hi</template>;
66+
(oh, no) => {};
6467
"
6568
`;
6669

@@ -71,6 +74,9 @@ exports[`ambiguous > config > default > (oh, no) => {} > with semi, with newline
7174
variable template. Private variable template.
7275
</template>;
7376
(oh, no) => {};
77+
78+
const What = <template>Hi</template>;
79+
(oh, no) => {};
7480
"
7581
`;
7682

@@ -713,6 +719,9 @@ exports[`ambiguous > config > default > (oops) => {} > with semi, with newline >
713719
variable template. Private variable template.
714720
</template>;
715721
(oops) => {};
722+
723+
const What = <template>Hi</template>;
724+
(oops) => {};
716725
"
717726
`;
718727

@@ -723,6 +732,9 @@ exports[`ambiguous > config > default > (oops) => {} > with semi, with newline >
723732
variable template. Private variable template.
724733
</template>;
725734
(oops) => {};
735+
736+
const What = <template>Hi</template>;
737+
(oops) => {};
726738
"
727739
`;
728740

@@ -1365,6 +1377,9 @@ exports[`ambiguous > config > default > +"oops" > with semi, with newline > it f
13651377
variable template. Private variable template.
13661378
</template>;
13671379
+\\"oops\\";
1380+
1381+
const What = <template>Hi</template>;
1382+
+\\"oops\\";
13681383
"
13691384
`;
13701385

@@ -1375,6 +1390,9 @@ exports[`ambiguous > config > default > +"oops" > with semi, with newline > it f
13751390
variable template. Private variable template.
13761391
</template>;
13771392
+\\"oops\\";
1393+
1394+
const What = <template>Hi</template>;
1395+
+\\"oops\\";
13781396
"
13791397
`;
13801398

@@ -1899,6 +1917,9 @@ exports[`ambiguous > config > default > /oops/ > with semi, with newline > it fo
18991917
variable template. Private variable template.
19001918
</template>;
19011919
/oops/;
1920+
1921+
const What = <template>Hi</template>;
1922+
/oops/;
19021923
"
19031924
`;
19041925

@@ -1909,6 +1930,9 @@ exports[`ambiguous > config > default > /oops/ > with semi, with newline > it fo
19091930
variable template. Private variable template.
19101931
</template>;
19111932
/oops/;
1933+
1934+
const What = <template>Hi</template>;
1935+
/oops/;
19121936
"
19131937
`;
19141938

@@ -2465,6 +2489,11 @@ exports[`ambiguous > config > default > <template>oops</template> > with semi, w
24652489
<template>
24662490
oops
24672491
</template>
2492+
2493+
const What = <template>Hi</template>;
2494+
<template>
2495+
oops
2496+
</template>
24682497
"
24692498
`;
24702499

@@ -2477,6 +2506,8 @@ exports[`ambiguous > config > default > <template>oops</template> > with semi, w
24772506
<template>
24782507
oops
24792508
</template>
2509+
const What = <template>Hi</template>;
2510+
<template>oops</template>;
24802511
"
24812512
`;
24822513

@@ -3080,6 +3111,11 @@ exports[`ambiguous > config > default > <template>oops</template> > without semi
30803111
<template>
30813112
oops
30823113
</template>
3114+
3115+
const What = <template>Hi</template>;
3116+
<template>
3117+
oops
3118+
</template>
30833119
"
30843120
`;
30853121

@@ -3427,6 +3463,11 @@ exports[`ambiguous > config > default > <template>oops</template> > without semi
34273463
<template>
34283464
oops
34293465
</template>
3466+
3467+
const What = <template>Hi</template>;
3468+
<template>
3469+
oops
3470+
</template>
34303471
"
34313472
`;
34323473

@@ -3707,6 +3748,9 @@ exports[`ambiguous > config > default > ["oops"] > with semi, with newline > it
37073748
variable template. Private variable template.
37083749
</template>;
37093750
[\\"oops\\"];
3751+
3752+
const What = <template>Hi</template>;
3753+
[\\"oops\\"];
37103754
"
37113755
`;
37123756

@@ -3717,6 +3761,9 @@ exports[`ambiguous > config > default > ["oops"] > with semi, with newline > it
37173761
variable template. Private variable template.
37183762
</template>;
37193763
[\\"oops\\"];
3764+
3765+
const What = <template>Hi</template>;
3766+
[\\"oops\\"];
37203767
"
37213768
`;
37223769

@@ -4611,6 +4658,9 @@ exports[`ambiguous > config > default > \`oops\` > with semi, with newline > it
46114658
variable template. Private variable template.
46124659
</template>;
46134660
\`oops\`;
4661+
4662+
const What = <template>Hi</template>;
4663+
\`oops\`;
46144664
"
46154665
`;
46164666

@@ -4621,6 +4671,9 @@ exports[`ambiguous > config > default > \`oops\` > with semi, with newline > it
46214671
variable template. Private variable template.
46224672
</template>;
46234673
\`oops\`;
4674+
4675+
const What = <template>Hi</template>;
4676+
\`oops\`;
46244677
"
46254678
`;
46264679

@@ -5354,6 +5407,9 @@ exports[`ambiguous > config > default > -"oops" > with semi, with newline > it f
53545407
variable template. Private variable template.
53555408
</template>;
53565409
-\\"oops\\";
5410+
5411+
const What = <template>Hi</template>;
5412+
-\\"oops\\";
53575413
"
53585414
`;
53595415

@@ -5364,6 +5420,9 @@ exports[`ambiguous > config > default > -"oops" > with semi, with newline > it f
53645420
variable template. Private variable template.
53655421
</template>;
53665422
-\\"oops\\";
5423+
5424+
const What = <template>Hi</template>;
5425+
-\\"oops\\";
53675426
"
53685427
`;
53695428

0 commit comments

Comments
 (0)