From 29eafbbf44684deeba0cd81516ec1adb16899e4a Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Fri, 13 Apr 2018 11:51:00 -0400 Subject: [PATCH] Remove tagged template literal global caching (#7722) * Remove tagged template literal global caching Fixes #7350. * Review comments * assert output --- .../src/index.js | 51 +++++++------------ .../fixtures/default/cache-revision/exec.js | 15 ++++++ .../fixtures/default/cache-revision/input.js | 15 ++++++ .../fixtures/default/cache-revision/output.js | 20 ++++++++ .../default/template-revision/output.js | 5 +- .../loose/template-revision/output.js | 5 +- 6 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/exec.js create mode 100644 packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/input.js create mode 100644 packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js diff --git a/packages/babel-plugin-transform-template-literals/src/index.js b/packages/babel-plugin-transform-template-literals/src/index.js index 1ac0b165f9..39f201f833 100644 --- a/packages/babel-plugin-transform-template-literals/src/index.js +++ b/packages/babel-plugin-transform-template-literals/src/index.js @@ -45,9 +45,6 @@ export default declare((api, options) => { } return { - pre() { - this.templates = new Map(); - }, visitor: { TaggedTemplateExpression(path) { const { node } = path; @@ -75,41 +72,27 @@ export default declare((api, options) => { } } - // 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}`; + const scope = path.scope.getProgramParent(); + const templateObject = scope.generateUidIdentifier("templateObject"); - let templateObject = this.templates.get(name); - if (templateObject) { - templateObject = t.cloneNode(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 callExpressionInput = [t.arrayExpression(strings)]; - const helperId = this.addHelper(helperName); - const callExpressionInput = []; - callExpressionInput.push(t.arrayExpression(strings)); - - if (!isStringsRawEqual) { - callExpressionInput.push(t.arrayExpression(raws)); - } - - // only add raw arrayExpression if there is any difference between raws and strings - const init = t.callExpression(helperId, callExpressionInput); - annotateAsPure(init); - 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, - }); + // only add raw arrayExpression if there is any difference between raws and strings + if (!isStringsRawEqual) { + callExpressionInput.push(t.arrayExpression(raws)); } + const init = t.callExpression(helperId, callExpressionInput); + annotateAsPure(init); + init._compact = true; + 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, [ t.cloneNode(templateObject), diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/exec.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/exec.js new file mode 100644 index 0000000000..31310013db --- /dev/null +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/exec.js @@ -0,0 +1,15 @@ +var tag = v => v; + +function foo() { + return tag`some template`; +} +function bar() { + return tag`some template`; +} +expect(foo()).toBe(foo()); +expect(foo()).toEqual(["some template"]); + +expect(bar()).toBe(bar()); +expect(bar()).toEqual(["some template"]); + +expect(bar()).not.toBe(foo()); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/input.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/input.js new file mode 100644 index 0000000000..31310013db --- /dev/null +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/input.js @@ -0,0 +1,15 @@ +var tag = v => v; + +function foo() { + return tag`some template`; +} +function bar() { + return tag`some template`; +} +expect(foo()).toBe(foo()); +expect(foo()).toEqual(["some template"]); + +expect(bar()).toBe(bar()); +expect(bar()).toEqual(["some template"]); + +expect(bar()).not.toBe(foo()); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js new file mode 100644 index 0000000000..8a02e8fcba --- /dev/null +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/cache-revision/output.js @@ -0,0 +1,20 @@ +var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral(["some template"]), + _templateObject2 = /*#__PURE__*/ _taggedTemplateLiteral(["some template"]); + +function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } + +var tag = v => v; + +function foo() { + return tag(_templateObject); +} + +function bar() { + return tag(_templateObject2); +} + +expect(foo()).toBe(foo()); +expect(foo()).toEqual(["some template"]); +expect(bar()).toBe(bar()); +expect(bar()).toEqual(["some template"]); +expect(bar()).not.toBe(foo()); diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/default/template-revision/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/default/template-revision/output.js index 2f8b82de8f..2a4f3bff53 100644 --- a/packages/babel-plugin-transform-template-literals/test/fixtures/default/template-revision/output.js +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/default/template-revision/output.js @@ -4,7 +4,8 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteral([void 0], ["\\unicode _templateObject4 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0], ["left", "\\xg"]), _templateObject5 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\xg", "right"]), _templateObject6 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u000g", "right"]), - _templateObject7 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "right"]); + _templateObject7 = /*#__PURE__*/ _taggedTemplateLiteral(["left", void 0, "right"], ["left", "\\u{-0}", "right"]), + _templateObject8 = /*#__PURE__*/ _taggedTemplateLiteral([void 0], ["\\01"]); function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } @@ -18,5 +19,5 @@ tag(_templateObject7, 0, 1); function a() { var undefined = 4; - tag(_templateObject2); + tag(_templateObject8); } diff --git a/packages/babel-plugin-transform-template-literals/test/fixtures/loose/template-revision/output.js b/packages/babel-plugin-transform-template-literals/test/fixtures/loose/template-revision/output.js index 9eee99e306..83ecc60448 100644 --- a/packages/babel-plugin-transform-template-literals/test/fixtures/loose/template-revision/output.js +++ b/packages/babel-plugin-transform-template-literals/test/fixtures/loose/template-revision/output.js @@ -4,7 +4,8 @@ var _templateObject = /*#__PURE__*/ _taggedTemplateLiteralLoose([void 0], ["\\un _templateObject4 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0], ["left", "\\xg"]), _templateObject5 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\xg", "right"]), _templateObject6 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u000g", "right"]), - _templateObject7 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u{-0}", "right"]); + _templateObject7 = /*#__PURE__*/ _taggedTemplateLiteralLoose(["left", void 0, "right"], ["left", "\\u{-0}", "right"]), + _templateObject8 = /*#__PURE__*/ _taggedTemplateLiteralLoose([void 0], ["\\01"]); function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; } @@ -18,5 +19,5 @@ tag(_templateObject7, 0, 1); function a() { var undefined = 4; - tag(_templateObject2); + tag(_templateObject8); }