ast-types 0.8.14 → 0.8.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/def/core.js +9 -1
- package/lib/scope.js +47 -12
- package/package.json +1 -1
package/def/core.js
CHANGED
|
@@ -305,7 +305,15 @@ def("MemberExpression")
|
|
|
305
305
|
.build("object", "property", "computed")
|
|
306
306
|
.field("object", def("Expression"))
|
|
307
307
|
.field("property", or(def("Identifier"), def("Expression")))
|
|
308
|
-
.field("computed", Boolean,
|
|
308
|
+
.field("computed", Boolean, function(){
|
|
309
|
+
var type = this.property.type;
|
|
310
|
+
if (type === 'Literal' ||
|
|
311
|
+
type === 'MemberExpression' ||
|
|
312
|
+
type === 'BinaryExpression') {
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
return false;
|
|
316
|
+
});
|
|
309
317
|
|
|
310
318
|
def("Pattern").bases("Node");
|
|
311
319
|
|
package/lib/scope.js
CHANGED
|
@@ -34,7 +34,8 @@ function Scope(path, parentScope) {
|
|
|
34
34
|
isGlobal: { value: !parentScope, enumerable: true },
|
|
35
35
|
depth: { value: depth },
|
|
36
36
|
parent: { value: parentScope },
|
|
37
|
-
bindings: { value: {} }
|
|
37
|
+
bindings: { value: {} },
|
|
38
|
+
types: { value: {} },
|
|
38
39
|
});
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -67,6 +68,11 @@ Sp.declares = function(name) {
|
|
|
67
68
|
return hasOwn.call(this.bindings, name);
|
|
68
69
|
};
|
|
69
70
|
|
|
71
|
+
Sp.declaresType = function(name) {
|
|
72
|
+
this.scan();
|
|
73
|
+
return hasOwn.call(this.types, name);
|
|
74
|
+
};
|
|
75
|
+
|
|
70
76
|
Sp.declareTemporary = function(prefix) {
|
|
71
77
|
if (prefix) {
|
|
72
78
|
if (!/^[a-z$_]/i.test(prefix)) {
|
|
@@ -115,7 +121,7 @@ Sp.scan = function(force) {
|
|
|
115
121
|
// Empty out this.bindings, just in cases.
|
|
116
122
|
delete this.bindings[name];
|
|
117
123
|
}
|
|
118
|
-
scanScope(this.path, this.bindings);
|
|
124
|
+
scanScope(this.path, this.bindings, this.types);
|
|
119
125
|
this.didScan = true;
|
|
120
126
|
}
|
|
121
127
|
};
|
|
@@ -125,7 +131,12 @@ Sp.getBindings = function () {
|
|
|
125
131
|
return this.bindings;
|
|
126
132
|
};
|
|
127
133
|
|
|
128
|
-
function
|
|
134
|
+
Sp.getTypes = function () {
|
|
135
|
+
this.scan();
|
|
136
|
+
return this.types;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
function scanScope(path, bindings, scopeTypes) {
|
|
129
140
|
var node = path.value;
|
|
130
141
|
ScopeType.assert(node);
|
|
131
142
|
|
|
@@ -136,11 +147,11 @@ function scanScope(path, bindings) {
|
|
|
136
147
|
addPattern(path.get("param"), bindings);
|
|
137
148
|
|
|
138
149
|
} else {
|
|
139
|
-
recursiveScanScope(path, bindings);
|
|
150
|
+
recursiveScanScope(path, bindings, scopeTypes);
|
|
140
151
|
}
|
|
141
152
|
}
|
|
142
153
|
|
|
143
|
-
function recursiveScanScope(path, bindings) {
|
|
154
|
+
function recursiveScanScope(path, bindings, scopeTypes) {
|
|
144
155
|
var node = path.value;
|
|
145
156
|
|
|
146
157
|
if (path.parent &&
|
|
@@ -154,7 +165,7 @@ function recursiveScanScope(path, bindings) {
|
|
|
154
165
|
|
|
155
166
|
} else if (isArray.check(node)) {
|
|
156
167
|
path.each(function(childPath) {
|
|
157
|
-
recursiveScanChild(childPath, bindings);
|
|
168
|
+
recursiveScanChild(childPath, bindings, scopeTypes);
|
|
158
169
|
});
|
|
159
170
|
|
|
160
171
|
} else if (namedTypes.Function.check(node)) {
|
|
@@ -162,11 +173,14 @@ function recursiveScanScope(path, bindings) {
|
|
|
162
173
|
addPattern(paramPath, bindings);
|
|
163
174
|
});
|
|
164
175
|
|
|
165
|
-
recursiveScanChild(path.get("body"), bindings);
|
|
176
|
+
recursiveScanChild(path.get("body"), bindings, scopeTypes);
|
|
177
|
+
|
|
178
|
+
} else if (namedTypes.TypeAlias && namedTypes.TypeAlias.check(node)) {
|
|
179
|
+
addTypePattern(path.get("id"), scopeTypes);
|
|
166
180
|
|
|
167
181
|
} else if (namedTypes.VariableDeclarator.check(node)) {
|
|
168
182
|
addPattern(path.get("id"), bindings);
|
|
169
|
-
recursiveScanChild(path.get("init"), bindings);
|
|
183
|
+
recursiveScanChild(path.get("init"), bindings, scopeTypes);
|
|
170
184
|
|
|
171
185
|
} else if (node.type === "ImportSpecifier" ||
|
|
172
186
|
node.type === "ImportNamespaceSpecifier" ||
|
|
@@ -187,12 +201,12 @@ function recursiveScanScope(path, bindings) {
|
|
|
187
201
|
if (childPath.value !== child) {
|
|
188
202
|
throw new Error("");
|
|
189
203
|
}
|
|
190
|
-
recursiveScanChild(childPath, bindings);
|
|
204
|
+
recursiveScanChild(childPath, bindings, scopeTypes);
|
|
191
205
|
});
|
|
192
206
|
}
|
|
193
207
|
}
|
|
194
208
|
|
|
195
|
-
function recursiveScanChild(path, bindings) {
|
|
209
|
+
function recursiveScanChild(path, bindings, scopeTypes) {
|
|
196
210
|
var node = path.value;
|
|
197
211
|
|
|
198
212
|
if (!node || Expression.check(node)) {
|
|
@@ -213,7 +227,7 @@ function recursiveScanChild(path, bindings) {
|
|
|
213
227
|
// Any declarations that occur inside the catch body that do
|
|
214
228
|
// not have the same name as the catch parameter should count
|
|
215
229
|
// as bindings in the outer scope.
|
|
216
|
-
recursiveScanScope(path.get("body"), bindings);
|
|
230
|
+
recursiveScanScope(path.get("body"), bindings, scopeTypes);
|
|
217
231
|
|
|
218
232
|
// If a new binding matching the catch parameter name was
|
|
219
233
|
// created while scanning the catch body, ignore it because it
|
|
@@ -225,7 +239,7 @@ function recursiveScanChild(path, bindings) {
|
|
|
225
239
|
}
|
|
226
240
|
|
|
227
241
|
} else {
|
|
228
|
-
recursiveScanScope(path, bindings);
|
|
242
|
+
recursiveScanScope(path, bindings, scopeTypes);
|
|
229
243
|
}
|
|
230
244
|
}
|
|
231
245
|
|
|
@@ -278,6 +292,20 @@ function addPattern(patternPath, bindings) {
|
|
|
278
292
|
}
|
|
279
293
|
}
|
|
280
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
|
+
|
|
281
309
|
Sp.lookup = function(name) {
|
|
282
310
|
for (var scope = this; scope; scope = scope.parent)
|
|
283
311
|
if (scope.declares(name))
|
|
@@ -285,6 +313,13 @@ Sp.lookup = function(name) {
|
|
|
285
313
|
return scope;
|
|
286
314
|
};
|
|
287
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
|
+
|
|
288
323
|
Sp.getGlobalScope = function() {
|
|
289
324
|
var scope = this;
|
|
290
325
|
while (!scope.isGlobal)
|