1 | "use strict";
|
2 |
|
3 | var _keys = require("babel-runtime/core-js/object/keys");
|
4 |
|
5 | var _keys2 = _interopRequireDefault(_keys);
|
6 |
|
7 | var _babelTypes = require("babel-types");
|
8 |
|
9 |
|
10 | var t = _interopRequireWildcard(_babelTypes);
|
11 |
|
12 |
|
13 | function _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 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 | var hasOwn = Object.prototype.hasOwnProperty;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | exports.hoist = function (funPath) {
|
34 | t.assertFunction(funPath.node);
|
35 |
|
36 | var vars = {};
|
37 |
|
38 | function varDeclToExpr(vdec, includeIdentifiers) {
|
39 | t.assertVariableDeclaration(vdec);
|
40 |
|
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 |
|
68 |
|
69 | path.replaceWith(t.expressionStatement(expr));
|
70 | }
|
71 |
|
72 |
|
73 |
|
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 |
|
100 |
|
101 | path.parentPath.unshiftContainer("body", assignment);
|
102 |
|
103 |
|
104 |
|
105 | path.remove();
|
106 | } else {
|
107 |
|
108 |
|
109 |
|
110 | path.replaceWith(assignment);
|
111 | }
|
112 |
|
113 |
|
114 | path.skip();
|
115 | },
|
116 |
|
117 | FunctionExpression: function /*istanbul ignore next*/FunctionExpression(path) {
|
118 |
|
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 |
|
130 |
|
131 | }
|
132 | });
|
133 |
|
134 | var declarations = [];
|
135 |
|
136 | (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;
|
144 | }
|
145 |
|
146 | return t.variableDeclaration("var", declarations);
|
147 | }; |
\ | No newline at end of file |