/
index.js
676 lines (556 loc) 路 19.3 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/* eslint max-len: 0 */
import type NodePath from "babel-traverse";
import type Scope from "babel-traverse";
import type File from "../../../file";
import traverse from "babel-traverse";
import { visitor as tdzVisitor } from "./tdz";
import * as t from "babel-types";
import values from "lodash/values";
import extend from "lodash/extend";
import template from "babel-template";
export default function () {
return {
visitor: {
VariableDeclaration(path, file) {
const { node, parent, scope } = path;
if (!isBlockScoped(node)) return;
convertBlockScopedToVar(path, null, parent, scope, true);
if (node._tdzThis) {
const nodes = [node];
for (let i = 0; i < node.declarations.length; i++) {
const decl = node.declarations[i];
if (decl.init) {
const assign = t.assignmentExpression("=", decl.id, decl.init);
assign._ignoreBlockScopingTDZ = true;
nodes.push(t.expressionStatement(assign));
}
decl.init = file.addHelper("temporalUndefined");
}
node._blockHoist = 2;
if (path.isCompletionRecord()) {
// ensure we don't break completion record semantics by returning
// the initialiser of the last declarator
nodes.push(t.expressionStatement(scope.buildUndefinedNode()));
}
path.replaceWithMultiple(nodes);
}
},
Loop(path, file) {
const { node, parent, scope } = path;
t.ensureBlock(node);
const blockScoping = new BlockScoping(path, path.get("body"), parent, scope, file);
const replace = blockScoping.run();
if (replace) path.replaceWith(replace);
},
CatchClause(path, file) {
const { parent, scope } = path;
const blockScoping = new BlockScoping(null, path.get("body"), parent, scope, file);
blockScoping.run();
},
"BlockStatement|SwitchStatement|Program"(path, file) {
if (!ignoreBlock(path)) {
const blockScoping = new BlockScoping(null, path, path.parent, path.scope, file);
blockScoping.run();
}
}
}
};
}
function ignoreBlock(path) {
return t.isLoop(path.parent) || t.isCatchClause(path.parent);
}
const buildRetCheck = template(`
if (typeof RETURN === "object") return RETURN.v;
`);
function isBlockScoped(node) {
if (!t.isVariableDeclaration(node)) return false;
if (node[t.BLOCK_SCOPED_SYMBOL]) return true;
if (node.kind !== "let" && node.kind !== "const") return false;
return true;
}
function convertBlockScopedToVar(path, node, parent, scope, moveBindingsToParent = false) {
if (!node) {
node = path.node;
}
// https://github.com/babel/babel/issues/255
if (!t.isFor(parent)) {
for (let i = 0; i < node.declarations.length; i++) {
const declar = node.declarations[i];
declar.init = declar.init || scope.buildUndefinedNode();
}
}
node[t.BLOCK_SCOPED_SYMBOL] = true;
node.kind = "var";
// Move bindings from current block scope to function scope.
if (moveBindingsToParent) {
const parentScope = scope.getFunctionParent();
const ids = path.getBindingIdentifiers();
for (const name in ids) {
const binding = scope.getOwnBinding(name);
if (binding) binding.kind = "var";
scope.moveBindingTo(name, parentScope);
}
}
}
function isVar(node) {
return t.isVariableDeclaration(node, { kind: "var" }) && !isBlockScoped(node);
}
const letReferenceBlockVisitor = traverse.visitors.merge([{
Function(path, state) {
path.traverse(letReferenceFunctionVisitor, state);
return path.skip();
}
}, tdzVisitor]);
const letReferenceFunctionVisitor = traverse.visitors.merge([{
ReferencedIdentifier(path, state) {
const ref = state.letReferences[path.node.name];
// not a part of our scope
if (!ref) return;
// this scope has a variable with the same name so it couldn't belong
// to our let scope
const localBinding = path.scope.getBindingIdentifier(path.node.name);
if (localBinding && localBinding !== ref) return;
state.closurify = true;
}
}, tdzVisitor]);
const hoistVarDeclarationsVisitor = {
enter(path, self) {
const { node, parent } = path;
if (path.isForStatement()) {
if (isVar(node.init, node)) {
const nodes = self.pushDeclar(node.init);
if (nodes.length === 1) {
node.init = nodes[0];
} else {
node.init = t.sequenceExpression(nodes);
}
}
} else if (path.isFor()) {
if (isVar(node.left, node)) {
self.pushDeclar(node.left);
node.left = node.left.declarations[0].id;
}
} else if (isVar(node, parent)) {
path.replaceWithMultiple(self.pushDeclar(node).map((expr) => t.expressionStatement(expr)));
} else if (path.isFunction()) {
return path.skip();
}
}
};
const loopLabelVisitor = {
LabeledStatement({ node }, state) {
state.innerLabels.push(node.label.name);
}
};
const continuationVisitor = {
enter(path, state) {
if (path.isAssignmentExpression() || path.isUpdateExpression()) {
const bindings = path.getBindingIdentifiers();
for (const name in bindings) {
if (state.outsideReferences[name] !== path.scope.getBindingIdentifier(name)) continue;
state.reassignments[name] = true;
}
}
}
};
function loopNodeTo(node) {
if (t.isBreakStatement(node)) {
return "break";
} else if (t.isContinueStatement(node)) {
return "continue";
}
}
const loopVisitor = {
Loop(path, state) {
const oldIgnoreLabeless = state.ignoreLabeless;
state.ignoreLabeless = true;
path.traverse(loopVisitor, state);
state.ignoreLabeless = oldIgnoreLabeless;
path.skip();
},
Function(path) {
path.skip();
},
SwitchCase(path, state) {
const oldInSwitchCase = state.inSwitchCase;
state.inSwitchCase = true;
path.traverse(loopVisitor, state);
state.inSwitchCase = oldInSwitchCase;
path.skip();
},
"BreakStatement|ContinueStatement|ReturnStatement"(path, state) {
const { node, parent, scope } = path;
if (node[this.LOOP_IGNORE]) return;
let replace;
let loopText = loopNodeTo(node);
if (loopText) {
if (node.label) {
// we shouldn't be transforming this because it exists somewhere inside
if (state.innerLabels.indexOf(node.label.name) >= 0) {
return;
}
loopText = `${loopText}|${node.label.name}`;
} else {
// we shouldn't be transforming these statements because
// they don't refer to the actual loop we're scopifying
if (state.ignoreLabeless) return;
//
if (state.inSwitchCase) return;
// break statements mean something different in this context
if (t.isBreakStatement(node) && t.isSwitchCase(parent)) return;
}
state.hasBreakContinue = true;
state.map[loopText] = node;
replace = t.stringLiteral(loopText);
}
if (path.isReturnStatement()) {
state.hasReturn = true;
replace = t.objectExpression([
t.objectProperty(t.identifier("v"), node.argument || scope.buildUndefinedNode())
]);
}
if (replace) {
replace = t.returnStatement(replace);
replace[this.LOOP_IGNORE] = true;
path.skip();
path.replaceWith(t.inherits(replace, node));
}
}
};
class BlockScoping {
constructor(loopPath?: NodePath, blockPath: NodePath, parent: Object, scope: Scope, file: File) {
this.parent = parent;
this.scope = scope;
this.file = file;
this.blockPath = blockPath;
this.block = blockPath.node;
this.outsideLetReferences = Object.create(null);
this.hasLetReferences = false;
this.letReferences = Object.create(null);
this.body = [];
if (loopPath) {
this.loopParent = loopPath.parent;
this.loopLabel = t.isLabeledStatement(this.loopParent) && this.loopParent.label;
this.loopPath = loopPath;
this.loop = loopPath.node;
}
}
/**
* Start the ball rolling.
*/
run() {
const block = this.block;
if (block._letDone) return;
block._letDone = true;
const needsClosure = this.getLetReferences();
// this is a block within a `Function/Program` so we can safely leave it be
if (t.isFunction(this.parent) || t.isProgram(this.block)) {
this.updateScopeInfo();
return;
}
// we can skip everything
if (!this.hasLetReferences) return;
if (needsClosure) {
this.wrapClosure();
} else {
this.remap();
}
this.updateScopeInfo(needsClosure);
if (this.loopLabel && !t.isLabeledStatement(this.loopParent)) {
return t.labeledStatement(this.loopLabel, this.loop);
}
}
updateScopeInfo(wrappedInClosure) {
const scope = this.scope;
const parentScope = scope.getFunctionParent();
const letRefs = this.letReferences;
for (const key in letRefs) {
const ref = letRefs[key];
const binding = scope.getBinding(ref.name);
if (!binding) continue;
if (binding.kind === "let" || binding.kind === "const") {
binding.kind = "var";
if (wrappedInClosure) {
scope.removeBinding(ref.name);
} else {
scope.moveBindingTo(ref.name, parentScope);
}
}
}
}
remap() {
const letRefs = this.letReferences;
const scope = this.scope;
// alright, so since we aren't wrapping this block in a closure
// we have to check if any of our let variables collide with
// those in upper scopes and then if they do, generate a uid
// for them and replace all references with it
for (const key in letRefs) {
// just an Identifier node we collected in `getLetReferences`
// this is the defining identifier of a declaration
const ref = letRefs[key];
// todo: could skip this if the colliding binding is in another function
if (scope.parentHasBinding(key) || scope.hasGlobal(key)) {
// The same identifier might have been bound separately in the block scope and
// the enclosing scope (e.g. loop or catch statement), so we should handle both
// individually
if (scope.hasOwnBinding(key))
scope.rename(ref.name);
if (this.blockPath.scope.hasOwnBinding(key))
this.blockPath.scope.rename(ref.name);
}
}
}
wrapClosure() {
if (this.file.opts.throwIfClosureRequired) {
throw this.blockPath.buildCodeFrameError(
"Variable declarations in this block can't be automatically rewritten " +
"without adding a closure -- refactor or avoid using let/const."
);
}
const block = this.block;
const outsideRefs = this.outsideLetReferences;
// remap loop heads with colliding variables
if (this.loop) {
for (const name in outsideRefs) {
const id = outsideRefs[name];
if (this.scope.hasGlobal(id.name) || this.scope.parentHasBinding(id.name)) {
delete outsideRefs[id.name];
delete this.letReferences[id.name];
this.scope.rename(id.name);
this.letReferences[id.name] = id;
outsideRefs[id.name] = id;
}
}
}
// if we're inside of a for loop then we search to see if there are any
// `break`s, `continue`s, `return`s etc
this.has = this.checkLoop();
// hoist let references to retain scope
this.hoistVarDeclarations();
// turn outsideLetReferences into an array
const params = values(outsideRefs);
const args = values(outsideRefs);
const isSwitch = this.blockPath.isSwitchStatement();
// build the closure that we're going to wrap the block with, possible wrapping switch(){}
const fn = t.functionExpression(null, params, t.blockStatement(isSwitch ? [block] : block.body));
fn.shadow = true;
// continuation
this.addContinuations(fn);
let ref = fn;
if (this.loop) {
ref = this.scope.generateUidIdentifier("loop");
this.loopPath.insertBefore(t.variableDeclaration("var", [
t.variableDeclarator(ref, fn)
]));
}
// build a call and a unique id that we can assign the return value to
let call = t.callExpression(ref, args);
const ret = this.scope.generateUidIdentifier("ret");
// handle generators
const hasYield = traverse.hasType(fn.body, this.scope, "YieldExpression", t.FUNCTION_TYPES);
if (hasYield) {
fn.generator = true;
call = t.yieldExpression(call, true);
}
// handlers async functions
const hasAsync = traverse.hasType(fn.body, this.scope, "AwaitExpression", t.FUNCTION_TYPES);
if (hasAsync) {
fn.async = true;
call = t.awaitExpression(call);
}
this.buildClosure(ret, call);
// replace the current block body with the one we're going to build
if (isSwitch) this.blockPath.replaceWithMultiple(this.body);
else block.body = this.body;
}
/**
* Push the closure to the body.
*/
buildClosure(ret: { type: "Identifier" }, call: { type: "CallExpression" }) {
const has = this.has;
if (has.hasReturn || has.hasBreakContinue) {
this.buildHas(ret, call);
} else {
this.body.push(t.expressionStatement(call));
}
}
/**
* If any of the outer let variables are reassigned then we need to rename them in
* the closure so we can get direct access to the outer variable to continue the
* iteration with bindings based on each iteration.
*
* Reference: https://github.com/babel/babel/issues/1078
*/
addContinuations(fn) {
const state = {
reassignments: {},
outsideReferences: this.outsideLetReferences
};
this.scope.traverse(fn, continuationVisitor, state);
for (let i = 0; i < fn.params.length; i++) {
const param = fn.params[i];
if (!state.reassignments[param.name]) continue;
const newParam = this.scope.generateUidIdentifier(param.name);
fn.params[i] = newParam;
this.scope.rename(param.name, newParam.name, fn);
// assign outer reference as it's been modified internally and needs to be retained
fn.body.body.push(t.expressionStatement(t.assignmentExpression("=", param, newParam)));
}
}
getLetReferences() {
const block = this.block;
let declarators = [];
if (this.loop) {
const init = this.loop.left || this.loop.init;
if (isBlockScoped(init)) {
declarators.push(init);
extend(this.outsideLetReferences, t.getBindingIdentifiers(init));
}
}
const addDeclarationsFromChild = (path, node) => {
node = node || path.node;
if (t.isClassDeclaration(node) || t.isFunctionDeclaration(node) || isBlockScoped(node)) {
if (isBlockScoped(node)) {
convertBlockScopedToVar(path, node, block, this.scope);
}
declarators = declarators.concat(node.declarations || node);
}
if (t.isLabeledStatement(node)) {
addDeclarationsFromChild(path.get("body"), node.body);
}
};
//
if (block.body) {
for (let i = 0; i < block.body.length; i++) {
const declarPath = this.blockPath.get("body")[i];
addDeclarationsFromChild(declarPath);
}
}
if (block.cases) {
for (let i = 0; i < block.cases.length; i++) {
const consequents = block.cases[i].consequent;
for (let j = 0; j < consequents.length; j++) {
const declarPath = this.blockPath.get("cases")[i];
const declar = consequents[j];
addDeclarationsFromChild(declarPath, declar);
}
}
}
//
for (let i = 0; i < declarators.length; i++) {
const declar = declarators[i];
// Passing true as the third argument causes t.getBindingIdentifiers
// to return only the *outer* binding identifiers of this
// declaration, rather than (for example) mistakenly including the
// parameters of a function declaration. Fixes #4880.
const keys = t.getBindingIdentifiers(declar, false, true);
extend(this.letReferences, keys);
this.hasLetReferences = true;
}
// no let references so we can just quit
if (!this.hasLetReferences) return;
const state = {
letReferences: this.letReferences,
closurify: false,
file: this.file
};
// traverse through this block, stopping on functions and checking if they
// contain any local let references
this.blockPath.traverse(letReferenceBlockVisitor, state);
return state.closurify;
}
/**
* If we're inside of a loop then traverse it and check if it has one of
* the following node types `ReturnStatement`, `BreakStatement`,
* `ContinueStatement` and replace it with a return value that we can track
* later on.
*/
checkLoop(): Object {
const state = {
hasBreakContinue: false,
ignoreLabeless: false,
inSwitchCase: false,
innerLabels: [],
hasReturn: false,
isLoop: !!this.loop,
map: {},
LOOP_IGNORE: Symbol()
};
this.blockPath.traverse(loopLabelVisitor, state);
this.blockPath.traverse(loopVisitor, state);
return state;
}
/**
* Hoist all let declarations in this block to before it so they retain scope
* once we wrap everything in a closure.
*/
hoistVarDeclarations() {
this.blockPath.traverse(hoistVarDeclarationsVisitor, this);
}
/**
* Turn a `VariableDeclaration` into an array of `AssignmentExpressions` with
* their declarations hoisted to before the closure wrapper.
*/
pushDeclar(node: { type: "VariableDeclaration" }): Array<Object> {
const declars = [];
const names = t.getBindingIdentifiers(node);
for (const name in names) {
declars.push(t.variableDeclarator(names[name]));
}
this.body.push(t.variableDeclaration(node.kind, declars));
const replace = [];
for (let i = 0; i < node.declarations.length; i++) {
const declar = node.declarations[i];
if (!declar.init) continue;
const expr = t.assignmentExpression("=", declar.id, declar.init);
replace.push(t.inherits(expr, declar));
}
return replace;
}
buildHas(ret: { type: "Identifier" }, call: { type: "CallExpression" }) {
const body = this.body;
body.push(t.variableDeclaration("var", [
t.variableDeclarator(ret, call)
]));
let retCheck;
const has = this.has;
const cases = [];
if (has.hasReturn) {
// typeof ret === "object"
retCheck = buildRetCheck({
RETURN: ret
});
}
if (has.hasBreakContinue) {
for (const key in has.map) {
cases.push(t.switchCase(t.stringLiteral(key), [has.map[key]]));
}
if (has.hasReturn) {
cases.push(t.switchCase(null, [retCheck]));
}
if (cases.length === 1) {
const single = cases[0];
body.push(t.ifStatement(
t.binaryExpression("===", ret, single.test),
single.consequent[0]
));
} else {
if (this.loop) {
// https://github.com/babel/babel/issues/998
for (let i = 0; i < cases.length; i++) {
const caseConsequent = cases[i].consequent[0];
if (t.isBreakStatement(caseConsequent) && !caseConsequent.label) {
caseConsequent.label = this.loopLabel = this.loopLabel || this.scope.generateUidIdentifier("loop");
}
}
}
body.push(t.switchStatement(ret, cases));
}
} else {
if (has.hasReturn) {
body.push(retCheck);
}
}
}
}