From ba5c3f6b573d12b4996c818d6771a0f43a11f596 Mon Sep 17 00:00:00 2001 From: Eugene ONeill Date: Tue, 20 Jul 2021 00:47:01 -0700 Subject: [PATCH] support inert TemplateLiteral in hbs plugin --- packages/core/src/babel-plugin-inline-hbs.ts | 17 ++++++++++----- packages/core/tests/inline-hbs.test.ts | 22 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/core/src/babel-plugin-inline-hbs.ts b/packages/core/src/babel-plugin-inline-hbs.ts index 9af69e494..f114a141e 100644 --- a/packages/core/src/babel-plugin-inline-hbs.ts +++ b/packages/core/src/babel-plugin-inline-hbs.ts @@ -205,13 +205,20 @@ export default function make(getCompiler: (opts: any) => TemplateCompiler) { ); } + function getTemplateString(template: any, path: NodePath): 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): { 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( @@ -225,7 +232,7 @@ export default function make(getCompiler: (opts: any) => TemplateCompiler) { ); return { - template: template.value, + template: getTemplateString(template, path), insertRuntimeErrors, }; } diff --git a/packages/core/tests/inline-hbs.test.ts b/packages/core/tests/inline-hbs.test.ts index 1a2324bc7..9eb7e2b71 100644 --- a/packages/core/tests/inline-hbs.test.ts +++ b/packages/core/tests/inline-hbs.test.ts @@ -27,6 +27,17 @@ function stage1Tests(transform: (code: string) => string) { expect(code).toMatch(/return hbs\("
{ + let code = transform(` + import hbs from 'htmlbars-inline-precompile'; + export default function() { + return hbs(\`
\`); + } + `); + expect(code).toMatch(/import hbs from 'htmlbars-inline-precompile'/); + expect(code).toMatch(/return hbs\("
{ let code = transform(` @@ -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(\`
\`); + } + `); + 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';