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

Add support for hbs(...) #215

Merged
merged 1 commit into from
May 6, 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
23 changes: 23 additions & 0 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ describe('htmlbars-inline-precompile', function () {
});
});

it('allows a template string literal when used as a call expression', function () {
let source = 'hello';
transform(`import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs(\`${source}\`);`);

expect(optionsReceived).toEqual({
contents: source,
});
});

it('errors when the template string contains placeholders', function () {
expect(() =>
transform(
"import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs(`string ${value}`)"
)
).toThrow(/placeholders inside a template string are not supported/);
});

it('errors when the template string is tagged', function () {
expect(() =>
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs(hbs`string`)")
).toThrow(/tagged template strings inside hbs are not supported/);
});

it('allows static userland options when used as a call expression', function () {
let source = 'hello';
transform(
Expand Down
39 changes: 26 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,44 @@ module.exports = function (babel) {
return;
}

let options;
let args = path.node.arguments;

let template = path.node.arguments[0];
if (template === undefined || typeof template.value !== 'string') {
throw path.buildCodeFrameError(
'hbs should be invoked with at least a single argument: the template string'
);
}
let template;

switch (path.node.arguments.length) {
case 0:
switch (args[0] && args[0].type) {
case 'StringLiteral':
template = args[0].value;
break;
case 'TemplateLiteral':
if (args[0].expressions.length) {
throw path.buildCodeFrameError(
'placeholders inside a template string are not supported'
);
} else {
template = args[0].quasis.map((quasi) => quasi.value.cooked).join('');
}
break;
case 'TaggedTemplateExpression':
throw path.buildCodeFrameError('tagged template strings inside hbs are not supported');
default:
throw path.buildCodeFrameError(
'hbs should be invoked with at least a single argument: the template string'
);
}

let options;

switch (args.length) {
case 1:
break;
case 2: {
let astOptions = path.node.arguments[1];
if (astOptions.type !== 'ObjectExpression') {
if (args[1].type !== 'ObjectExpression') {
throw path.buildCodeFrameError(
'hbs can only be invoked with 2 arguments: the template string, and any static options'
);
}

options = parseObjectExpression(path.buildCodeFrameError.bind(path), astOptions);
options = parseObjectExpression(path.buildCodeFrameError.bind(path), args[1]);

break;
}
Expand All @@ -240,7 +253,7 @@ module.exports = function (babel) {

let { precompile } = state.opts;

path.replaceWith(compileTemplate(precompile, template.value, options));
path.replaceWith(compileTemplate(precompile, template, options));
},
},
};
Expand Down