source: trip-planner-front/node_modules/@babel/helper-module-transforms/lib/rewrite-live-references.js@ ceaed42

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

initial commit

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