ast-types 0.11.5 → 0.12.1

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.
Files changed (57) hide show
  1. package/.travis.yml +2 -2
  2. package/def/babel-core.d.ts +2 -0
  3. package/def/babel-core.js +250 -290
  4. package/def/babel.d.ts +2 -0
  5. package/def/babel.js +12 -3
  6. package/def/core.d.ts +2 -0
  7. package/def/core.js +57 -126
  8. package/def/es-proposals.d.ts +2 -0
  9. package/def/es-proposals.js +34 -32
  10. package/def/es6.d.ts +2 -0
  11. package/def/es6.js +210 -253
  12. package/def/es7.d.ts +2 -0
  13. package/def/es7.js +35 -41
  14. package/def/esprima.d.ts +2 -0
  15. package/def/esprima.js +50 -71
  16. package/def/flow.d.ts +2 -0
  17. package/def/flow.js +289 -393
  18. package/def/jsx.d.ts +2 -0
  19. package/def/jsx.js +99 -125
  20. package/def/type-annotations.d.ts +7 -0
  21. package/def/type-annotations.js +42 -0
  22. package/def/typescript.d.ts +2 -0
  23. package/def/typescript.js +326 -444
  24. package/fork.d.ts +46 -0
  25. package/fork.js +40 -31
  26. package/gen/builders.d.ts +2539 -0
  27. package/gen/builders.js +2 -0
  28. package/gen/kinds.d.ts +259 -0
  29. package/gen/kinds.js +2 -0
  30. package/gen/namedTypes.d.ts +262 -0
  31. package/gen/namedTypes.js +2 -0
  32. package/gen/nodes.d.ts +1255 -0
  33. package/gen/nodes.js +2 -0
  34. package/gen/visitor.d.ts +263 -0
  35. package/gen/visitor.js +2 -0
  36. package/lib/equiv.d.ts +5 -0
  37. package/lib/equiv.js +19 -49
  38. package/lib/node-path.d.ts +22 -0
  39. package/lib/node-path.js +60 -125
  40. package/lib/path-visitor.d.ts +52 -0
  41. package/lib/path-visitor.js +117 -194
  42. package/lib/path.d.ts +25 -0
  43. package/lib/path.js +71 -107
  44. package/lib/scope.d.ts +26 -0
  45. package/lib/scope.js +100 -143
  46. package/lib/shared.d.ts +13 -0
  47. package/lib/shared.js +34 -31
  48. package/lib/types.d.ts +140 -0
  49. package/lib/types.js +584 -676
  50. package/main.d.ts +16 -0
  51. package/main.js +33 -18
  52. package/package.json +24 -7
  53. package/tsconfig.json +20 -0
  54. package/types.d.ts +6 -0
  55. package/types.js +2 -0
  56. package/def/e4x.js +0 -86
  57. package/def/mozilla.js +0 -42
@@ -1,140 +1,113 @@
1
+ "use strict";;
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var types_1 = __importDefault(require("./types"));
7
+ var node_path_1 = __importDefault(require("./node-path"));
1
8
  var hasOwn = Object.prototype.hasOwnProperty;
