1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | import * as t from "babel-types";
|
12 | let hasOwn = Object.prototype.hasOwnProperty;
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | exports.hoist = function(funPath) {
|
19 | t.assertFunction(funPath.node);
|
20 |
|
21 | let vars = {};
|
22 |
|
23 | function varDeclToExpr(vdec, includeIdentifiers) {
|
24 | t.assertVariableDeclaration(vdec);
|
25 |
|
26 | let exprs = [];
|
27 |
|
28 | vdec.declarations.forEach(function(dec) {
|
29 | vars[dec.id.name] = dec.id;
|
30 |
|
31 | if (dec.init) {
|
32 | exprs.push(t.assignmentExpression(
|
33 | "=", dec.id, dec.init
|
34 | ));
|
35 | } else if (includeIdentifiers) {
|
36 | exprs.push(dec.id);
|
37 | }
|
38 | });
|
39 |
|
40 | if (exprs.length === 0)
|
41 | return null;
|
42 |
|
43 | if (exprs.length === 1)
|
44 | return exprs[0];
|
45 |
|
46 | return t.sequenceExpression(exprs);
|
47 | }
|
48 |
|
49 | funPath.get("body").traverse({
|
50 | VariableDeclaration: {
|
51 | exit: function(path) {
|
52 | let expr = varDeclToExpr(path.node, false);
|
53 | if (expr === null) {
|
54 | path.remove();
|
55 | } else {
|
56 |
|
57 |
|
58 | path.replaceWith(t.expressionStatement(expr));
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 | path.skip();
|
64 | }
|
65 | },
|
66 |
|
67 | ForStatement: function(path) {
|
68 | let init = path.node.init;
|
69 | if (t.isVariableDeclaration(init)) {
|
70 | path.get("init").replaceWith(varDeclToExpr(init, false));
|
71 | }
|
72 | },
|
73 |
|
74 | ForXStatement: function(path) {
|
75 | let left = path.get("left");
|
76 | if (left.isVariableDeclaration()) {
|
77 | left.replaceWith(varDeclToExpr(left.node, true));
|
78 | }
|
79 | },
|
80 |
|
81 | FunctionDeclaration: function(path) {
|
82 | let node = path.node;
|
83 | vars[node.id.name] = node.id;
|
84 |
|
85 | let assignment = t.expressionStatement(
|
86 | t.assignmentExpression(
|
87 | "=",
|
88 | node.id,
|
89 | t.functionExpression(
|
90 | node.id,
|
91 | node.params,
|
92 | node.body,
|
93 | node.generator,
|
94 | node.expression
|
95 | )
|
96 | )
|
97 | );
|
98 |
|
99 | if (path.parentPath.isBlockStatement()) {
|
100 |
|
101 |
|
102 | path.parentPath.unshiftContainer("body", assignment);
|
103 |
|
104 |
|
105 |
|
106 | path.remove();
|
107 | } else {
|
108 |
|
109 |
|
110 |
|
111 | path.replaceWith(assignment);
|
112 | }
|
113 |
|
114 |
|
115 | path.skip();
|
116 | },
|
117 |
|
118 | FunctionExpression: function(path) {
|
119 |
|
120 | path.skip();
|
121 | }
|
122 | });
|
123 |
|
124 | let paramNames = {};
|
125 | funPath.get("params").forEach(function(paramPath) {
|
126 | let param = paramPath.node;
|
127 | if (t.isIdentifier(param)) {
|
128 | paramNames[param.name] = param;
|
129 | } else {
|
130 |
|
131 |
|
132 | }
|
133 | });
|
134 |
|
135 | let declarations = [];
|
136 |
|
137 | Object.keys(vars).forEach(function(name) {
|
138 | if (!hasOwn.call(paramNames, name)) {
|
139 | declarations.push(t.variableDeclarator(vars[name], null));
|
140 | }
|
141 | });
|
142 |
|
143 | if (declarations.length === 0) {
|
144 | return null;
|
145 | }
|
146 |
|
147 | return t.variableDeclaration("var", declarations);
|
148 | };
|