Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

[WIP] precompileTemplate with scope that has properties with different key and value #486

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 14 additions & 7 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,14 +920,21 @@ describe('htmlbars-inline-precompile', function () {
});
});

it('errors if scope contains mismatched keys/values', function () {
expect(() => {
transform(
"import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('hello', { scope: { foo: bar } });"
);
}).toThrow(
/Scope objects for `precompileTemplate` may only contain direct references to in-scope values, e.g. { foo } or { foo: foo }/
it('correctly handles scope function (object with different key value)', function () {
const source = '<Foo />';
transform(
`import { precompileTemplate } from '@ember/template-compilation';
import Foo$1 from 'foo';
var compiled = precompileTemplate('${source}', {
scope() { return {
Foo: Foo$1,
};
}});`
);
expect(optionsReceived).toEqual({
contents: source,
locals: ['Foo'],
});
});

it('errors if scope is not an object', function () {
Expand Down
33 changes: 22 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ module.exports = function (babel) {
);
}

return objExpression.properties.map((prop) => {
return objExpression.properties.reduce((res, prop) => {
let { key, value } = prop;

if (value.type !== 'Identifier' || value.name !== key.name) {
if (value.type !== 'Identifier') {
throw buildError(
`Scope objects for \`${name}\` may only contain direct references to in-scope values, e.g. { ${key.name} } or { ${key.name}: ${key.name} }`
);
}

return key.name;
});
res[key.name] = value.name;
return res;
}, {});
}

function parseObjectExpression(state, buildError, name, node, shouldParseScope = false) {
Expand All @@ -109,6 +110,10 @@ module.exports = function (babel) {
function compileTemplate(precompile, template, templateCompilerIdentifier, _options) {
let options = Object.assign({ contents: template }, _options);

if (_options.locals) {
options.locals = Object.keys(_options.locals);
}

let precompileResultString;

if (options.insertRuntimeErrors) {
Expand All @@ -121,10 +126,16 @@ module.exports = function (babel) {
precompileResultString = precompile(template, options);
}

let precompileResultAST = babel.parse(`var precompileResult = ${precompileResultString};`, {
babelrc: false,
configFile: false,
});
const keys = Object.keys(_options.locals ?? {}).join(',');
const values = Object.values(_options.locals ?? {}).join(',');

let precompileResultAST = babel.parse(
`var precompileResult = ((${keys})=>(${precompileResultString}))(${values}); `,
{
babelrc: false,
configFile: false,
}
);

let templateExpression = precompileResultAST.program.body[0].declarations[0].init;

Expand All @@ -139,17 +150,17 @@ module.exports = function (babel) {
}

function getScope(scope) {
let names = [];
let properties = {};

while (scope) {
for (let binding in scope.bindings) {
names.push(binding);
properties[binding] = binding;
}

scope = scope.parent;
}

return names;
return properties;
}

function shouldUseAutomaticScope(options) {
Expand Down