Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support inert TemplateLiteral in hbs plugin #899

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/core/src/babel-plugin-inline-hbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,20 @@ export default function make(getCompiler: (opts: any) => TemplateCompiler) {
);
}

function getTemplateString(template: any, path: NodePath<t.CallExpression>): string {
if (template?.type === 'StringLiteral') {
return template.value;
}
// treat inert TemplateLiteral (without subexpressions) like a StringLiteral
if (template?.type === 'TemplateLiteral' && !template.expressions.length) {
return template.quasis[0].value.cooked;
}
throw path.buildCodeFrameError('hbs accepts only a string literal argument');
}

function getCallArguments(path: NodePath<t.CallExpression>): { template: string; insertRuntimeErrors: boolean } {
let [template, options] = path.node.arguments;

if (template?.type !== 'StringLiteral') {
throw path.buildCodeFrameError('hbs accepts only a string literal argument');
}

let insertRuntimeErrors =
options?.type === 'ObjectExpression' &&
options.properties.some(
Expand All @@ -225,7 +232,7 @@ export default function make(getCompiler: (opts: any) => TemplateCompiler) {
);

return {
template: template.value,
template: getTemplateString(template, path),
insertRuntimeErrors,
};
}
Expand Down
22 changes: 22 additions & 0 deletions packages/core/tests/inline-hbs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ function stage1Tests(transform: (code: string) => string) {
expect(code).toMatch(/return hbs\("<div/);
expect(code).toMatch(/embroider-sample-transforms-result/);
});
test('call form with template literal', () => {
let code = transform(`
import hbs from 'htmlbars-inline-precompile';
export default function() {
return hbs(\`<div class={{embroider-sample-transforms-target}}></div>\`);
}
`);
expect(code).toMatch(/import hbs from 'htmlbars-inline-precompile'/);
expect(code).toMatch(/return hbs\("<div/);
expect(code).toMatch(/embroider-sample-transforms-result/);
});

test('runtime errors are left in place in stage 1', () => {
let code = transform(`
Expand Down Expand Up @@ -63,6 +74,17 @@ function stage3Tests(transform: (code: string) => string) {
expect(code).toMatch(/import { createTemplateFactory } from ['"]@ember\/template-factory['"]/);
expect(code).toMatch(/return createTemplateFactory\({/);
});
test('call form with template literal', () => {
let code = transform(`
import hbs from 'htmlbars-inline-precompile';
export default function() {
return hbs(\`<div class={{embroider-sample-transforms-target}}></div>\`);
}
`);
expect(code).not.toMatch(/import hbs from 'htmlbars-inline-precompile'/);
expect(code).toMatch(/import { createTemplateFactory } from ['"]@ember\/template-factory['"]/);
expect(code).toMatch(/return createTemplateFactory\({/);
});
test('runtime errors become exceptions in stage 3', () => {
let code = transform(`
import hbs from 'htmlbars-inline-precompile';
Expand Down