Move template object creation from core into the template transform. (#6307)
* Move template object creation into the template transform. * use shorthand [skip ci]
This commit is contained in:
parent
0379060f8a
commit
5a2a5fb411
@ -263,34 +263,10 @@ export default class File extends Store {
|
||||
return uid;
|
||||
}
|
||||
|
||||
addTemplateObject(
|
||||
helperName: string,
|
||||
strings: Array<Object>,
|
||||
raw: Object,
|
||||
): Object {
|
||||
// Generate a unique name based on the string literals so we dedupe
|
||||
// identical strings used in the program.
|
||||
const stringIds = raw.elements.map(function(string) {
|
||||
return string.value;
|
||||
});
|
||||
const name = `${helperName}_${raw.elements.length}_${stringIds.join(",")}`;
|
||||
|
||||
const declar = this.declarations[name];
|
||||
if (declar) return declar;
|
||||
|
||||
const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
|
||||
"templateObject",
|
||||
));
|
||||
|
||||
const helperId = this.addHelper(helperName);
|
||||
const init = t.callExpression(helperId, [strings, raw]);
|
||||
init._compact = true;
|
||||
this.scope.push({
|
||||
id: uid,
|
||||
init: init,
|
||||
_blockHoist: 1.9, // This ensures that we don't fail if not using function expression helpers
|
||||
});
|
||||
return uid;
|
||||
addTemplateObject() {
|
||||
throw new Error(
|
||||
"This function has been moved into the template literal transform itself.",
|
||||
);
|
||||
}
|
||||
|
||||
buildCodeFrameError(
|
||||
|
||||
@ -35,6 +35,9 @@ export default function({ types: t }) {
|
||||
}
|
||||
|
||||
return {
|
||||
pre() {
|
||||
this.templates = new Map();
|
||||
},
|
||||
visitor: {
|
||||
TaggedTemplateExpression(path, state) {
|
||||
const { node } = path;
|
||||
@ -54,18 +57,41 @@ export default function({ types: t }) {
|
||||
raws.push(t.stringLiteral(raw));
|
||||
}
|
||||
|
||||
let templateName = "taggedTemplateLiteral";
|
||||
if (state.opts.loose) templateName += "Loose";
|
||||
let helperName = "taggedTemplateLiteral";
|
||||
if (state.opts.loose) helperName += "Loose";
|
||||
|
||||
const templateObject = state.file.addTemplateObject(
|
||||
templateName,
|
||||
t.arrayExpression(strings),
|
||||
t.arrayExpression(raws),
|
||||
// Generate a unique name based on the string literals so we dedupe
|
||||
// identical strings used in the program.
|
||||
const rawParts = raws.map(s => s.value).join(",");
|
||||
const name = `${helperName}_${raws.length}_${rawParts}`;
|
||||
|
||||
let templateObject = this.templates.get(name);
|
||||
if (templateObject) {
|
||||
templateObject = t.clone(templateObject);
|
||||
} else {
|
||||
const programPath = path.find(p => p.isProgram());
|
||||
templateObject = programPath.scope.generateUidIdentifier(
|
||||
"templateObject",
|
||||
);
|
||||
this.templates.set(name, templateObject);
|
||||
|
||||
const helperId = this.addHelper(helperName);
|
||||
const init = t.callExpression(helperId, [
|
||||
t.arrayExpression(strings),
|
||||
t.arrayExpression(raws),
|
||||
]);
|
||||
init._compact = true;
|
||||
programPath.scope.push({
|
||||
id: templateObject,
|
||||
init,
|
||||
// This ensures that we don't fail if not using function expression helpers
|
||||
_blockHoist: 1.9,
|
||||
});
|
||||
}
|
||||
|
||||
path.replaceWith(
|
||||
t.callExpression(node.tag, [templateObject, ...quasi.expressions]),
|
||||
);
|
||||
|
||||
const args = [templateObject].concat(quasi.expressions);
|
||||
|
||||
path.replaceWith(t.callExpression(node.tag, args));
|
||||
},
|
||||
|
||||
TemplateLiteral(path, state) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user