From 5a2a5fb41130c51a8be19855cb9b59c4afa8733c Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 26 Sep 2017 07:33:18 -0700 Subject: [PATCH] Move template object creation from core into the template transform. (#6307) * Move template object creation into the template transform. * use shorthand [skip ci] --- .../src/transformation/file/index.js | 32 ++----------- .../src/index.js | 46 +++++++++++++++---- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/packages/babel-core/src/transformation/file/index.js b/packages/babel-core/src/transformation/file/index.js index d5603fdcab..6c2ab6044f 100644 --- a/packages/babel-core/src/transformation/file/index.js +++ b/packages/babel-core/src/transformation/file/index.js @@ -263,34 +263,10 @@ export default class File extends Store { return uid; } - addTemplateObject( - helperName: string, - strings: Array, - 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( diff --git a/packages/babel-plugin-transform-es2015-template-literals/src/index.js b/packages/babel-plugin-transform-es2015-template-literals/src/index.js index 1cdcc39187..7dfa42fde5 100644 --- a/packages/babel-plugin-transform-es2015-template-literals/src/index.js +++ b/packages/babel-plugin-transform-es2015-template-literals/src/index.js @@ -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) {