2
-
3
- module.exports = function (fork) {
4
- var types = fork.use(require("./types"));
5
- var NodePath = fork.use(require("./node-path"));
6
- var Printable = types.namedTypes.Printable;
9
+ function pathVisitorPlugin(fork) {
10
+ var types = fork.use(types_1.default);
11
+ var NodePath = fork.use(node_path_1.default);
7
12
  var isArray = types.builtInTypes.array;
8
13
  var isObject = types.builtInTypes.object;
9
14
  var isFunction = types.builtInTypes.function;
10
15
  var undefined;
11
-
12
- function PathVisitor() {
16
+ var PathVisitor = function PathVisitor() {
13
17
  if (!(this instanceof PathVisitor)) {
14
- throw new Error(
15
- "PathVisitor constructor cannot be invoked without 'new'"
16
- );
18
+ throw new Error("PathVisitor constructor cannot be invoked without 'new'");
17
19
  }
18
-
19
20
  // Permanent state.
20
21
  this._reusableContextStack = [];
21
-
22
22
  this._methodNameTable = computeMethodNameTable(this);
23
23
  this._shouldVisitComments =
24
- hasOwn.call(this._methodNameTable, "Block") ||
25
- hasOwn.call(this._methodNameTable, "Line");
26
-
24
+ hasOwn.call(this._methodNameTable, "Block") ||
25
+ hasOwn.call(this._methodNameTable, "Line");
27
26
  this.Context = makeContextConstructor(this);
28
-
29
27
  // State reset every time PathVisitor.prototype.visit is called.
30
28
  this._visiting = false;
31
29
  this._changeReported = false;
32
- }
33
-
30
+ };
34
31
  function computeMethodNameTable(visitor) {
35
32
  var typeNames = Object.create(null);
36
-
37
33
  for (var methodName in visitor) {
38
34
  if (/^visit[A-Z]/.test(methodName)) {
39
35
  typeNames[methodName.slice("visit".length)] = true;
40
36
  }
41
37
  }
42
-
43
38
  var supertypeTable = types.computeSupertypeLookupTable(typeNames);
44
39
  var methodNameTable = Object.create(null);
45
-
46
- var typeNames = Object.keys(supertypeTable);
47
- var typeNameCount = typeNames.length;
40
+ var typeNameKeys = Object.keys(supertypeTable);
41
+ var typeNameCount = typeNameKeys.length;
48
42
  for (var i = 0; i < typeNameCount; ++i) {
49
- var typeName = typeNames[i];
43
+ var typeName = typeNameKeys[i];
50
44
  methodName = "visit" + supertypeTable[typeName];
51
45
  if (isFunction.check(visitor[methodName])) {
52
46
  methodNameTable[typeName] = methodName;
53
47
  }
54
48
  }
55
-
56
49
  return methodNameTable;
57
50
  }
58
-
59
51
  PathVisitor.fromMethodsObject = function fromMethodsObject(methods) {
60
52
  if (methods instanceof PathVisitor) {
61
53
  return methods;
62
54
  }
63
-
64
55
  if (!isObject.check(methods)) {
65
56
  // An empty visitor?
66
57
  return new PathVisitor;
67
58
  }
68
-
69
- function Visitor() {
59
+ var Visitor = function Visitor() {
70
60
  if (!(this instanceof Visitor)) {
71
- throw new Error(
72
- "Visitor constructor cannot be invoked without 'new'"
73
- );
61
+ throw new Error("Visitor constructor cannot be invoked without 'new'");
74
62
  }
75
63
  PathVisitor.call(this);
76
- }
77
-
64
+ };
78
65
  var Vp = Visitor.prototype = Object.create(PVp);
79
66
  Vp.constructor = Visitor;
80
-
81
67
  extend(Vp, methods);
82
68
  extend(Visitor, PathVisitor);
83
-
84
69
  isFunction.assert(Visitor.fromMethodsObject);
85
70
  isFunction.assert(Visitor.visit);
86
-
87
71
  return new Visitor;
88
72
  };
89
-
90
73
  function extend(target, source) {
91
74
  for (var property in source) {
92
75
  if (hasOwn.call(source, property)) {
93
76
  target[property] = source[property];
94
77
  }
95
78
  }
96
-
97
79
  return target;
98
80
  }
99
-
100
81
  PathVisitor.visit = function visit(node, methods) {
101
82
  return PathVisitor.fromMethodsObject(methods).visit(node);
102
83
  };
103
-
104
84
  var PVp = PathVisitor.prototype;
105
-
106
85
  PVp.visit = function () {
107
86
  if (this._visiting) {
108
- throw new Error(
109
- "Recursively calling visitor.visit(path) resets visitor state. " +
110
- "Try this.visit(path) or this.traverse(path) instead."
111
- );
87
+ throw new Error("Recursively calling visitor.visit(path) resets visitor state. " +
88
+ "Try this.visit(path) or this.traverse(path) instead.");
112
89
  }
113
-
114
90
  // Private state that needs to be reset before every traversal.
115
91
  this._visiting = true;
116
92
  this._changeReported = false;
117
93
  this._abortRequested = false;
118
-
119
94
  var argc = arguments.length;
120
- var args = new Array(argc)
95
+ var args = new Array(argc);
121
96
  for (var i = 0; i < argc; ++i) {
122
97
  args[i] = arguments[i];
123
98
  }
124
-
125
99
  if (!(args[0] instanceof NodePath)) {
126
- args[0] = new NodePath({root: args[0]}).get("root");
100
+ args[0] = new NodePath({ root: args[0] }).get("root");
127
101
  }
128
-
129
102
  // Called with the same arguments as .visit.
130
103
  this.reset.apply(this, args);
131
-
104
+ var didNotThrow;
132
105
  try {
133
106
  var root = this.visitWithoutReset(args[0]);
134
- var didNotThrow = true;
135
- } finally {
107
+ didNotThrow = true;
108
+ }
109
+ finally {
136
110
  this._visiting = false;
137
-
138
111
  if (!didNotThrow && this._abortRequested) {
139
112
  // If this.visitWithoutReset threw an exception and
140
113
  // this._abortRequested was set to true, return the root of
@@ -147,30 +120,24 @@ module.exports = function (fork) {
147
120
  return args[0].value;
148
121
  }
149
122
  }
150
-
151
123
  return root;
152
124
  };
153
-
154
- PVp.AbortRequest = function AbortRequest() {};
125
+ PVp.AbortRequest = function AbortRequest() { };
155
126
  PVp.abort = function () {
156
127
  var visitor = this;
157
128
  visitor._abortRequested = true;
158
129
  var request = new visitor.AbortRequest();
159
-
160
130
  // If you decide to catch this exception and stop it from propagating,
161
131
  // make sure to call its cancel method to avoid silencing other
162
132
  // exceptions that might be thrown later in the traversal.
163
133
  request.cancel = function () {
164
134
  visitor._abortRequested = false;
165
135
  };
166
-
167
136
  throw request;
168
137
  };
169
-
170
- PVp.reset = function (path/*, additional arguments */) {
138
+ PVp.reset = function (_path /*, additional arguments */) {
171
139
  // Empty stub; may be reassigned or overridden by subclasses.
172
140
  };
173
-
174
141
  PVp.visitWithoutReset = function (path) {
175
142
  if (this instanceof this.Context) {
176
143
  // Since this.Context.prototype === this, there's a chance we
@@ -178,33 +145,29 @@ module.exports = function (fork) {
178
145
  // happens, re-invoke the method against context.visitor.
179
146
  return this.visitor.visitWithoutReset(path);
180
147
  }
181
-
182
148
  if (!(path instanceof NodePath)) {
183
149
  throw new Error("");
184
150
  }
185
-
186
151
  var value = path.value;
187
-
188
152
  var methodName = value &&
189
- typeof value === "object" &&
190
- typeof value.type === "string" &&
191
- this._methodNameTable[value.type];
192
-
153
+ typeof value === "object" &&
154
+ typeof value.type === "string" &&
155
+ this._methodNameTable[value.type];
193
156
  if (methodName) {
194
157
  var context = this.acquireContext(path);
195
158
  try {
196
159
  return context.invokeVisitorMethod(methodName);
197
- } finally {
160
+ }
161
+ finally {
198
162
  this.releaseContext(context);
199
163
  }
200
-
201
- } else {
164
+ }
165
+ else {
202
166
  // If there was no visitor method to call, visit the children of
203
167
  // this node generically.
204
168
  return visitChildren(path, this);
205
169
  }
206
170
  };
207
-
208
171
  function visitChildren(path, visitor) {
209
172
  if (!(path instanceof NodePath)) {
210
173
  throw new Error("");
@@ -212,28 +175,25 @@ module.exports = function (fork) {
212
175
  if (!(visitor instanceof PathVisitor)) {
213
176
  throw new Error("");
214
177
  }
215
-
216
178
  var value = path.value;
217
-
218
179
  if (isArray.check(value)) {
219
180
  path.each(visitor.visitWithoutReset, visitor);
220
- } else if (!isObject.check(value)) {
181
+ }
182
+ else if (!isObject.check(value)) {
221
183
  // No children to visit.
222
- } else {
184
+ }
185
+ else {
223
186
  var childNames = types.getFieldNames(value);
224
-
225
187
  // The .comments field of the Node type is hidden, so we only
226
188
  // visit it if the visitor defines visitBlock or visitLine, and
227
189
  // value.comments is defined.
228
190
  if (visitor._shouldVisitComments &&
229
- value.comments &&
230
- childNames.indexOf("comments") < 0) {
191
+ value.comments &&
192
+ childNames.indexOf("comments") < 0) {
231
193
  childNames.push("comments");
232
194
  }
233
-
234
195
  var childCount = childNames.length;
235
196
  var childPaths = [];
236
-
237
197
  for (var i = 0; i < childCount; ++i) {
238
198
  var childName = childNames[i];
239
199
  if (!hasOwn.call(value, childName)) {
@@ -241,22 +201,18 @@ module.exports = function (fork) {
241
201
  }
242
202
  childPaths.push(path.get(childName));
243
203
  }
244
-
245
204
  for (var i = 0; i < childCount; ++i) {
246
205
  visitor.visitWithoutReset(childPaths[i]);
247
206
  }
248
207
  }
249
-
250
208
  return path.value;
251
209
  }
252
-
253
210
  PVp.acquireContext = function (path) {
254
211
  if (this._reusableContextStack.length === 0) {
255
212
  return new this.Context(path);
256
213
  }
257
214
  return this._reusableContextStack.pop().reset(path);
258
215
  };
259
-
260
216
  PVp.releaseContext = function (context) {
261
217
  if (!(context instanceof this.Context)) {
262
218
  throw new Error("");
@@ -264,15 +220,12 @@ module.exports = function (fork) {
264
220
  this._reusableContextStack.push(context);
265
221
  context.currentPath = null;
266
222
  };
267
-
268
223
  PVp.reportChanged = function () {
269
224
  this._changeReported = true;
270
225
  };
271
-
272
226
  PVp.wasChangeReported = function () {
273
227
  return this._changeReported;
274
228
  };
275
-
276
229
  function makeContextConstructor(visitor) {
277
230
  function Context(path) {
278
231
  if (!(this instanceof Context)) {
@@ -284,139 +237,109 @@ module.exports = function (fork) {
284
237
  if (!(path instanceof NodePath)) {
285
238
  throw new Error("");
286
239
  }
287
-
288
240
  Object.defineProperty(this, "visitor", {
289
241
  value: visitor,
290
242
  writable: false,
291
243
  enumerable: true,
292
244
  configurable: false
293
245
  });
294
-
295
246
  this.currentPath = path;
296
247
  this.needToCallTraverse = true;
297
-
298
248
  Object.seal(this);
299
249
  }
300
-
301
250
  if (!(visitor instanceof PathVisitor)) {
302
251
  throw new Error("");
303
252
  }
304
-
305
253
  // Note that the visitor object is the prototype of Context.prototype,
306
254
  // so all visitor methods are inherited by context objects.
307
255
  var Cp = Context.prototype = Object.create(visitor);
308
-
309
256
  Cp.constructor = Context;
310
257
  extend(Cp, sharedContextProtoMethods);
311
-
312
258
  return Context;
313
259
  }
314
-
315
- // Every PathVisitor has a different this.Context constructor and
316
- // this.Context.prototype object, but those prototypes can all use the
317
- // same reset, invokeVisitorMethod, and traverse function objects.
260
+ // Every PathVisitor has a different this.Context constructor and
261
+ // this.Context.prototype object, but those prototypes can all use the
262
+ // same reset, invokeVisitorMethod, and traverse function objects.
318
263
  var sharedContextProtoMethods = Object.create(null);
319
-
320
264
  sharedContextProtoMethods.reset =
321
- function reset(path) {
322
- if (!(this instanceof this.Context)) {
323
- throw new Error("");
324
- }
325
- if (!(path instanceof NodePath)) {
326
- throw new Error("");
327
- }
328
-
329
- this.currentPath = path;
330
- this.needToCallTraverse = true;
331
-
332
- return this;
333
- };
334
-
265
+ function reset(path) {
266
+ if (!(this instanceof this.Context)) {
267
+ throw new Error("");
268
+ }
269
+ if (!(path instanceof NodePath)) {
270
+ throw new Error("");
271
+ }
272
+ this.currentPath = path;
273
+ this.needToCallTraverse = true;
274
+ return this;
275
+ };
335
276
  sharedContextProtoMethods.invokeVisitorMethod =
336
- function invokeVisitorMethod(methodName) {
337
- if (!(this instanceof this.Context)) {
338
- throw new Error("");
339
- }
340
- if (!(this.currentPath instanceof NodePath)) {
341
- throw new Error("");
342
- }
343
-
344
- var result = this.visitor[methodName].call(this, this.currentPath);
345
-
346
- if (result === false) {
347
- // Visitor methods return false to indicate that they have handled
348
- // their own traversal needs, and we should not complain if
349
- // this.needToCallTraverse is still true.
350
- this.needToCallTraverse = false;
351
-
352
- } else if (result !== undefined) {
353
- // Any other non-undefined value returned from the visitor method
354
- // is interpreted as a replacement value.
355
- this.currentPath = this.currentPath.replace(result)[0];
356
-
357
- if (this.needToCallTraverse) {
358
- // If this.traverse still hasn't been called, visit the
359
- // children of the replacement node.
360
- this.traverse(this.currentPath);
361
- }
362
- }
363
-
364
- if (this.needToCallTraverse !== false) {
365
- throw new Error(
366
- "Must either call this.traverse or return false in " + methodName
367
- );
368
- }
369
-
370
- var path = this.currentPath;
371
- return path && path.value;
372
- };
373
-
277
+ function invokeVisitorMethod(methodName) {
278
+ if (!(this instanceof this.Context)) {
279
+ throw new Error("");
280
+ }
281
+ if (!(this.currentPath instanceof NodePath)) {
282
+ throw new Error("");
283
+ }
284
+ var result = this.visitor[methodName].call(this, this.currentPath);
285
+ if (result === false) {
286
+ // Visitor methods return false to indicate that they have handled
287
+ // their own traversal needs, and we should not complain if
288
+ // this.needToCallTraverse is still true.
289
+ this.needToCallTraverse = false;
290
+ }
291
+ else if (result !== undefined) {
292
+ // Any other non-undefined value returned from the visitor method
293
+ // is interpreted as a replacement value.
294
+ this.currentPath = this.currentPath.replace(result)[0];
295
+ if (this.needToCallTraverse) {
296
+ // If this.traverse still hasn't been called, visit the
297
+ // children of the replacement node.
298
+ this.traverse(this.currentPath);
299
+ }
300
+ }
301
+ if (this.needToCallTraverse !== false) {
302
+ throw new Error("Must either call this.traverse or return false in " + methodName);
303
+ }
304
+ var path = this.currentPath;
305
+ return path && path.value;
306
+ };
374
307
  sharedContextProtoMethods.traverse =
375
- function traverse(path, newVisitor) {
376
- if (!(this instanceof this.Context)) {
377
- throw new Error("");
378
- }
379
- if (!(path instanceof NodePath)) {
380
- throw new Error("");
381
- }
382
- if (!(this.currentPath instanceof NodePath)) {
383
- throw new Error("");
384
- }
385
-
386
- this.needToCallTraverse = false;
387
-
388
- return visitChildren(path, PathVisitor.fromMethodsObject(
389
- newVisitor || this.visitor
390
- ));
391
- };
392
-
308
+ function traverse(path, newVisitor) {
309
+ if (!(this instanceof this.Context)) {
310
+ throw new Error("");
311
+ }
312
+ if (!(path instanceof NodePath)) {
313
+ throw new Error("");
314
+ }
315
+ if (!(this.currentPath instanceof NodePath)) {
316
+ throw new Error("");
317
+ }
318
+ this.needToCallTraverse = false;
319
+ return visitChildren(path, PathVisitor.fromMethodsObject(newVisitor || this.visitor));
320
+ };
393
321
  sharedContextProtoMethods.visit =
394
- function visit(path, newVisitor) {
395
- if (!(this instanceof this.Context)) {
396
- throw new Error("");
397
- }
398
- if (!(path instanceof NodePath)) {
399
- throw new Error("");
400
- }
401
- if (!(this.currentPath instanceof NodePath)) {
402
- throw new Error("");
403
- }
404
-
405
- this.needToCallTraverse = false;
406
-
407
- return PathVisitor.fromMethodsObject(
408
- newVisitor || this.visitor
409
- ).visitWithoutReset(path);
410
- };
411
-
322
+ function visit(path, newVisitor) {
323
+ if (!(this instanceof this.Context)) {
324
+ throw new Error("");
325
+ }
326
+ if (!(path instanceof NodePath)) {
327
+ throw new Error("");
328
+ }
329
+ if (!(this.currentPath instanceof NodePath)) {
330
+ throw new Error("");
331
+ }
332
+ this.needToCallTraverse = false;
333
+ return PathVisitor.fromMethodsObject(newVisitor || this.visitor).visitWithoutReset(path);
334
+ };
412
335
  sharedContextProtoMethods.reportChanged = function reportChanged() {
413
336
  this.visitor.reportChanged();
414
337
  };
415
-
416
338
  sharedContextProtoMethods.abort = function abort() {
417
339
  this.needToCallTraverse = false;
418
340
  this.visitor.abort();
419
341
  };
420
-
421
342
  return PathVisitor;
422
- };
343
+ }
344
+ exports.default = pathVisitorPlugin;
345
+ module.exports = exports["default"];
package/lib/path.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { Fork } from "../types";
2
+ import { ASTNode } from "./types";
3
+ export interface Path<V = any> {
4
+ value: V;
5
+ parentPath: any;
6
+ name: any;
7
+ __childCache: object | null;
8
+ getValueProperty(name: any): any;
9
+ get(...names: any[]): any;
10
+ each(callback: any, context: any): any;
11
+ map(callback: any, context: any): any;
12
+ filter(callback: any, context: any): any;
13
+ shift(): any;
14
+ unshift(...args: any[]): any;
15
+ push(...args: any[]): any;
16
+ pop(): any;
17
+ insertAt(index: number, ...args: any[]): any;
18
+ insertBefore(...args: any[]): any;
19
+ insertAfter(...args: any[]): any;
20
+ replace(replacement?: ASTNode, ...args: ASTNode[]): any;
21
+ }
22
+ export interface PathConstructor {
23
+ new <V = any>(value: any, parentPath?: any, name?: any): Path<V>;
24
+ }
25
+ export default function pathPlugin(fork: Fork): PathConstructor;