source: trip-planner-front/node_modules/@babel/preset-modules/lib/plugins/transform-tagged-template-caching/index.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.1 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6/**
7 * Converts destructured parameters with default values to non-shorthand syntax.
8 * This fixes the only Tagged Templates-related bug in ES Modules-supporting browsers (Safari 10 & 11).
9 * Use this plugin instead of `@babel/plugin-transform-template-literals` when targeting ES Modules.
10 *
11 * @example
12 * // Bug 1: Safari 10/11 doesn't reliably return the same Strings value.
13 * // The value changes depending on invocation and function optimization state.
14 * function f() { return Object`` }
15 * f() === new f() // false, should be true.
16 *
17 * @example
18 * // Bug 2: Safari 10/11 use the same cached strings value when the string parts are the same.
19 * // This behavior comes from an earlier version of the spec, and can cause tricky bugs.
20 * Object``===Object`` // true, should be false.
21 *
22 * Benchmarks: https://jsperf.com/compiled-tagged-template-performance
23 */
24var _default = ({
25 types: t
26}) => ({
27 name: "transform-tagged-template-caching",
28 visitor: {
29 TaggedTemplateExpression(path, state) {
30 // tagged templates we've already dealt with
31 let processed = state.get("processed");
32
33 if (!processed) {
34 processed = new Map();
35 state.set("processed", processed);
36 }
37
38 if (processed.has(path.node)) return path.skip(); // Grab the expressions from the original tag.
39 // tag`a${'hello'}` // ['hello']
40
41 const expressions = path.node.quasi.expressions; // Create an identity function helper:
42 // identity = t => t
43
44 let identity = state.get("identity");
45
46 if (!identity) {
47 identity = path.scope.getProgramParent().generateDeclaredUidIdentifier("_");
48 state.set("identity", identity);
49 const binding = path.scope.getBinding(identity.name);
50 binding.path.get("init").replaceWith(t.arrowFunctionExpression( // re-use the helper identifier for compressability
51 [t.identifier("t")], t.identifier("t")));
52 } // Use the identity function helper to get a reference to the template's Strings.
53 // We replace all expressions with `0` ensure Strings has the same shape.
54 // identity`a${0}`
55
56
57 const template = t.taggedTemplateExpression(identity, t.templateLiteral(path.node.quasi.quasis, expressions.map(() => t.numericLiteral(0))));
58 processed.set(template, true); // Install an inline cache at the callsite using the global variable:
59 // _t || (_t = identity`a${0}`)
60
61 const ident = path.scope.getProgramParent().generateDeclaredUidIdentifier("t");
62 path.scope.getBinding(ident.name).path.parent.kind = "let";
63 const inlineCache = t.logicalExpression("||", ident, t.assignmentExpression("=", ident, template)); // The original tag function becomes a plain function call.
64 // The expressions omitted from the cached Strings tag are directly applied as arguments.
65 // tag(_t || (_t = Object`a${0}`), 'hello')
66
67 const node = t.callExpression(path.node.tag, [inlineCache, ...expressions]);
68 path.replaceWith(node);
69 }
70
71 }
72});
73
74exports.default = _default;
75module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.