-
-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathattachScopes.ts
More file actions
executable file
·143 lines (118 loc) · 3.67 KB
/
Copy pathattachScopes.ts
File metadata and controls
executable file
·143 lines (118 loc) · 3.67 KB
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
import type * as estree from 'estree';
import { walk } from 'estree-walker';
import type { AttachedScope, AttachScopes } from '../types';
import extractAssignedNames from './extractAssignedNames';
interface BlockDeclaration {
[index: string]: boolean;
}
const blockDeclarations: BlockDeclaration = {
const: true,
let: true
};
interface ScopeOptions {
parent?: AttachedScope;
block?: boolean;
params?: estree.Node[];
}
class Scope implements AttachedScope {
parent?: AttachedScope;
isBlockScope: boolean;
declarations: { [key: string]: boolean };
constructor(options: ScopeOptions = {}) {
this.parent = options.parent;
this.isBlockScope = !!options.block;
this.declarations = Object.create(null);
if (options.params) {
options.params.forEach((param) => {
extractAssignedNames(param).forEach((name) => {
this.declarations[name] = true;
});
});
}
}
addDeclaration(node: estree.Node, isBlockDeclaration: boolean, isVar: boolean): void {
if (!isBlockDeclaration && this.isBlockScope) {
// it's a `var` or function node, and this
// is a block scope, so we need to go up
this.parent!.addDeclaration(node, isBlockDeclaration, isVar);
} else if ((node as any).id) {
extractAssignedNames((node as any).id).forEach((name) => {
this.declarations[name] = true;
});
}
}
contains(name: string): boolean {
return this.declarations[name] || (this.parent ? this.parent.contains(name) : false);
}
}
const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'scope') {
let scope = new Scope();
walk(ast, {
enter(n, parent) {
const node = n as estree.Node;
// function foo () {...}
// class Foo {...}
if (/(?:Function|Class)Declaration/.test(node.type)) {
scope.addDeclaration(node, false, false);
}
// var foo = 1
if (node.type === 'VariableDeclaration') {
const { kind } = node;
const isBlockDeclaration = blockDeclarations[kind];
node.declarations.forEach((declaration) => {
scope.addDeclaration(declaration, isBlockDeclaration, true);
});
}
let newScope: AttachedScope | undefined;
// create new function scope
if (node.type.includes('Function')) {
const func = node as estree.Function;
newScope = new Scope({
parent: scope,
block: false,
params: func.params
});
// named function expressions - the name is considered
// part of the function's scope
if (func.type === 'FunctionExpression' && func.id) {
newScope.addDeclaration(func, false, false);
}
}
// create new for scope
if (/For(?:In|Of)?Statement/.test(node.type)) {
newScope = new Scope({
parent: scope,
block: true
});
}
// create new block scope
if (node.type === 'BlockStatement' && !parent.type.includes('Function')) {
newScope = new Scope({
parent: scope,
block: true
});
}
// catch clause has its own block scope
if (node.type === 'CatchClause') {
newScope = new Scope({
parent: scope,
params: node.param ? [node.param] : [],
block: true
});
}
if (newScope) {
Object.defineProperty(node, propertyName, {
value: newScope,
configurable: true
});
scope = newScope;
}
},
leave(n) {
const node = n as estree.Node & Record<string, any>;
if (node[propertyName]) scope = scope.parent!;
}
});
return scope;
};
export { attachScopes as default };