/
index.ts
187 lines (155 loc) 路 5.37 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { declare } from "@babel/helper-plugin-utils";
import { types as t } from "@babel/core";
import {
DestructuringTransformer,
convertVariableDeclaration,
convertAssignmentExpression,
unshiftForXStatementBody,
} from "./util";
export { buildObjectExcludingKeys, unshiftForXStatementBody } from "./util";
/**
* Test if a VariableDeclaration's declarations contains any Patterns.
*/
function variableDeclarationHasPattern(node: t.VariableDeclaration) {
for (const declar of node.declarations) {
if (t.isPattern(declar.id)) {
return true;
}
}
return false;
}
export interface Options {
allowArrayLike?: boolean;
loose?: boolean;
useBuiltIns?: boolean;
}
export default declare((api, options: Options) => {
api.assertVersion(7);
const { useBuiltIns = false } = options;
const iterableIsArray = (api.assumption("iterableIsArray") ??
options.loose ??
false) as boolean;
const arrayLikeIsIterable = (options.allowArrayLike ??
api.assumption("arrayLikeIsIterable") ??
false) as boolean;
const objectRestNoSymbols = (api.assumption("objectRestNoSymbols") ??
options.loose ??
false) as boolean;
return {
name: "transform-destructuring",
visitor: {
ExportNamedDeclaration(path) {
const declaration = path.get("declaration");
if (!declaration.isVariableDeclaration()) return;
if (!variableDeclarationHasPattern(declaration.node)) return;
const specifiers = [];
for (const name of Object.keys(path.getOuterBindingIdentifiers())) {
specifiers.push(
t.exportSpecifier(t.identifier(name), t.identifier(name)),
);
}
// Split the declaration and export list into two declarations so that the variable
// declaration can be split up later without needing to worry about not being a
// top-level statement.
path.replaceWith(declaration.node);
path.insertAfter(t.exportNamedDeclaration(null, specifiers));
path.scope.crawl();
},
ForXStatement(path) {
const { node, scope } = path;
const left = node.left;
if (t.isPattern(left)) {
// for ({ length: k } in { abc: 3 });
const temp = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration("var", [
t.variableDeclarator(temp),
]);
path.ensureBlock();
const statementBody = (node.body as t.BlockStatement).body;
const nodes = [];
// todo: the completion of a for statement can only be observed from
// a do block (or eval that we don't support),
// but the new do-expression proposal plans to ban iteration ends in the
// do block, maybe we can get rid of this
if (statementBody.length === 0 && path.isCompletionRecord()) {
nodes.unshift(t.expressionStatement(scope.buildUndefinedNode()));
}
nodes.unshift(
t.expressionStatement(
t.assignmentExpression("=", left, t.cloneNode(temp)),
),
);
unshiftForXStatementBody(path, nodes);
scope.crawl();
return;
}
if (!t.isVariableDeclaration(left)) return;
const pattern = left.declarations[0].id;
if (!t.isPattern(pattern)) return;
const key = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration(left.kind, [
t.variableDeclarator(key, null),
]);
const nodes = [];
const destructuring = new DestructuringTransformer({
kind: left.kind,
scope: scope,
nodes: nodes,
arrayLikeIsIterable,
iterableIsArray,
objectRestNoSymbols,
useBuiltIns,
addHelper: name => this.addHelper(name),
});
destructuring.init(pattern, key);
unshiftForXStatementBody(path, nodes);
scope.crawl();
},
CatchClause({ node, scope }) {
const pattern = node.param;
if (!t.isPattern(pattern)) return;
const ref = scope.generateUidIdentifier("ref");
node.param = ref;
const nodes = [];
const destructuring = new DestructuringTransformer({
kind: "let",
scope: scope,
nodes: nodes,
arrayLikeIsIterable,
iterableIsArray,
objectRestNoSymbols,
useBuiltIns,
addHelper: name => this.addHelper(name),
});
destructuring.init(pattern, ref);
node.body.body = nodes.concat(node.body.body);
scope.crawl();
},
AssignmentExpression(path, state) {
if (!t.isPattern(path.node.left)) return;
convertAssignmentExpression(
path,
name => state.addHelper(name),
arrayLikeIsIterable,
iterableIsArray,
objectRestNoSymbols,
useBuiltIns,
);
},
VariableDeclaration(path, state) {
const { node, parent } = path;
if (t.isForXStatement(parent)) return;
if (!parent || !path.container) return; // i don't know why this is necessary - TODO
if (!variableDeclarationHasPattern(node)) return;
convertVariableDeclaration(
path,
name => state.addHelper(name),
arrayLikeIsIterable,
iterableIsArray,
objectRestNoSymbols,
useBuiltIns,
);
},
},
};
});