-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
971 lines (812 loc) · 24.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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
import includes from "lodash/includes";
import repeat from "lodash/repeat";
import Renamer from "./lib/renamer";
import type NodePath from "../path";
import traverse from "../index";
import defaults from "lodash/defaults";
import Binding from "./binding";
import globals from "globals";
import * as t from "@babel/types";
import { scope as scopeCache } from "../cache";
// Recursively gathers the identifying names of a node.
function gatherNodeParts(node: Object, parts: Array) {
if (t.isModuleDeclaration(node)) {
if (node.source) {
gatherNodeParts(node.source, parts);
} else if (node.specifiers && node.specifiers.length) {
for (const specifier of (node.specifiers: Array)) {
gatherNodeParts(specifier, parts);
}
} else if (node.declaration) {
gatherNodeParts(node.declaration, parts);
}
} else if (t.isModuleSpecifier(node)) {
gatherNodeParts(node.local, parts);
} else if (t.isMemberExpression(node)) {
gatherNodeParts(node.object, parts);
gatherNodeParts(node.property, parts);
} else if (t.isIdentifier(node)) {
parts.push(node.name);
} else if (t.isLiteral(node)) {
parts.push(node.value);
} else if (t.isCallExpression(node)) {
gatherNodeParts(node.callee, parts);
} else if (t.isObjectExpression(node) || t.isObjectPattern(node)) {
for (const prop of (node.properties: Array)) {
gatherNodeParts(prop.key || prop.argument, parts);
}
} else if (t.isPrivateName(node)) {
gatherNodeParts(node.id, parts);
} else if (t.isThisExpression(node)) {
parts.push("this");
} else if (t.isSuper(node)) {
parts.push("super");
}
}
//
const collectorVisitor = {
For(path) {
for (const key of (t.FOR_INIT_KEYS: Array)) {
const declar = path.get(key);
if (declar.isVar()) {
const parentScope =
path.scope.getFunctionParent() || path.scope.getProgramParent();
parentScope.registerBinding("var", declar);
}
}
},
Declaration(path) {
// delegate block scope handling to the `BlockScoped` method
if (path.isBlockScoped()) return;
// this will be hit again once we traverse into it after this iteration
if (path.isExportDeclaration() && path.get("declaration").isDeclaration()) {
return;
}
// we've ran into a declaration!
const parent =
path.scope.getFunctionParent() || path.scope.getProgramParent();
parent.registerDeclaration(path);
},
ReferencedIdentifier(path, state) {
state.references.push(path);
},
ForXStatement(path, state) {
const left = path.get("left");
if (left.isPattern() || left.isIdentifier()) {
state.constantViolations.push(path);
}
},
ExportDeclaration: {
exit(path) {
const { node, scope } = path;
const declar = node.declaration;
if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar)) {
const id = declar.id;
if (!id) return;
const binding = scope.getBinding(id.name);
if (binding) binding.reference(path);
} else if (t.isVariableDeclaration(declar)) {
for (const decl of (declar.declarations: Array<Object>)) {
for (const name of Object.keys(t.getBindingIdentifiers(decl))) {
const binding = scope.getBinding(name);
if (binding) binding.reference(path);
}
}
}
},
},
LabeledStatement(path) {
path.scope.getProgramParent().addGlobal(path.node);
path.scope.getBlockParent().registerDeclaration(path);
},
AssignmentExpression(path, state) {
state.assignments.push(path);
},
UpdateExpression(path, state) {
state.constantViolations.push(path);
},
UnaryExpression(path, state) {
if (path.node.operator === "delete") {
state.constantViolations.push(path);
}
},
BlockScoped(path) {
let scope = path.scope;
if (scope.path === path) scope = scope.parent;
scope.getBlockParent().registerDeclaration(path);
},
ClassDeclaration(path) {
const id = path.node.id;
if (!id) return;
const name = id.name;
path.scope.bindings[name] = path.scope.getBinding(name);
},
Block(path) {
const paths = path.get("body");
for (const bodyPath of (paths: Array)) {
if (bodyPath.isFunctionDeclaration()) {
path.scope.getBlockParent().registerDeclaration(bodyPath);
}
}
},
};
let uid = 0;
export default class Scope {
/**
* This searches the current "scope" and collects all references/bindings
* within.
*/
constructor(path: NodePath) {
const { node } = path;
const cached = scopeCache.get(node);
// Sometimes, a scopable path is placed higher in the AST tree.
// In these cases, have to create a new Scope.
if (cached && cached.path === path) {
return cached;
}
scopeCache.set(node, this);
this.uid = uid++;
this.block = node;
this.path = path;
this.labels = new Map();
}
/**
* Globals.
*/
static globals = Object.keys(globals.builtin);
/**
* Variables available in current context.
*/
static contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
get parent() {
const parent = this.path.findParent(p => p.isScope());
return parent && parent.scope;
}
get parentBlock() {
return this.path.parent;
}
get hub() {
return this.path.hub;
}
/**
* Traverse node with current scope and path.
*/
traverse(node: Object, opts: Object, state?) {
traverse(node, opts, this, state, this.path);
}
/**
* Generate a unique identifier and add it to the current scope.
*/
generateDeclaredUidIdentifier(name?: string) {
const id = this.generateUidIdentifier(name);
this.push({ id });
return t.cloneNode(id);
}
/**
* Generate a unique identifier.
*/
generateUidIdentifier(name?: string) {
return t.identifier(this.generateUid(name));
}
/**
* Generate a unique `_id1` binding.
*/
generateUid(name: string = "temp") {
name = t
.toIdentifier(name)
.replace(/^_+/, "")
.replace(/[0-9]+$/g, "");
let uid;
let i = 0;
do {
uid = this._generateUid(name, i);
i++;
} while (
this.hasLabel(uid) ||
this.hasBinding(uid) ||
this.hasGlobal(uid) ||
this.hasReference(uid)
);
const program = this.getProgramParent();
program.references[uid] = true;
program.uids[uid] = true;
return uid;
}
/**
* Generate an `_id1`.
*/
_generateUid(name, i) {
let id = name;
if (i > 1) id += i;
return `_${id}`;
}
generateUidBasedOnNode(parent: Object, defaultName?: String) {
let node = parent;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
} else if (t.isObjectProperty(node) || t.isObjectMethod(node)) {
node = node.key;
}
const parts = [];
gatherNodeParts(node, parts);
let id = parts.join("$");
id = id.replace(/^_/, "") || defaultName || "ref";
return this.generateUid(id.slice(0, 20));
}
/**
* Generate a unique identifier based on a node.
*/
generateUidIdentifierBasedOnNode(
parent: Object,
defaultName?: String,
): Object {
return t.identifier(this.generateUidBasedOnNode(parent, defaultName));
}
/**
* Determine whether evaluating the specific input `node` is a consequenceless reference. ie.
* evaluating it wont result in potentially arbitrary code from being ran. The following are
* whitelisted and determined not to cause side effects:
*
* - `this` expressions
* - `super` expressions
* - Bound identifiers
*/
isStatic(node: Object): boolean {
if (t.isThisExpression(node) || t.isSuper(node)) {
return true;
}
if (t.isIdentifier(node)) {
const binding = this.getBinding(node.name);
if (binding) {
return binding.constant;
} else {
return this.hasBinding(node.name);
}
}
return false;
}
/**
* Possibly generate a memoised identifier if it is not static and has consequences.
*/
maybeGenerateMemoised(node: Object, dontPush?: boolean): ?Object {
if (this.isStatic(node)) {
return null;
} else {
const id = this.generateUidIdentifierBasedOnNode(node);
if (!dontPush) {
this.push({ id });
return t.cloneNode(id);
}
return id;
}
}
checkBlockScopedCollisions(local, kind: string, name: string, id: Object) {
// ignore parameters
if (kind === "param") return;
// Ignore existing binding if it's the name of the current function or
// class expression
if (local.kind === "local") return;
const duplicate =
// don't allow duplicate bindings to exist alongside
kind === "let" ||
local.kind === "let" ||
local.kind === "const" ||
local.kind === "module" ||
// don't allow a local of param with a kind of let
(local.kind === "param" && (kind === "let" || kind === "const"));
if (duplicate) {
throw this.hub.buildError(
id,
`Duplicate declaration "${name}"`,
TypeError,
);
}
}
rename(oldName: string, newName: string, block?) {
const binding = this.getBinding(oldName);
if (binding) {
newName = newName || this.generateUidIdentifier(oldName).name;
return new Renamer(binding, oldName, newName).rename(block);
}
}
_renameFromMap(map, oldName, newName, value) {
if (map[oldName]) {
map[newName] = value;
map[oldName] = null;
}
}
dump() {
const sep = repeat("-", 60);
console.log(sep);
let scope = this;
do {
console.log("#", scope.block.type);
for (const name of Object.keys(scope.bindings)) {
const binding = scope.bindings[name];
console.log(" -", name, {
constant: binding.constant,
references: binding.references,
violations: binding.constantViolations.length,
kind: binding.kind,
});
}
} while ((scope = scope.parent));
console.log(sep);
}
toArray(node: Object, i?: number) {
if (t.isIdentifier(node)) {
const binding = this.getBinding(node.name);
if (binding && binding.constant && binding.path.isGenericType("Array")) {
return node;
}
}
if (t.isArrayExpression(node)) {
return node;
}
if (t.isIdentifier(node, { name: "arguments" })) {
return t.callExpression(
t.memberExpression(
t.memberExpression(
t.memberExpression(
t.identifier("Array"),
t.identifier("prototype"),
),
t.identifier("slice"),
),
t.identifier("call"),
),
[node],
);
}
let helperName;
const args = [node];
if (i === true) {
// Used in array-spread to create an array.
helperName = "toConsumableArray";
} else if (i) {
args.push(t.numericLiteral(i));
// Used in array-rest to create an array from a subset of an iterable.
helperName = "slicedToArray";
// TODO if (this.hub.isLoose("es6.forOf")) helperName += "-loose";
} else {
// Used in array-rest to create an array
helperName = "toArray";
}
return t.callExpression(this.hub.addHelper(helperName), args);
}
hasLabel(name: string) {
return !!this.getLabel(name);
}
getLabel(name: string) {
return this.labels.get(name);
}
registerLabel(path: NodePath) {
this.labels.set(path.node.label.name, path);
}
registerDeclaration(path: NodePath) {
if (path.isLabeledStatement()) {
this.registerLabel(path);
} else if (path.isFunctionDeclaration()) {
this.registerBinding("hoisted", path.get("id"), path);
} else if (path.isVariableDeclaration()) {
const declarations = path.get("declarations");
for (const declar of (declarations: Array)) {
this.registerBinding(path.node.kind, declar);
}
} else if (path.isClassDeclaration()) {
this.registerBinding("let", path);
} else if (path.isImportDeclaration()) {
const specifiers = path.get("specifiers");
for (const specifier of (specifiers: Array)) {
this.registerBinding("module", specifier);
}
} else if (path.isExportDeclaration()) {
const declar = path.get("declaration");
if (
declar.isClassDeclaration() ||
declar.isFunctionDeclaration() ||
declar.isVariableDeclaration()
) {
this.registerDeclaration(declar);
}
} else {
this.registerBinding("unknown", path);
}
}
buildUndefinedNode() {
return t.unaryExpression("void", t.numericLiteral(0), true);
}
registerConstantViolation(path: NodePath) {
const ids = path.getBindingIdentifiers();
for (const name of Object.keys(ids)) {
const binding = this.getBinding(name);
if (binding) binding.reassign(path);
}
}
registerBinding(kind: string, path: NodePath, bindingPath = path) {
if (!kind) throw new ReferenceError("no `kind`");
if (path.isVariableDeclaration()) {
const declarators: Array<NodePath> = path.get("declarations");
for (const declar of declarators) {
this.registerBinding(kind, declar);
}
return;
}
const parent = this.getProgramParent();
const ids = path.getOuterBindingIdentifiers(true);
for (const name of Object.keys(ids)) {
for (const id of (ids[name]: Array<Object>)) {
const local = this.getOwnBinding(name);
if (local) {
// same identifier so continue safely as we're likely trying to register it
// multiple times
if (local.identifier === id) continue;
this.checkBlockScopedCollisions(local, kind, name, id);
}
parent.references[name] = true;
// A redeclaration of an existing variable is a modification
if (local) {
this.registerConstantViolation(bindingPath);
} else {
this.bindings[name] = new Binding({
identifier: id,
scope: this,
path: bindingPath,
kind: kind,
});
}
}
}
}
addGlobal(node: Object) {
this.globals[node.name] = node;
}
hasUid(name): boolean {
let scope = this;
do {
if (scope.uids[name]) return true;
} while ((scope = scope.parent));
return false;
}
hasGlobal(name: string): boolean {
let scope = this;
do {
if (scope.globals[name]) return true;
} while ((scope = scope.parent));
return false;
}
hasReference(name: string): boolean {
let scope = this;
do {
if (scope.references[name]) return true;
} while ((scope = scope.parent));
return false;
}
isPure(node, constantsOnly?: boolean) {
if (t.isIdentifier(node)) {
const binding = this.getBinding(node.name);
if (!binding) return false;
if (constantsOnly) return binding.constant;
return true;
} else if (t.isClass(node)) {
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
return false;
}
return this.isPure(node.body, constantsOnly);
} else if (t.isClassBody(node)) {
for (const method of node.body) {
if (!this.isPure(method, constantsOnly)) return false;
}
return true;
} else if (t.isBinary(node)) {
return (
this.isPure(node.left, constantsOnly) &&
this.isPure(node.right, constantsOnly)
);
} else if (t.isArrayExpression(node)) {
for (const elem of (node.elements: Array<Object>)) {
if (!this.isPure(elem, constantsOnly)) return false;
}
return true;
} else if (t.isObjectExpression(node)) {
for (const prop of (node.properties: Array<Object>)) {
if (!this.isPure(prop, constantsOnly)) return false;
}
return true;
} else if (t.isClassMethod(node)) {
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
if (node.kind === "get" || node.kind === "set") return false;
return true;
} else if (t.isProperty(node)) {
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
return this.isPure(node.value, constantsOnly);
} else if (t.isUnaryExpression(node)) {
return this.isPure(node.argument, constantsOnly);
} else if (t.isTaggedTemplateExpression(node)) {
return (
t.matchesPattern(node.tag, "String.raw") &&
!this.hasBinding("String", true) &&
this.isPure(node.quasi, constantsOnly)
);
} else if (t.isTemplateLiteral(node)) {
for (const expression of (node.expressions: Array<Object>)) {
if (!this.isPure(expression, constantsOnly)) return false;
}
return true;
} else {
return t.isPureish(node);
}
}
/**
* Set some arbitrary data on the current scope.
*/
setData(key, val) {
return (this.data[key] = val);
}
/**
* Recursively walk up scope tree looking for the data `key`.
*/
getData(key) {
let scope = this;
do {
const data = scope.data[key];
if (data != null) return data;
} while ((scope = scope.parent));
}
/**
* Recursively walk up scope tree looking for the data `key` and if it exists,
* remove it.
*/
removeData(key) {
let scope = this;
do {
const data = scope.data[key];
if (data != null) scope.data[key] = null;
} while ((scope = scope.parent));
}
init() {
if (!this.references) this.crawl();
}
crawl() {
const path = this.path;
this.references = Object.create(null);
this.bindings = Object.create(null);
this.globals = Object.create(null);
this.uids = Object.create(null);
this.data = Object.create(null);
// ForStatement - left, init
if (path.isLoop()) {
for (const key of (t.FOR_INIT_KEYS: Array<string>)) {
const node = path.get(key);
if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
}
}
// FunctionExpression - id
if (path.isFunctionExpression() && path.has("id")) {
if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
this.registerBinding("local", path.get("id"), path);
}
}
// Class
if (path.isClassExpression() && path.has("id")) {
if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
this.registerBinding("local", path);
}
}
// Function - params, rest
if (path.isFunction()) {
const params: Array<NodePath> = path.get("params");
for (const param of params) {
this.registerBinding("param", param);
}
}
// CatchClause - param
if (path.isCatchClause()) {
this.registerBinding("let", path);
}
// Program
const parent = this.getProgramParent();
if (parent.crawling) return;
const state = {
references: [],
constantViolations: [],
assignments: [],
};
this.crawling = true;
path.traverse(collectorVisitor, state);
this.crawling = false;
// register assignments
for (const path of state.assignments) {
// register undeclared bindings as globals
const ids = path.getBindingIdentifiers();
let programParent;
for (const name of Object.keys(ids)) {
if (path.scope.getBinding(name)) continue;
programParent = programParent || path.scope.getProgramParent();
programParent.addGlobal(ids[name]);
}
// register as constant violation
path.scope.registerConstantViolation(path);
}
// register references
for (const ref of state.references) {
const binding = ref.scope.getBinding(ref.node.name);
if (binding) {
binding.reference(ref);
} else {
ref.scope.getProgramParent().addGlobal(ref.node);
}
}
// register constant violations
for (const path of state.constantViolations) {
path.scope.registerConstantViolation(path);
}
}
push(opts: {
id: Object,
init: ?Object,
unique: ?boolean,
_blockHoist: ?number,
kind: "var" | "let",
}) {
let path = this.path;
if (!path.isBlockStatement() && !path.isProgram()) {
path = this.getBlockParent().path;
}
if (path.isSwitchStatement()) {
path = (this.getFunctionParent() || this.getProgramParent()).path;
}
if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
path.ensureBlock();
path = path.get("body");
}
const unique = opts.unique;
const kind = opts.kind || "var";
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
const dataKey = `declaration:${kind}:${blockHoist}`;
let declarPath = !unique && path.getData(dataKey);
if (!declarPath) {
const declar = t.variableDeclaration(kind, []);
declar._blockHoist = blockHoist;
[declarPath] = path.unshiftContainer("body", [declar]);
if (!unique) path.setData(dataKey, declarPath);
}
const declarator = t.variableDeclarator(opts.id, opts.init);
declarPath.node.declarations.push(declarator);
this.registerBinding(kind, declarPath.get("declarations").pop());
}
/**
* Walk up to the top of the scope tree and get the `Program`.
*/
getProgramParent() {
let scope = this;
do {
if (scope.path.isProgram()) {
return scope;
}
} while ((scope = scope.parent));
throw new Error("Couldn't find a Program");
}
/**
* Walk up the scope tree until we hit either a Function or return null.
*/
getFunctionParent() {
let scope = this;
do {
if (scope.path.isFunctionParent()) {
return scope;
}
} while ((scope = scope.parent));
return null;
}
/**
* Walk up the scope tree until we hit either a BlockStatement/Loop/Program/Function/Switch or reach the
* very top and hit Program.
*/
getBlockParent() {
let scope = this;
do {
if (scope.path.isBlockParent()) {
return scope;
}
} while ((scope = scope.parent));
throw new Error(
"We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...",
);
}
/**
* Walks the scope tree and gathers **all** bindings.
*/
getAllBindings(): Object {
const ids = Object.create(null);
let scope = this;
do {
defaults(ids, scope.bindings);
scope = scope.parent;
} while (scope);
return ids;
}
/**
* Walks the scope tree and gathers all declarations of `kind`.
*/
getAllBindingsOfKind(): Object {
const ids = Object.create(null);
for (const kind of (arguments: Array)) {
let scope = this;
do {
for (const name of Object.keys(scope.bindings)) {
const binding = scope.bindings[name];
if (binding.kind === kind) ids[name] = binding;
}
scope = scope.parent;
} while (scope);
}
return ids;
}
bindingIdentifierEquals(name: string, node: Object): boolean {
return this.getBindingIdentifier(name) === node;
}
getBinding(name: string) {
let scope = this;
do {
const binding = scope.getOwnBinding(name);
if (binding) return binding;
} while ((scope = scope.parent));
}
getOwnBinding(name: string) {
return this.bindings[name];
}
getBindingIdentifier(name: string) {
const info = this.getBinding(name);
return info && info.identifier;
}
getOwnBindingIdentifier(name: string) {
const binding = this.bindings[name];
return binding && binding.identifier;
}
hasOwnBinding(name: string) {
return !!this.getOwnBinding(name);
}
hasBinding(name: string, noGlobals?) {
if (!name) return false;
if (this.hasOwnBinding(name)) return true;
if (this.parentHasBinding(name, noGlobals)) return true;
if (this.hasUid(name)) return true;
if (!noGlobals && includes(Scope.globals, name)) return true;
if (!noGlobals && includes(Scope.contextVariables, name)) return true;
return false;
}
parentHasBinding(name: string, noGlobals?) {
return this.parent && this.parent.hasBinding(name, noGlobals);
}
/**
* Move a binding of `name` to another `scope`.
*/
moveBindingTo(name, scope) {
const info = this.getBinding(name);
if (info) {
info.scope.removeOwnBinding(name);
info.scope = scope;
scope.bindings[name] = info;
}
}
removeOwnBinding(name: string) {
delete this.bindings[name];
}
removeBinding(name: string) {
// clear literal binding
const info = this.getBinding(name);
if (info) {
info.scope.removeOwnBinding(name);
}
// clear uids with this name - https://github.com/babel/babel/issues/2101
let scope = this;
do {
if (scope.uids[name]) {
scope.uids[name] = false;
}
} while ((scope = scope.parent));
}
}