source: imaps-frontend/node_modules/regenerator-transform/lib/hoist.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

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