Skip to content

Commit 7296a9a

Browse files
committed
No-op for invalid hbs (Fixes #43)
This is similar to prettier's typical behavior for JS syntax errors
1 parent 8266927 commit 7296a9a

6 files changed

Lines changed: 63 additions & 21 deletions

File tree

src/print/index.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,35 @@ export function definePrinter(options: Options): void {
128128

129129
if (hasPrettierIgnore) {
130130
return printRawText(path, embedOptions);
131-
} else if (wasPreprocessed && isGlimmerTemplateLiteral(node)) {
132-
return printTemplateLiteral(
133-
printTemplateContent(node, textToDoc, embedOptions)
134-
);
135-
} else if (!wasPreprocessed && isGlimmerClassProperty(node)) {
136-
return printTemplateTag(
137-
printTemplateContent(node.key.arguments[0], textToDoc, embedOptions),
138-
node.extra.isDefaultTemplate ?? false
139-
);
140-
} else if (!wasPreprocessed && isGlimmerArrayExpression(node)) {
141-
return printTemplateTag(
142-
printTemplateContent(
143-
node.elements[0].arguments[0],
144-
textToDoc,
145-
embedOptions
146-
),
147-
node.extra.isDefaultTemplate ?? false
148-
);
149-
} else {
150-
// Nothing to embed, so move on to the regular printer.
151-
return null;
152131
}
132+
133+
try {
134+
if (wasPreprocessed && isGlimmerTemplateLiteral(node)) {
135+
return printTemplateLiteral(
136+
printTemplateContent(node, textToDoc, embedOptions)
137+
);
138+
} else if (!wasPreprocessed && isGlimmerClassProperty(node)) {
139+
return printTemplateTag(
140+
printTemplateContent(node.key.arguments[0], textToDoc, embedOptions),
141+
node.extra.isDefaultTemplate ?? false
142+
);
143+
} else if (!wasPreprocessed && isGlimmerArrayExpression(node)) {
144+
return printTemplateTag(
145+
printTemplateContent(
146+
node.elements[0].arguments[0],
147+
textToDoc,
148+
embedOptions
149+
),
150+
node.extra.isDefaultTemplate ?? false
151+
);
152+
}
153+
} catch (error: unknown) {
154+
console.error(error);
155+
return printRawText(path, embedOptions);
156+
}
157+
158+
// Nothing to embed, so move on to the regular printer.
159+
return null;
153160
};
154161

155162
/**
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
{{#if oops}}
3+
</template>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ exports[`format > config > default > it formats ../cases/gjs/exported-mod-var.gj
117117
"
118118
`;
119119
120+
exports[`format > config > default > it formats ../cases/gjs/invalid-template.gjs 1`] = `
121+
"<template>
122+
{{#if oops}}
123+
</template>
124+
"
125+
`;
126+
120127
exports[`format > config > default > it formats ../cases/gjs/js-only.gjs 1`] = `
121128
"const num = 1;
122129
"

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ exports[`format > config > with preprocessed code > it formats ../cases/gjs/expo
127127
"
128128
`;
129129
130+
exports[`format > config > with preprocessed code > it formats ../cases/gjs/invalid-template.gjs 1`] = `
131+
"[
132+
__GLIMMER_TEMPLATE(\`
133+
{{#if oops}}
134+
\`, {
135+
strictMode: true,
136+
}),
137+
]
138+
"
139+
`;
140+
130141
exports[`format > config > with preprocessed code > it formats ../cases/gjs/js-only.gjs 1`] = `
131142
"const num = 1;
132143
"

tests/unit-tests/config/__snapshots__/semi-false.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ exports[`config > semi: false > it formats ../cases/gjs/exported-mod-var.gjs 1`]
117117
"
118118
`;
119119
120+
exports[`config > semi: false > it formats ../cases/gjs/invalid-template.gjs 1`] = `
121+
"<template>
122+
{{#if oops}}
123+
</template>
124+
"
125+
`;
126+
120127
exports[`config > semi: false > it formats ../cases/gjs/js-only.gjs 1`] = `
121128
"const num = 1
122129
"

tests/unit-tests/config/__snapshots__/template-export-default.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ exports[`config > templateExportDefault: true > it formats ../cases/gjs/exported
117117
"
118118
`;
119119
120+
exports[`config > templateExportDefault: true > it formats ../cases/gjs/invalid-template.gjs 1`] = `
121+
"export default <template>
122+
{{#if oops}}
123+
</template>
124+
"
125+
`;
126+
120127
exports[`config > templateExportDefault: true > it formats ../cases/gjs/js-only.gjs 1`] = `
121128
"const num = 1;
122129
"

0 commit comments

Comments
 (0)