-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ChildScope.ts
62 lines (54 loc) · 1.92 KB
/
ChildScope.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
import { NameCollection } from '../../utils/reservedNames';
import { getSafeName } from '../../utils/safeName';
import { ExpressionEntity } from '../nodes/shared/Expression';
import Variable from '../variables/Variable';
import Scope from './Scope';
export default class ChildScope extends Scope {
accessedOutsideVariables: { [name: string]: Variable } = Object.create(null);
parent: Scope;
constructor(parent: Scope) {
super();
this.parent = parent;
parent.children.push(this);
}
addNamespaceMemberAccess(name: string, variable: Variable) {
this.accessedOutsideVariables[name] = variable;
if (this.parent instanceof ChildScope) {
this.parent.addNamespaceMemberAccess(name, variable);
}
}
addReturnExpression(expression: ExpressionEntity) {
this.parent instanceof ChildScope && this.parent.addReturnExpression(expression);
}
contains(name: string): boolean {
return name in this.variables || this.parent.contains(name);
}
deconflict(forbiddenNames: NameCollection) {
const usedNames: NameCollection = Object.assign(Object.create(null), forbiddenNames);
for (const name of Object.keys(this.accessedOutsideVariables)) {
const variable = this.accessedOutsideVariables[name];
if (variable.included) {
usedNames[variable.getBaseVariableName()] = true;
}
}
for (const name of Object.keys(this.variables)) {
const variable = this.variables[name];
if (variable.included) {
variable.setSafeName(getSafeName(name, usedNames));
}
}
for (const scope of this.children) {
scope.deconflict(forbiddenNames);
}
}
findLexicalBoundary(): ChildScope {
return this.parent instanceof ChildScope ? this.parent.findLexicalBoundary() : this;
}
findVariable(name: string): Variable {
const knownVariable = this.variables[name] || this.accessedOutsideVariables[name];
if (knownVariable) {
return knownVariable;
}
return (this.accessedOutsideVariables[name] = this.parent.findVariable(name));
}
}