Skip to content

Commit 0a9f541

Browse files
authored
Merge pull request #899 from eoneill/master
support inert TemplateLiteral in hbs plugin
2 parents c3d2629 + ba5c3f6 commit 0a9f541

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

packages/core/src/babel-plugin-inline-hbs.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,20 @@ export default function make(getCompiler: (opts: any) => TemplateCompiler) {
205205
);
206206
}
207207

208+
function getTemplateString(template: any, path: NodePath<t.CallExpression>): string {
209+
if (template?.type === 'StringLiteral') {
210+
return template.value;
211+
}
212+
// treat inert TemplateLiteral (without subexpressions) like a StringLiteral
213+
if (template?.type === 'TemplateLiteral' && !template.expressions.length) {
214+
return template.quasis[0].value.cooked;
215+
}
216+
throw path.buildCodeFrameError('hbs accepts only a string literal argument');
217+
}
218+
208219
function getCallArguments(path: NodePath<t.CallExpression>): { template: string; insertRuntimeErrors: boolean } {
209220
let [template, options] = path.node.arguments;
210221

211-
if (template?.type !== 'StringLiteral') {
212-
throw path.buildCodeFrameError('hbs accepts only a string literal argument');
213-
}
214-
215222
let insertRuntimeErrors =
216223
options?.type === 'ObjectExpression' &&
217224
options.properties.some(
@@ -225,7 +232,7 @@ export default function make(getCompiler: (opts: any) => TemplateCompiler) {
225232
);
226233

227234
return {
228-
template: template.value,
235+
template: getTemplateString(template, path),
229236
insertRuntimeErrors,
230237
};
231238
}

packages/core/tests/inline-hbs.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ function stage1Tests(transform: (code: string) => string) {
2727
expect(code).toMatch(/return hbs\("<div/);
2828
expect(code).toMatch(/embroider-sample-transforms-result/);
2929
});
30+
test('call form with template literal', () => {
31+
let code = transform(`
32+
import hbs from 'htmlbars-inline-precompile';
33+
export default function() {
34+
return hbs(\`<div class={{embroider-sample-transforms-target}}></div>\`);
35+
}
36+
`);
37+
expect(code).toMatch(/import hbs from 'htmlbars-inline-precompile'/);
38+
expect(code).toMatch(/return hbs\("<div/);
39+
expect(code).toMatch(/embroider-sample-transforms-result/);
40+
});
3041

3142
test('runtime errors are left in place in stage 1', () => {
3243
let code = transform(`
@@ -63,6 +74,17 @@ function stage3Tests(transform: (code: string) => string) {
6374
expect(code).toMatch(/import { createTemplateFactory } from ['"]@ember\/template-factory['"]/);
6475
expect(code).toMatch(/return createTemplateFactory\({/);
6576
});
77+
test('call form with template literal', () => {
78+
let code = transform(`
79+
import hbs from 'htmlbars-inline-precompile';
80+
export default function() {
81+
return hbs(\`<div class={{embroider-sample-transforms-target}}></div>\`);
82+
}
83+
`);
84+
expect(code).not.toMatch(/import hbs from 'htmlbars-inline-precompile'/);
85+
expect(code).toMatch(/import { createTemplateFactory } from ['"]@ember\/template-factory['"]/);
86+
expect(code).toMatch(/return createTemplateFactory\({/);
87+
});
6688
test('runtime errors become exceptions in stage 3', () => {
6789
let code = transform(`
6890
import hbs from 'htmlbars-inline-precompile';

0 commit comments

Comments
 (0)