/
index.js
232 lines (200 loc) 路 6.4 KB
/
index.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { declare } from "@babel/helper-plugin-utils";
import { template, types as t } from "@babel/core";
import transformWithoutHelper from "./no-helper-implementation";
export default declare((api, options) => {
api.assertVersion(7);
const { loose, assumeArray } = options;
if (loose === true && assumeArray === true) {
throw new Error(
`The loose and assumeArray options cannot be used together in @babel/plugin-transform-for-of`,
);
}
if (assumeArray) {
return {
name: "transform-for-of",
visitor: {
ForOfStatement(path) {
const { scope } = path;
const { left, right, await: isAwait } = path.node;
if (isAwait) {
return;
}
const i = scope.generateUidIdentifier("i");
let array = scope.maybeGenerateMemoised(right, true);
const inits = [t.variableDeclarator(i, t.numericLiteral(0))];
if (array) {
inits.push(t.variableDeclarator(array, right));
} else {
array = right;
}
const item = t.memberExpression(
t.cloneNode(array),
t.cloneNode(i),
true,
);
let assignment;
if (t.isVariableDeclaration(left)) {
assignment = left;
assignment.declarations[0].init = item;
} else {
assignment = t.expressionStatement(
t.assignmentExpression("=", left, item),
);
}
let blockBody;
const body = path.get("body");
if (
body.isBlockStatement() &&
Object.keys(path.getBindingIdentifiers()).some(id =>
body.scope.hasOwnBinding(id),
)
) {
blockBody = t.blockStatement([assignment, body.node]);
} else {
blockBody = t.toBlock(body.node);
blockBody.body.unshift(assignment);
}
path.replaceWith(
t.forStatement(
t.variableDeclaration("let", inits),
t.binaryExpression(
"<",
t.cloneNode(i),
t.memberExpression(t.cloneNode(array), t.identifier("length")),
),
t.updateExpression("++", t.cloneNode(i)),
blockBody,
),
);
},
},
};
}
const buildForOfArray = template(`
for (var KEY = 0, NAME = ARR; KEY < NAME.length; KEY++) BODY;
`);
const buildForOfLoose = template.statements(`
for (var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT), STEP_KEY;
!(STEP_KEY = ITERATOR_HELPER()).done;) BODY;
`);
const buildForOf = template.statements(`
var ITERATOR_HELPER = CREATE_ITERATOR_HELPER(OBJECT), STEP_KEY;
try {
for (ITERATOR_HELPER.s(); !(STEP_KEY = ITERATOR_HELPER.n()).done;) BODY;
} catch (err) {
ITERATOR_HELPER.e(err);
} finally {
ITERATOR_HELPER.f();
}
`);
const builder = loose
? {
build: buildForOfLoose,
helper: "createForOfIteratorHelperLoose",
getContainer: nodes => nodes,
}
: {
build: buildForOf,
helper: "createForOfIteratorHelper",
getContainer: nodes => nodes[1].block.body,
};
function _ForOfStatementArray(path) {
const { node, scope } = path;
const right = scope.generateUidIdentifierBasedOnNode(node.right, "arr");
const iterationKey = scope.generateUidIdentifier("i");
const loop = buildForOfArray({
BODY: node.body,
KEY: iterationKey,
NAME: right,
ARR: node.right,
});
t.inherits(loop, node);
t.ensureBlock(loop);
const iterationValue = t.memberExpression(
t.cloneNode(right),
t.cloneNode(iterationKey),
true,
);
const left = node.left;
if (t.isVariableDeclaration(left)) {
left.declarations[0].init = iterationValue;
loop.body.body.unshift(left);
} else {
loop.body.body.unshift(
t.expressionStatement(
t.assignmentExpression("=", left, iterationValue),
),
);
}
return loop;
}
return {
name: "transform-for-of",
visitor: {
ForOfStatement(path, state) {
const right = path.get("right");
if (
right.isArrayExpression() ||
right.isGenericType("Array") ||
t.isArrayTypeAnnotation(right.getTypeAnnotation())
) {
path.replaceWith(_ForOfStatementArray(path));
return;
}
if (!state.availableHelper(builder.helper)) {
// Babel <7.9.0 doesn't support this helper
transformWithoutHelper(loose, path, state);
}
const { node, parent, scope } = path;
const left = node.left;
let declar;
const stepKey = scope.generateUid("step");
const stepValue = t.memberExpression(
t.identifier(stepKey),
t.identifier("value"),
);
if (
t.isIdentifier(left) ||
t.isPattern(left) ||
t.isMemberExpression(left)
) {
// for (i of test), for ({ i } of test)
declar = t.expressionStatement(
t.assignmentExpression("=", left, stepValue),
);
} else if (t.isVariableDeclaration(left)) {
// for (let i of test)
declar = t.variableDeclaration(left.kind, [
t.variableDeclarator(left.declarations[0].id, stepValue),
]);
} else {
throw path
.get("left")
.buildCodeFrameError(
`Unknown node type ${left.type} in ForStatement`,
);
}
// ensure that it's a block so we can take all its statements
path.ensureBlock();
node.body.body.unshift(declar);
const nodes = builder.build({
CREATE_ITERATOR_HELPER: state.addHelper(builder.helper),
ITERATOR_HELPER: scope.generateUidIdentifier("iteratorHelper"),
STEP_KEY: t.identifier(stepKey),
OBJECT: node.right,
BODY: node.body,
});
const container = builder.getContainer(nodes);
t.inherits(container[0], node);
t.inherits(container[0].body, node.body);
if (t.isLabeledStatement(parent)) {
container[0] = t.labeledStatement(parent.label, container[0]);
path.parentPath.replaceWithMultiple(nodes);
path.remove();
} else {
path.replaceWithMultiple(nodes);
}
},
},
};
});