source: trip-planner-front/node_modules/regenerator-transform/lib/hoist.js@ 6a80231

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

initial commit

  • Property mode set to 100644
File size: 4.7 KB
Line 
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var util = _interopRequireWildcard(require("./util"));
6
7/**
8 * Copyright (c) 2014-present, Facebook, Inc.
9 *
10 * This source code is licensed under the MIT license found in the
11 * LICENSE file in the root directory of this source tree.
12 */
13var hasOwn = Object.prototype.hasOwnProperty; // The hoist function takes a FunctionExpression or FunctionDeclaration
14// and replaces any Declaration nodes in its body with assignments, then
15// returns a VariableDeclaration containing just the names of the removed
16// declarations.
17
18exports.hoist = function (funPath) {
19 var t = util.getTypes();
20 t.assertFunction(funPath.node);
21 var vars = {};
22
23 function varDeclToExpr(_ref, includeIdentifiers) {
24 var vdec = _ref.node,
25 scope = _ref.scope;
26 t.assertVariableDeclaration(vdec); // TODO assert.equal(vdec.kind, "var");
27
28 var exprs = [];
29 vdec.declarations.forEach(function (dec) {
30 // Note: We duplicate 'dec.id' here to ensure that the variable declaration IDs don't
31 // have the same 'loc' value, since that can make sourcemaps and retainLines behave poorly.
32 vars[dec.id.name] = t.identifier(dec.id.name); // Remove the binding, to avoid "duplicate declaration" errors when it will
33 // be injected again.
34
35 scope.removeBinding(dec.id.name);
36
37 if (dec.init) {
38 exprs.push(t.assignmentExpression("=", dec.id, dec.init));
39 } else if (includeIdentifiers) {
40 exprs.push(dec.id);
41 }
42 });
43 if (exprs.length === 0) return null;
44 if (exprs.length === 1) return exprs[0];
45 return t.sequenceExpression(exprs);
46 }
47
48 funPath.get("body").traverse({
49 VariableDeclaration: {
50 exit: function exit(path) {
51 var expr = varDeclToExpr(path, false);
52
53 if (expr === null) {
54 path.remove();
55 } else {
56 // We don't need to traverse this expression any further because
57 // there can't be any new declarations inside an expression.
58 util.replaceWithOrRemove(path, t.expressionStatement(expr));
59 } // Since the original node has been either removed or replaced,
60 // avoid traversing it any further.
61
62
63 path.skip();
64 }
65 },
66 ForStatement: function ForStatement(path) {
67 var init = path.get("init");
68
69 if (init.isVariableDeclaration()) {
70 util.replaceWithOrRemove(init, varDeclToExpr(init, false));
71 }
72 },
73 ForXStatement: function ForXStatement(path) {
74 var left = path.get("left");
75
76 if (left.isVariableDeclaration()) {
77 util.replaceWithOrRemove(left, varDeclToExpr(left, true));
78 }
79 },
80 FunctionDeclaration: function FunctionDeclaration(path) {
81 var node = path.node;
82 vars[node.id.name] = node.id;
83 var assignment = t.expressionStatement(t.assignmentExpression("=", t.clone(node.id), t.functionExpression(path.scope.generateUidIdentifierBasedOnNode(node), node.params, node.body, node.generator, node.expression)));
84
85 if (path.parentPath.isBlockStatement()) {
86 // Insert the assignment form before the first statement in the
87 // enclosing block.
88 path.parentPath.unshiftContainer("body", assignment); // Remove the function declaration now that we've inserted the
89 // equivalent assignment form at the beginning of the block.
90
91 path.remove();
92 } else {
93 // If the parent node is not a block statement, then we can just
94 // replace the declaration with the equivalent assignment form
95 // without worrying about hoisting it.
96 util.replaceWithOrRemove(path, assignment);
97 } // Remove the binding, to avoid "duplicate declaration" errors when it will
98 // be injected again.
99
100
101 path.scope.removeBinding(node.id.name); // Don't hoist variables out of inner functions.
102
103 path.skip();
104 },
105 FunctionExpression: function FunctionExpression(path) {
106 // Don't descend into nested function expressions.
107 path.skip();
108 },
109 ArrowFunctionExpression: function ArrowFunctionExpression(path) {
110 // Don't descend into nested function expressions.
111 path.skip();
112 }
113 });
114 var paramNames = {};
115 funPath.get("params").forEach(function (paramPath) {
116 var param = paramPath.node;
117
118 if (t.isIdentifier(param)) {
119 paramNames[param.name] = param;
120 } else {// Variables declared by destructuring parameter patterns will be
121 // harmlessly re-declared.
122 }
123 });
124 var declarations = [];
125 Object.keys(vars).forEach(function (name) {
126 if (!hasOwn.call(paramNames, name)) {
127 declarations.push(t.variableDeclarator(vars[name], null));
128 }
129 });
130
131 if (declarations.length === 0) {
132 return null; // Be sure to handle this case!
133 }
134
135 return t.variableDeclaration("var", declarations);
136};
Note: See TracBrowser for help on using the repository browser.