UNPKG

3.47 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.PreludeGenerator = void 0;
7
8var t = _interopRequireWildcard(require("@babel/types"));
9
10var _babelhelpers = require("./babelhelpers.js");
11
12var _NameGenerator = require("./NameGenerator.js");
13
14function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
15
16/**
17 * Copyright (c) 2017-present, Facebook, Inc.
18 * All rights reserved.
19 *
20 * This source code is licensed under the BSD-style license found in the
21 * LICENSE file in the root directory of this source tree. An additional grant
22 * of patent rights can be found in the PATENTS file in the same directory.
23 */
24class PreludeGenerator {
25 constructor(debugNames, uniqueSuffix) {
26 this.prelude = [];
27 this.memoizedRefs = new Map();
28 this.nameGenerator = new _NameGenerator.NameGenerator(new Set(), !!debugNames, uniqueSuffix || "", "_$");
29 this.usesThis = false;
30 this.declaredGlobals = new Set();
31 this.nextInvariantId = 0;
32 }
33
34 createNameGenerator(prefix) {
35 return new _NameGenerator.NameGenerator(this.nameGenerator.forbiddenNames, this.nameGenerator.debugNames, this.nameGenerator.uniqueSuffix, prefix);
36 }
37
38 convertStringToMember(str) {
39 return str.split(".").map(name => {
40 if (name === "global") {
41 return this.memoizeReference(name);
42 } else if (name === "this") {
43 return t.thisExpression();
44 } else {
45 return t.identifier(name);
46 }
47 }).reduce((obj, prop) => t.memberExpression(obj, prop));
48 }
49
50 globalReference(key, globalScope = false) {
51 if (globalScope && t.isValidIdentifier(key)) return t.identifier(key);
52 return (0, _babelhelpers.memberExpressionHelper)(this.memoizeReference("global"), key);
53 }
54
55 memoizeReference(key) {
56 let ref = this.memoizedRefs.get(key);
57 if (ref) return ref;
58 let init;
59
60 if (key.includes("(") || key.includes("[")) {
61 // Horrible but effective hack:
62 // Some internal object have intrinsic names such as
63 // ([][Symbol.iterator]().__proto__.__proto__)
64 // and
65 // RegExp.prototype[Symbol.match]
66 // which get turned into a babel node here.
67 // TODO: We should properly parse such a string, and memoize all references in it separately.
68 // Instead, we just turn it into a funky identifier, which Babel seems to accept.
69 init = t.identifier(key);
70 } else if (key === "global") {
71 this.usesThis = true;
72 init = t.thisExpression();
73 } else {
74 let i = key.lastIndexOf(".");
75
76 if (i === -1) {
77 init = t.memberExpression(this.memoizeReference("global"), t.identifier(key));
78 } else {
79 init = t.memberExpression(this.memoizeReference(key.substr(0, i)), t.identifier(key.substr(i + 1)));
80 }
81 }
82
83 ref = t.identifier(this.nameGenerator.generate(key));
84 this.prelude.push(t.variableDeclaration("var", [t.variableDeclarator(ref, init)]));
85 this.memoizedRefs.set(key, ref);
86 return ref;
87 }
88
89}
90
91exports.PreludeGenerator = PreludeGenerator;
92//# sourceMappingURL=PreludeGenerator.js.map
\No newline at end of file