Skip to content

Commit

Permalink
updating to support new test added in #899
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Jul 29, 2021
1 parent 68721e8 commit ea3abe7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 12 additions & 5 deletions packages/core/src/babel-plugin-stage1-inline-hbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ export default function make<Opts>(getCompiler: (opts: Opts) => TemplateCompiler
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 @@ -107,10 +103,21 @@ export default function make<Opts>(getCompiler: (opts: Opts) => TemplateCompiler
);

return {
template: template.value,
template: getTemplateString(template, path),
insertRuntimeErrors,
};
}

return stage1InlineHBSTransform;
}

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');
}
2 changes: 1 addition & 1 deletion packages/core/tests/inline-hbs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function stage3Tests(transform: (code: string) => string) {
`);
expect(code).not.toMatch(/import hbs from 'htmlbars-inline-precompile'/);
expect(code).toMatch(/import { createTemplateFactory } from ['"]@ember\/template-factory['"]/);
expect(code).toMatch(/return createTemplateFactory\({/);
expect(code).toMatch(/return createTemplateFactory\(/);
});
test('runtime errors become exceptions in stage 3', () => {
let code = transform(`
Expand Down

0 comments on commit ea3abe7

Please sign in to comment.