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

[FEAT] Add shouldParseScope, disableTemplateTag, disableFunctionCall options #333

Merged
merged 1 commit into from
Feb 22, 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
153 changes: 153 additions & 0 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,95 @@ describe('htmlbars-inline-precompile', function () {
expect(transformed).toEqual(expected, 'tagged template is replaced');
});

it('works with multiple imports from different modules', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},

modules: {
'ember-cli-htmlbars': 'hbs',
'@ember/template-compilation': {
export: 'precompileTemplate',
},
},
},
],
];

let transformed = transform(`
import { hbs } from 'ember-cli-htmlbars';
import { precompileTemplate } from '@ember/template-compilation';
let a = hbs\`hello\`;
let b = precompileTemplate('hello');
`);

let expected = `let a = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");\nlet b = Ember.HTMLBars.template(\n/*\n hello\n*/\n"precompiled(hello)");`;

expect(transformed).toEqual(expected, 'tagged template is replaced');
});

it('can disable template literal usage', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},

modules: {
'@ember/template-compilation': {
export: 'precompileTemplate',
disableTemplateLiteral: true,
},
},
},
],
];

expect(() => {
transform(`
import { precompileTemplate } from '@ember/template-compilation';
let a = precompileTemplate\`hello\`;
`);
}).toThrow(
/Attempted to use `precompileTemplate` as a template tag, but it can only be called as a function with a string passed to it:/
);
});

it('can disable function call usage', function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},

modules: {
'ember-template-imports': {
export: 'hbs',
disableFunctionCall: true,
},
},
},
],
];

expect(() => {
transform(`
import { hbs } from 'ember-template-imports';
let a = hbs(\`hello\`);
`);
}).toThrow(
/Attempted to use `hbs` as a function call, but it can only be used as a template tag:/
);
});

it('works properly when used along with modules transform', function () {
plugins.push([TransformModules]);
let transformed = transform(
Expand Down Expand Up @@ -527,4 +616,68 @@ describe('htmlbars-inline-precompile', function () {
`);
});
});

describe('with transformScope: true', function () {
beforeEach(() => {
plugins = [
[
HTMLBarsInlinePrecompile,
{
precompile() {
return precompile.apply(this, arguments);
},

modules: {
'@ember/template-compilation': {
export: 'precompileTemplate',
shouldParseScope: true,
},
},
},
],
];
});

it('correctly handles scope', function () {
let source = 'hello';
transform(
`import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('${source}', { scope: { foo, bar } });`
);

expect(optionsReceived).toEqual({
contents: source,
scope: ['foo', 'bar'],
});
});

it('errors if scope contains mismatched keys/values', function () {
expect(() => {
transform(
"import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('hello', { scope: { foo: bar } });"
pzuraq marked this conversation as resolved.
Show resolved Hide resolved
);
}).toThrow(
/Scope objects for `precompileTemplate` may only contain direct references to in-scope values, e.g. { foo } or { foo: foo }/
);
});

it('errors if scope is not an object', function () {
expect(() => {
transform(
"import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('hello', { scope: ['foo', 'bar'] });"
);
}).toThrow(
/Scope objects for `precompileTemplate` must be an object expression containing only references to in-scope values/
);
});

it('errors if scope contains any non-reference values', function () {
expect(() => {
transform(
"import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('hello', { scope: { foo, bar: 123 } });"
);
}).toThrow(
/Scope objects for `precompileTemplate` may only contain direct references to in-scope values, e.g. { bar } or { bar: bar }/
);
});
});
});
Loading