1 | var types = require("./types");
|
2 | var Type = types.Type;
|
3 | var namedTypes = types.namedTypes;
|
4 | var Node = namedTypes.Node;
|
5 | var Expression = namedTypes.Expression;
|
6 | var isArray = types.builtInTypes.array;
|
7 | var hasOwn = Object.prototype.hasOwnProperty;
|
8 | var b = types.builders;
|
9 |
|
10 | function Scope(path, parentScope) {
|
11 | if (!(this instanceof Scope)) {
|
12 | throw new Error("Scope constructor cannot be invoked without 'new'");
|
13 | }
|
14 | if (!(path instanceof require("./node-path"))) {
|
15 | throw new Error("");
|
16 | }
|
17 | ScopeType.assert(path.value);
|
18 |
|
19 | var depth;
|
20 |
|
21 | if (parentScope) {
|
22 | if (!(parentScope instanceof Scope)) {
|
23 | throw new Error("");
|
24 | }
|
25 | depth = parentScope.depth + 1;
|
26 | } else {
|
27 | parentScope = null;
|
28 | depth = 0;
|
29 | }
|
30 |
|
31 | Object.defineProperties(this, {
|
32 | path: { value: path },
|
33 | node: { value: path.value },
|
34 | isGlobal: { value: !parentScope, enumerable: true },
|
35 | depth: { value: depth },
|
36 | parent: { value: parentScope },
|
37 | bindings: { value: {} },
|
38 | types: { value: {} },
|
39 | });
|
40 | }
|
41 |
|
42 | var scopeTypes = [
|
43 |
|
44 | namedTypes.Program,
|
45 |
|
46 |
|
47 |
|
48 | namedTypes.Function,
|
49 |
|
50 |
|
51 |
|
52 | namedTypes.CatchClause
|
53 | ];
|
54 |
|
55 | var ScopeType = Type.or.apply(Type, scopeTypes);
|
56 |
|
57 | Scope.isEstablishedBy = function(node) {
|
58 | return ScopeType.check(node);
|
59 | };
|
60 |
|
61 | var Sp = Scope.prototype;
|
62 |
|
63 |
|
64 | Sp.didScan = false;
|
65 |
|
66 | Sp.declares = function(name) {
|
67 | this.scan();
|
68 | return hasOwn.call(this.bindings, name);
|
69 | };
|
70 |
|
71 | Sp.declaresType = function(name) {
|
72 | this.scan();
|
73 | return hasOwn.call(this.types, name);
|
74 | };
|
75 |
|
76 | Sp.declareTemporary = function(prefix) {
|
77 | if (prefix) {
|
78 | if (!/^[a-z$_]/i.test(prefix)) {
|
79 | throw new Error("");
|
80 | }
|
81 | } else {
|
82 | prefix = "t$";
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 | prefix += this.depth.toString(36) + "$";
|
88 |
|
89 | this.scan();
|
90 |
|
91 | var index = 0;
|
92 | while (this.declares(prefix + index)) {
|
93 | ++index;
|
94 | }
|
95 |
|
96 | var name = prefix + index;
|
97 | return this.bindings[name] = types.builders.identifier(name);
|
98 | };
|
99 |
|
100 | Sp.injectTemporary = function(identifier, init) {
|
101 | identifier || (identifier = this.declareTemporary());
|
102 |
|
103 | var bodyPath = this.path.get("body");
|
104 | if (namedTypes.BlockStatement.check(bodyPath.value)) {
|
105 | bodyPath = bodyPath.get("body");
|
106 | }
|
107 |
|
108 | bodyPath.unshift(
|
109 | b.variableDeclaration(
|
110 | "var",
|
111 | [b.variableDeclarator(identifier, init || null)]
|
112 | )
|
113 | );
|
114 |
|
115 | return identifier;
|
116 | };
|
117 |
|
118 | Sp.scan = function(force) {
|
119 | if (force || !this.didScan) {
|
120 | for (var name in this.bindings) {
|
121 |
|
122 | delete this.bindings[name];
|
123 | }
|
124 | scanScope(this.path, this.bindings, this.types);
|
125 | this.didScan = true;
|
126 | }
|
127 | };
|
128 |
|
129 | Sp.getBindings = function () {
|
130 | this.scan();
|
131 | return this.bindings;
|
132 | };
|
133 |
|
134 | Sp.getTypes = function () {
|
135 | this.scan();
|
136 | return this.types;
|
137 | };
|
138 |
|
139 | function scanScope(path, bindings, scopeTypes) {
|
140 | var node = path.value;
|
141 | ScopeType.assert(node);
|
142 |
|
143 | if (namedTypes.CatchClause.check(node)) {
|
144 |
|
145 |
|
146 |
|
147 | addPattern(path.get("param"), bindings);
|
148 |
|
149 | } else {
|
150 | recursiveScanScope(path, bindings, scopeTypes);
|
151 | }
|
152 | }
|
153 |
|
154 | function recursiveScanScope(path, bindings, scopeTypes) {
|
155 | var node = path.value;
|
156 |
|
157 | if (path.parent &&
|
158 | namedTypes.FunctionExpression.check(path.parent.node) &&
|
159 | path.parent.node.id) {
|
160 | addPattern(path.parent.get("id"), bindings);
|
161 | }
|
162 |
|
163 | if (!node) {
|
164 |
|
165 |
|
166 | } else if (isArray.check(node)) {
|
167 | path.each(function(childPath) {
|
168 | recursiveScanChild(childPath, bindings, scopeTypes);
|
169 | });
|
170 |
|
171 | } else if (namedTypes.Function.check(node)) {
|
172 | path.get("params").each(function(paramPath) {
|
173 | addPattern(paramPath, bindings);
|
174 | });
|
175 |
|
176 | recursiveScanChild(path.get("body"), bindings, scopeTypes);
|
177 |
|
178 | } else if (namedTypes.TypeAlias && namedTypes.TypeAlias.check(node)) {
|
179 | addTypePattern(path.get("id"), scopeTypes);
|
180 |
|
181 | } else if (namedTypes.VariableDeclarator.check(node)) {
|
182 | addPattern(path.get("id"), bindings);
|
183 | recursiveScanChild(path.get("init"), bindings, scopeTypes);
|
184 |
|
185 | } else if (node.type === "ImportSpecifier" ||
|
186 | node.type === "ImportNamespaceSpecifier" ||
|
187 | node.type === "ImportDefaultSpecifier") {
|
188 | addPattern(
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | path.get(node.local ? "local" :
|
194 | node.name ? "name" : "id"),
|
195 | bindings
|
196 | );
|
197 |
|
198 | } else if (Node.check(node) && !Expression.check(node)) {
|
199 | types.eachField(node, function(name, child) {
|
200 | var childPath = path.get(name);
|
201 | if (childPath.value !== child) {
|
202 | throw new Error("");
|
203 | }
|
204 | recursiveScanChild(childPath, bindings, scopeTypes);
|
205 | });
|
206 | }
|
207 | }
|
208 |
|
209 | function recursiveScanChild(path, bindings, scopeTypes) {
|
210 | var node = path.value;
|
211 |
|
212 | if (!node || Expression.check(node)) {
|
213 |
|
214 |
|
215 | } else if (namedTypes.FunctionDeclaration.check(node)) {
|
216 | addPattern(path.get("id"), bindings);
|
217 |
|
218 | } else if (namedTypes.ClassDeclaration &&
|
219 | namedTypes.ClassDeclaration.check(node)) {
|
220 | addPattern(path.get("id"), bindings);
|
221 |
|
222 | } else if (ScopeType.check(node)) {
|
223 | if (namedTypes.CatchClause.check(node)) {
|
224 | var catchParamName = node.param.name;
|
225 | var hadBinding = hasOwn.call(bindings, catchParamName);
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | recursiveScanScope(path.get("body"), bindings, scopeTypes);
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | if (!hadBinding) {
|
237 | delete bindings[catchParamName];
|
238 | }
|
239 | }
|
240 |
|
241 | } else {
|
242 | recursiveScanScope(path, bindings, scopeTypes);
|
243 | }
|
244 | }
|
245 |
|
246 | function addPattern(patternPath, bindings) {
|
247 | var pattern = patternPath.value;
|
248 | namedTypes.Pattern.assert(pattern);
|
249 |
|
250 | if (namedTypes.Identifier.check(pattern)) {
|
251 | if (hasOwn.call(bindings, pattern.name)) {
|
252 | bindings[pattern.name].push(patternPath);
|
253 | } else {
|
254 | bindings[pattern.name] = [patternPath];
|
255 | }
|
256 |
|
257 | } else if (namedTypes.ObjectPattern &&
|
258 | namedTypes.ObjectPattern.check(pattern)) {
|
259 | patternPath.get('properties').each(function(propertyPath) {
|
260 | var property = propertyPath.value;
|
261 | if (namedTypes.Pattern.check(property)) {
|
262 | addPattern(propertyPath, bindings);
|
263 | } else if (namedTypes.Property.check(property)) {
|
264 | addPattern(propertyPath.get('value'), bindings);
|
265 | } else if (namedTypes.SpreadProperty &&
|
266 | namedTypes.SpreadProperty.check(property)) {
|
267 | addPattern(propertyPath.get('argument'), bindings);
|
268 | }
|
269 | });
|
270 |
|
271 | } else if (namedTypes.ArrayPattern &&
|
272 | namedTypes.ArrayPattern.check(pattern)) {
|
273 | patternPath.get('elements').each(function(elementPath) {
|
274 | var element = elementPath.value;
|
275 | if (namedTypes.Pattern.check(element)) {
|
276 | addPattern(elementPath, bindings);
|
277 | } else if (namedTypes.SpreadElement &&
|
278 | namedTypes.SpreadElement.check(element)) {
|
279 | addPattern(elementPath.get("argument"), bindings);
|
280 | }
|
281 | });
|
282 |
|
283 | } else if (namedTypes.PropertyPattern &&
|
284 | namedTypes.PropertyPattern.check(pattern)) {
|
285 | addPattern(patternPath.get('pattern'), bindings);
|
286 |
|
287 | } else if ((namedTypes.SpreadElementPattern &&
|
288 | namedTypes.SpreadElementPattern.check(pattern)) ||
|
289 | (namedTypes.SpreadPropertyPattern &&
|
290 | namedTypes.SpreadPropertyPattern.check(pattern))) {
|
291 | addPattern(patternPath.get('argument'), bindings);
|
292 | }
|
293 | }
|
294 |
|
295 | function addTypePattern(patternPath, types) {
|
296 | var pattern = patternPath.value;
|
297 | namedTypes.Pattern.assert(pattern);
|
298 |
|
299 | if (namedTypes.Identifier.check(pattern)) {
|
300 | if (hasOwn.call(types, pattern.name)) {
|
301 | types[pattern.name].push(patternPath);
|
302 | } else {
|
303 | types[pattern.name] = [patternPath];
|
304 | }
|
305 |
|
306 | }
|
307 | }
|
308 |
|
309 | Sp.lookup = function(name) {
|
310 | for (var scope = this; scope; scope = scope.parent)
|
311 | if (scope.declares(name))
|
312 | break;
|
313 | return scope;
|
314 | };
|
315 |
|
316 | Sp.lookupType = function(name) {
|
317 | for (var scope = this; scope; scope = scope.parent)
|
318 | if (scope.declaresType(name))
|
319 | break;
|
320 | return scope;
|
321 | };
|
322 |
|
323 | Sp.getGlobalScope = function() {
|
324 | var scope = this;
|
325 | while (!scope.isGlobal)
|
326 | scope = scope.parent;
|
327 | return scope;
|
328 | };
|
329 |
|
330 | module.exports = Scope;
|