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

implement insertRuntimeErrors option for inline hbs #658

Merged
merged 1 commit into from
Dec 19, 2020
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
73 changes: 62 additions & 11 deletions packages/core/src/babel-plugin-inline-hbs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
TaggedTemplateExpression,
CallExpression,
isStringLiteral,
templateLiteral,
templateElement,
ExpressionStatement,
Expand All @@ -10,6 +9,8 @@ import {
Program,
functionExpression,
blockStatement,
throwStatement,
newExpression,
} from '@babel/types';
import { NodePath } from '@babel/traverse';
import { join } from 'path';
Expand Down Expand Up @@ -119,19 +120,44 @@ function handleTagged(path: NodePath<TaggedTemplateExpression>, state: State) {
}

function handleCalled(path: NodePath<CallExpression>, state: State) {
if (path.node.arguments.length !== 1) {
throw path.buildCodeFrameError('hbs accepts exactly one argument');
}
let arg = path.node.arguments[0];
if (!isStringLiteral(arg)) {
throw path.buildCodeFrameError('hbs accepts only a string literal argument');
}
let template = arg.value;
let { template, insertRuntimeErrors } = getCallArguments(path);
let compilerInstance = compiler(state);

if (state.opts.stage === 1) {
let compiled = compiler(state).applyTransforms(state.file.opts.filename, template);
let compiled: string;
try {
compiled = compilerInstance.applyTransforms(state.file.opts.filename, template);
} catch (err) {
if (insertRuntimeErrors) {
// in stage 1 we just leave the bad template in place (we were only
// trying to run transforms and re-emit hbs), so that it will be handled
// at stage3 instead.
return;
}
throw err;
}
(path.get('arguments')[0] as NodePath).replaceWith(stringLiteral(compiled));
} else {
let { compiled, dependencies } = compiler(state).precompile(state.file.opts.filename, template);
let result: ReturnType<TemplateCompiler['precompile']>;
try {
result = compilerInstance.precompile(state.file.opts.filename, template);
} catch (err) {
if (insertRuntimeErrors) {
path.replaceWith(
callExpression(
functionExpression(
null,
[],
blockStatement([throwStatement(newExpression(identifier('Error'), [stringLiteral(err.message)]))])
),
[]
)
);
return;
}
throw err;
}
let { compiled, dependencies } = result;
for (let dep of dependencies) {
state.dependencies.set(dep.runtimeName, dep);
}
Expand Down Expand Up @@ -179,3 +205,28 @@ function amdDefine(runtimeName: string, importCounter: number) {
])
);
}

function getCallArguments(path: NodePath<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(
prop =>
prop.type === 'ObjectProperty' &&
prop.computed === false &&
prop.key.type === 'Identifier' &&
prop.key.name === 'insertRuntimeErrors' &&
prop.value.type === 'BooleanLiteral' &&
prop.value.value
);

return {
template: template.value,
insertRuntimeErrors,
};
}
21 changes: 21 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('runtime errors are left in place in stage 1', () => {
let code = transform(`
import hbs from 'htmlbars-inline-precompile';
export default function() {
return hbs("<div>", { insertRuntimeErrors: true });
}
`);
expect(code).toMatch(/import hbs from 'htmlbars-inline-precompile'/);
expect(code).toMatch(/return hbs\("<div>",\s*\{\s*insertRuntimeErrors: true\s*\}\)/);
});
}

function stage3Tests(transform: (code: string) => string) {
Expand All @@ -50,6 +61,16 @@ function stage3Tests(transform: (code: string) => string) {
expect(code).not.toMatch(/import hbs from 'htmlbars-inline-precompile'/);
expect(code).toMatch(/return Ember.HTMLBars.template\({/);
});
test('runtime errors become exceptions in stage 3', () => {
let code = transform(`
import hbs from 'htmlbars-inline-precompile';
export default function() {
return hbs("<div>", { insertRuntimeErrors: true });
}
`);
expect(code).not.toMatch(/import hbs from 'htmlbars-inline-precompile'/);
expect(code).toMatch(/throw new Error\("Unclosed element `div`/);
});
}

describe('inline-hbs', () => {
Expand Down