1 | "use strict";
|
2 |
|
3 | var util = _interopRequireWildcard(require("./util"));
|
4 | function _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); }
|
5 | function _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 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var hasOwn = Object.prototype.hasOwnProperty;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | exports.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 |
|
28 | var exprs = [];
|
29 | vdec.declarations.forEach(function (dec) {
|
30 |
|
31 |
|
32 | vars[dec.id.name] = t.identifier(dec.id.name);
|
33 |
|
34 |
|
35 |
|
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 |
|
55 |
|
56 | util.replaceWithOrRemove(path, t.expressionStatement(expr));
|
57 | }
|
58 |
|
59 |
|
60 |
|
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 |
|
82 |
|
83 | path.parentPath.unshiftContainer("body", assignment);
|
84 |
|
85 |
|
86 |
|
87 | path.remove();
|
88 | } else {
|
89 |
|
90 |
|
91 |
|
92 | util.replaceWithOrRemove(path, assignment);
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 | path.scope.removeBinding(node.id.name);
|
98 |
|
99 |
|
100 | path.skip();
|
101 | },
|
102 | FunctionExpression: function FunctionExpression(path) {
|
103 |
|
104 | path.skip();
|
105 | },
|
106 | ArrowFunctionExpression: function ArrowFunctionExpression(path) {
|
107 |
|
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 |
|
118 |
|
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;
|
129 | }
|
130 |
|
131 | return t.variableDeclaration("var", declarations);
|
132 | }; |
\ | No newline at end of file |