source: imaps-frontend/node_modules/@babel/helper-module-transforms/lib/rewrite-live-references.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 13.0 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = rewriteLiveReferences;
7var _assert = require("assert");
8var _core = require("@babel/core");
9var _helperSimpleAccess = require("@babel/helper-simple-access");
10function isInType(path) {
11 do {
12 switch (path.parent.type) {
13 case "TSTypeAnnotation":
14 case "TSTypeAliasDeclaration":
15 case "TSTypeReference":
16 case "TypeAnnotation":
17 case "TypeAlias":
18 return true;
19 case "ExportSpecifier":
20 return path.parentPath.parent.exportKind === "type";
21 default:
22 if (path.parentPath.isStatement() || path.parentPath.isExpression()) {
23 return false;
24 }
25 }
26 } while (path = path.parentPath);
27}
28function rewriteLiveReferences(programPath, metadata, wrapReference) {
29 const imported = new Map();
30 const exported = new Map();
31 const requeueInParent = path => {
32 programPath.requeue(path);
33 };
34 for (const [source, data] of metadata.source) {
35 for (const [localName, importName] of data.imports) {
36 imported.set(localName, [source, importName, null]);
37 }
38 for (const localName of data.importsNamespace) {
39 imported.set(localName, [source, null, localName]);
40 }
41 }
42 for (const [local, data] of metadata.local) {
43 let exportMeta = exported.get(local);
44 if (!exportMeta) {
45 exportMeta = [];
46 exported.set(local, exportMeta);
47 }
48 exportMeta.push(...data.names);
49 }
50 const rewriteBindingInitVisitorState = {
51 metadata,
52 requeueInParent,
53 scope: programPath.scope,
54 exported
55 };
56 programPath.traverse(rewriteBindingInitVisitor, rewriteBindingInitVisitorState);
57 const bindingNames = new Set([...Array.from(imported.keys()), ...Array.from(exported.keys())]);
58 {
59 (0, _helperSimpleAccess.default)(programPath, bindingNames, false);
60 }
61 const rewriteReferencesVisitorState = {
62 seen: new WeakSet(),
63 metadata,
64 requeueInParent,
65 scope: programPath.scope,
66 imported,
67 exported,
68 buildImportReference([source, importName, localName], identNode) {
69 const meta = metadata.source.get(source);
70 meta.referenced = true;
71 if (localName) {
72 if (meta.wrap) {
73 var _wrapReference;
74 identNode = (_wrapReference = wrapReference(identNode, meta.wrap)) != null ? _wrapReference : identNode;
75 }
76 return identNode;
77 }
78 let namespace = _core.types.identifier(meta.name);
79 if (meta.wrap) {
80 var _wrapReference2;
81 namespace = (_wrapReference2 = wrapReference(namespace, meta.wrap)) != null ? _wrapReference2 : namespace;
82 }
83 if (importName === "default" && meta.interop === "node-default") {
84 return namespace;
85 }
86 const computed = metadata.stringSpecifiers.has(importName);
87 return _core.types.memberExpression(namespace, computed ? _core.types.stringLiteral(importName) : _core.types.identifier(importName), computed);
88 }
89 };
90 programPath.traverse(rewriteReferencesVisitor, rewriteReferencesVisitorState);
91}
92const rewriteBindingInitVisitor = {
93 Scope(path) {
94 path.skip();
95 },
96 ClassDeclaration(path) {
97 const {
98 requeueInParent,
99 exported,
100 metadata
101 } = this;
102 const {
103 id
104 } = path.node;
105 if (!id) throw new Error("Expected class to have a name");
106 const localName = id.name;
107 const exportNames = exported.get(localName) || [];
108 if (exportNames.length > 0) {
109 const statement = _core.types.expressionStatement(buildBindingExportAssignmentExpression(metadata, exportNames, _core.types.identifier(localName), path.scope));
110 statement._blockHoist = path.node._blockHoist;
111 requeueInParent(path.insertAfter(statement)[0]);
112 }
113 },
114 VariableDeclaration(path) {
115 const {
116 requeueInParent,
117 exported,
118 metadata
119 } = this;
120 const isVar = path.node.kind === "var";
121 for (const decl of path.get("declarations")) {
122 const {
123 id
124 } = decl.node;
125 let {
126 init
127 } = decl.node;
128 if (_core.types.isIdentifier(id) && exported.has(id.name) && !_core.types.isArrowFunctionExpression(init) && (!_core.types.isFunctionExpression(init) || init.id) && (!_core.types.isClassExpression(init) || init.id)) {
129 if (!init) {
130 if (isVar) {
131 continue;
132 } else {
133 init = path.scope.buildUndefinedNode();
134 }
135 }
136 decl.node.init = buildBindingExportAssignmentExpression(metadata, exported.get(id.name), init, path.scope);
137 requeueInParent(decl.get("init"));
138 } else {
139 for (const localName of Object.keys(decl.getOuterBindingIdentifiers())) {
140 if (exported.has(localName)) {
141 const statement = _core.types.expressionStatement(buildBindingExportAssignmentExpression(metadata, exported.get(localName), _core.types.identifier(localName), path.scope));
142 statement._blockHoist = path.node._blockHoist;
143 requeueInParent(path.insertAfter(statement)[0]);
144 }
145 }
146 }
147 }
148 }
149};
150const buildBindingExportAssignmentExpression = (metadata, exportNames, localExpr, scope) => {
151 const exportsObjectName = metadata.exportName;
152 for (let currentScope = scope; currentScope != null; currentScope = currentScope.parent) {
153 if (currentScope.hasOwnBinding(exportsObjectName)) {
154 currentScope.rename(exportsObjectName);
155 }
156 }
157 return (exportNames || []).reduce((expr, exportName) => {
158 const {
159 stringSpecifiers
160 } = metadata;
161 const computed = stringSpecifiers.has(exportName);
162 return _core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.identifier(exportsObjectName), computed ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName), computed), expr);
163 }, localExpr);
164};
165const buildImportThrow = localName => {
166 return _core.template.expression.ast`
167 (function() {
168 throw new Error('"' + '${localName}' + '" is read-only.');
169 })()
170 `;
171};
172const rewriteReferencesVisitor = {
173 ReferencedIdentifier(path) {
174 const {
175 seen,
176 buildImportReference,
177 scope,
178 imported,
179 requeueInParent
180 } = this;
181 if (seen.has(path.node)) return;
182 seen.add(path.node);
183 const localName = path.node.name;
184 const importData = imported.get(localName);
185 if (importData) {
186 if (isInType(path)) {
187 throw path.buildCodeFrameError(`Cannot transform the imported binding "${localName}" since it's also used in a type annotation. ` + `Please strip type annotations using @babel/preset-typescript or @babel/preset-flow.`);
188 }
189 const localBinding = path.scope.getBinding(localName);
190 const rootBinding = scope.getBinding(localName);
191 if (rootBinding !== localBinding) return;
192 const ref = buildImportReference(importData, path.node);
193 ref.loc = path.node.loc;
194 if ((path.parentPath.isCallExpression({
195 callee: path.node
196 }) || path.parentPath.isOptionalCallExpression({
197 callee: path.node
198 }) || path.parentPath.isTaggedTemplateExpression({
199 tag: path.node
200 })) && _core.types.isMemberExpression(ref)) {
201 path.replaceWith(_core.types.sequenceExpression([_core.types.numericLiteral(0), ref]));
202 } else if (path.isJSXIdentifier() && _core.types.isMemberExpression(ref)) {
203 const {
204 object,
205 property
206 } = ref;
207 path.replaceWith(_core.types.jsxMemberExpression(_core.types.jsxIdentifier(object.name), _core.types.jsxIdentifier(property.name)));
208 } else {
209 path.replaceWith(ref);
210 }
211 requeueInParent(path);
212 path.skip();
213 }
214 },
215 UpdateExpression(path) {
216 const {
217 scope,
218 seen,
219 imported,
220 exported,
221 requeueInParent,
222 buildImportReference
223 } = this;
224 if (seen.has(path.node)) return;
225 seen.add(path.node);
226 const arg = path.get("argument");
227 if (arg.isMemberExpression()) return;
228 const update = path.node;
229 if (arg.isIdentifier()) {
230 const localName = arg.node.name;
231 if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
232 return;
233 }
234 const exportedNames = exported.get(localName);
235 const importData = imported.get(localName);
236 if ((exportedNames == null ? void 0 : exportedNames.length) > 0 || importData) {
237 if (importData) {
238 path.replaceWith(_core.types.assignmentExpression(update.operator[0] + "=", buildImportReference(importData, arg.node), buildImportThrow(localName)));
239 } else if (update.prefix) {
240 path.replaceWith(buildBindingExportAssignmentExpression(this.metadata, exportedNames, _core.types.cloneNode(update), path.scope));
241 } else {
242 const ref = scope.generateDeclaredUidIdentifier(localName);
243 path.replaceWith(_core.types.sequenceExpression([_core.types.assignmentExpression("=", _core.types.cloneNode(ref), _core.types.cloneNode(update)), buildBindingExportAssignmentExpression(this.metadata, exportedNames, _core.types.identifier(localName), path.scope), _core.types.cloneNode(ref)]));
244 }
245 }
246 }
247 requeueInParent(path);
248 path.skip();
249 },
250 AssignmentExpression: {
251 exit(path) {
252 const {
253 scope,
254 seen,
255 imported,
256 exported,
257 requeueInParent,
258 buildImportReference
259 } = this;
260 if (seen.has(path.node)) return;
261 seen.add(path.node);
262 const left = path.get("left");
263 if (left.isMemberExpression()) return;
264 if (left.isIdentifier()) {
265 const localName = left.node.name;
266 if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
267 return;
268 }
269 const exportedNames = exported.get(localName);
270 const importData = imported.get(localName);
271 if ((exportedNames == null ? void 0 : exportedNames.length) > 0 || importData) {
272 _assert(path.node.operator === "=", "Path was not simplified");
273 const assignment = path.node;
274 if (importData) {
275 assignment.left = buildImportReference(importData, left.node);
276 assignment.right = _core.types.sequenceExpression([assignment.right, buildImportThrow(localName)]);
277 }
278 path.replaceWith(buildBindingExportAssignmentExpression(this.metadata, exportedNames, assignment, path.scope));
279 requeueInParent(path);
280 }
281 } else {
282 const ids = left.getOuterBindingIdentifiers();
283 const programScopeIds = Object.keys(ids).filter(localName => scope.getBinding(localName) === path.scope.getBinding(localName));
284 const id = programScopeIds.find(localName => imported.has(localName));
285 if (id) {
286 path.node.right = _core.types.sequenceExpression([path.node.right, buildImportThrow(id)]);
287 }
288 const items = [];
289 programScopeIds.forEach(localName => {
290 const exportedNames = exported.get(localName) || [];
291 if (exportedNames.length > 0) {
292 items.push(buildBindingExportAssignmentExpression(this.metadata, exportedNames, _core.types.identifier(localName), path.scope));
293 }
294 });
295 if (items.length > 0) {
296 let node = _core.types.sequenceExpression(items);
297 if (path.parentPath.isExpressionStatement()) {
298 node = _core.types.expressionStatement(node);
299 node._blockHoist = path.parentPath.node._blockHoist;
300 }
301 const statement = path.insertAfter(node)[0];
302 requeueInParent(statement);
303 }
304 }
305 }
306 },
307 "ForOfStatement|ForInStatement"(path) {
308 const {
309 scope,
310 node
311 } = path;
312 const {
313 left
314 } = node;
315 const {
316 exported,
317 imported,
318 scope: programScope
319 } = this;
320 if (!_core.types.isVariableDeclaration(left)) {
321 let didTransformExport = false,
322 importConstViolationName;
323 const loopBodyScope = path.get("body").scope;
324 for (const name of Object.keys(_core.types.getOuterBindingIdentifiers(left))) {
325 if (programScope.getBinding(name) === scope.getBinding(name)) {
326 if (exported.has(name)) {
327 didTransformExport = true;
328 if (loopBodyScope.hasOwnBinding(name)) {
329 loopBodyScope.rename(name);
330 }
331 }
332 if (imported.has(name) && !importConstViolationName) {
333 importConstViolationName = name;
334 }
335 }
336 }
337 if (!didTransformExport && !importConstViolationName) {
338 return;
339 }
340 path.ensureBlock();
341 const bodyPath = path.get("body");
342 const newLoopId = scope.generateUidIdentifierBasedOnNode(left);
343 path.get("left").replaceWith(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(newLoopId))]));
344 scope.registerDeclaration(path.get("left"));
345 if (didTransformExport) {
346 bodyPath.unshiftContainer("body", _core.types.expressionStatement(_core.types.assignmentExpression("=", left, newLoopId)));
347 }
348 if (importConstViolationName) {
349 bodyPath.unshiftContainer("body", _core.types.expressionStatement(buildImportThrow(importConstViolationName)));
350 }
351 }
352 }
353};
354
355//# sourceMappingURL=rewrite-live-references.js.map
Note: See TracBrowser for help on using the repository browser.