UNPKG

4.86 kBJavaScriptView Raw
1/*istanbul ignore next*/"use strict";
2
3var _keys = require("babel-runtime/core-js/object/keys");
4
5var _keys2 = _interopRequireDefault(_keys);
6
7var /*istanbul ignore next*/_babelTypes = require("babel-types");
8
9/*istanbul ignore next*/
10var t = _interopRequireWildcard(_babelTypes);
11
12/*istanbul ignore next*/
13function _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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17var hasOwn = Object.prototype.hasOwnProperty;
18
19// The hoist function takes a FunctionExpression or FunctionDeclaration
20// and replaces any Declaration nodes in its body with assignments, then
21// returns a VariableDeclaration containing just the names of the removed
22// declarations.
23/**
24 * Copyright (c) 2014, Facebook, Inc.
25 * All rights reserved.
26 *
27 * This source code is licensed under the BSD-style license found in the
28 * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
29 * additional grant of patent rights can be found in the PATENTS file in
30 * the same directory.
31 */
32
33exports.hoist = function (funPath) {
34 t.assertFunction(funPath.node);
35
36 var vars = {};
37
38 function varDeclToExpr(vdec, includeIdentifiers) {
39 t.assertVariableDeclaration(vdec);
40 // TODO assert.equal(vdec.kind, "var");
41 var exprs = [];
42
43 vdec.declarations.forEach(function (dec) {
44 vars[dec.id.name] = dec.id;
45
46 if (dec.init) {
47 exprs.push(t.assignmentExpression("=", dec.id, dec.init));
48 } else if (includeIdentifiers) {
49 exprs.push(dec.id);
50 }
51 });
52
53 if (exprs.length === 0) return null;
54
55 if (exprs.length === 1) return exprs[0];
56
57 return t.sequenceExpression(exprs);
58 }
59
60 funPath.get("body").traverse({
61 VariableDeclaration: {
62 exit: function /*istanbul ignore next*/exit(path) {
63 var expr = varDeclToExpr(path.node, false);
64 if (expr === null) {
65 path.remove();
66 } else {
67 // We don't need to traverse this expression any further because
68 // there can't be any new declarations inside an expression.
69 path.replaceWith(t.expressionStatement(expr));
70 }
71
72 // Since the original node has been either removed or replaced,
73 // avoid traversing it any further.
74 path.skip();
75 }
76 },
77
78 ForStatement: function /*istanbul ignore next*/ForStatement(path) {
79 var init = path.node.init;
80 if (t.isVariableDeclaration(init)) {
81 path.get("init").replaceWith(varDeclToExpr(init, false));
82 }
83 },
84
85 ForXStatement: function /*istanbul ignore next*/ForXStatement(path) {
86 var left = path.get("left");
87 if (left.isVariableDeclaration()) {
88 left.replaceWith(varDeclToExpr(left.node, true));
89 }
90 },
91
92 FunctionDeclaration: function /*istanbul ignore next*/FunctionDeclaration(path) {
93 var node = path.node;
94 vars[node.id.name] = node.id;
95
96 var assignment = t.expressionStatement(t.assignmentExpression("=", node.id, t.functionExpression(node.id, node.params, node.body, node.generator, node.expression)));
97
98 if (path.parentPath.isBlockStatement()) {
99 // Insert the assignment form before the first statement in the
100 // enclosing block.
101 path.parentPath.unshiftContainer("body", assignment);
102
103 // Remove the function declaration now that we've inserted the
104 // equivalent assignment form at the beginning of the block.
105 path.remove();
106 } else {
107 // If the parent node is not a block statement, then we can just
108 // replace the declaration with the equivalent assignment form
109 // without worrying about hoisting it.
110 path.replaceWith(assignment);
111 }
112
113 // Don't hoist variables out of inner functions.
114 path.skip();
115 },
116
117 FunctionExpression: function /*istanbul ignore next*/FunctionExpression(path) {
118 // Don't descend into nested function expressions.
119 path.skip();
120 }
121 });
122
123 var paramNames = {};
124 funPath.get("params").forEach(function (paramPath) {
125 var param = paramPath.node;
126 if (t.isIdentifier(param)) {
127 paramNames[param.name] = param;
128 } else {
129 // Variables declared by destructuring parameter patterns will be
130 // harmlessly re-declared.
131 }
132 });
133
134 var declarations = [];
135
136 /*istanbul ignore next*/(0, _keys2.default)(vars).forEach(function (name) {
137 if (!hasOwn.call(paramNames, name)) {
138 declarations.push(t.variableDeclarator(vars[name], null));
139 }
140 });
141
142 if (declarations.length === 0) {
143 return null; // Be sure to handle this case!
144 }
145
146 return t.variableDeclaration("var", declarations);
147};
\No newline at end of file