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
package/lib/node-path.js CHANGED
@@ -1,19 +1,25 @@
1
- module.exports = function (fork) {
2
- var types = fork.use(require("./types"));
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 path_1 = __importDefault(require("./path"));
8
+ var scope_1 = __importDefault(require("./scope"));
9
+ function nodePathPlugin(fork) {
10
+ var types = fork.use(types_1.default);
3
11
  var n = types.namedTypes;
4
12
  var b = types.builders;
5
13
  var isNumber = types.builtInTypes.number;
6
14
  var isArray = types.builtInTypes.array;
7
- var Path = fork.use(require("./path"));
8
- var Scope = fork.use(require("./scope"));
9
-
10
- function NodePath(value, parentPath, name) {
15
+ var Path = fork.use(path_1.default);
16
+ var Scope = fork.use(scope_1.default);
17
+ var NodePath = function NodePath(value, parentPath, name) {
11
18
  if (!(this instanceof NodePath)) {
12
19
  throw new Error("NodePath constructor cannot be invoked without 'new'");
13
20
  }
14
21
  Path.call(this, value, parentPath, name);
15
- }
16
-
22
+ };
17
23
  var NPp = NodePath.prototype = Object.create(Path.prototype, {
18
24
  constructor: {
19
25
  value: NodePath,
@@ -22,108 +28,86 @@ module.exports = function (fork) {
22
28
  configurable: true
23
29
  }
24
30
  });
25
-
26
31
  Object.defineProperties(NPp, {
27
32
  node: {
28
33
  get: function () {
29
34
  Object.defineProperty(this, "node", {
30
- configurable: true, // Enable deletion.
35
+ configurable: true,
31
36
  value: this._computeNode()
32
37
  });
33
-
34
38
  return this.node;
35
39
  }
36
40
  },
37
-
38
41
  parent: {
39
42
  get: function () {
40
43
  Object.defineProperty(this, "parent", {
41
- configurable: true, // Enable deletion.
44
+ configurable: true,
42
45
  value: this._computeParent()
43
46
  });
44
-
45
47
  return this.parent;
46
48
  }
47
49
  },
48
-
49
50
  scope: {
50
51
  get: function () {
51
52
  Object.defineProperty(this, "scope", {
52
- configurable: true, // Enable deletion.
53
+ configurable: true,
53
54
  value: this._computeScope()
54
55
  });
55
-
56
56
  return this.scope;
57
57
  }
58
58
  }
59
59
  });
60
-
61
60
  NPp.replace = function () {
62
61
  delete this.node;
63
62
  delete this.parent;
64
63
  delete this.scope;
65
64
  return Path.prototype.replace.apply(this, arguments);
66
65
  };
67
-
68
66
  NPp.prune = function () {
69
67
  var remainingNodePath = this.parent;
70
-
71
68
  this.replace();
72
-
73
69
  return cleanUpNodesAfterPrune(remainingNodePath);
74
70
  };
75
-
76
71
  // The value of the first ancestor Path whose value is a Node.
77
72
  NPp._computeNode = function () {
78
73
  var value = this.value;
79
74
  if (n.Node.check(value)) {
80
75
  return value;
81
76
  }
82
-
83
77
  var pp = this.parentPath;
84
78
  return pp && pp.node || null;
85
79
  };
86
-
87
80
  // The first ancestor Path whose value is a Node distinct from this.node.
88
81
  NPp._computeParent = function () {
89
82
  var value = this.value;
90
83
  var pp = this.parentPath;
91
-
92
84
  if (!n.Node.check(value)) {
93
85
  while (pp && !n.Node.check(pp.value)) {
94
86
  pp = pp.parentPath;
95
87
  }
96
-
97
88
  if (pp) {
98
89
  pp = pp.parentPath;
99
90
  }
100
91
  }
101
-
102
92
  while (pp && !n.Node.check(pp.value)) {
103
93
  pp = pp.parentPath;
104
94
  }
105
-
106
95
  return pp || null;
107
96
  };
108
-
109
97
  // The closest enclosing scope that governs this node.
110
98
  NPp._computeScope = function () {
111
99
  var value = this.value;
112
100
  var pp = this.parentPath;
113
101
  var scope = pp && pp.scope;
114
-
115
102
  if (n.Node.check(value) &&
116
- Scope.isEstablishedBy(value)) {
103
+ Scope.isEstablishedBy(value)) {
117
104
  scope = new Scope(this, scope);
118
105
  }
119
-
120
106
  return scope || null;
121
107
  };
122
-
123
108
  NPp.getValueProperty = function (name) {
124
109
  return types.getFieldValue(this.value, name);
125
110
  };
126
-
127
111
  /**
128
112
  * Determine whether this.node needs to be wrapped in parentheses in order
129
113
  * for a parser to reproduce the same local AST structure.
@@ -142,74 +126,60 @@ module.exports = function (fork) {
142
126
  if (!pp) {
143
127
  return false;
144
128
  }
145
-
146
129
  var node = this.value;
147
-
148
130
  // Only expressions need parentheses.
149
131
  if (!n.Expression.check(node)) {
150
132
  return false;
151
133
  }
152
-
153
134
  // Identifiers never need parentheses.
154
135
  if (node.type === "Identifier") {
155
136
  return false;
156
137
  }
157
-
158
138
  while (!n.Node.check(pp.value)) {
159
139
  pp = pp.parentPath;
160
140
  if (!pp) {
161
141
  return false;
162
142
  }
163
143
  }
164
-
165
144
  var parent = pp.value;
166
-
167
145
  switch (node.type) {
168
146
  case "UnaryExpression":
169
147
  case "SpreadElement":
170
148
  case "SpreadProperty":
171
149
  return parent.type === "MemberExpression"
172
- && this.name === "object"
173
- && parent.object === node;
174
-
150
+ && this.name === "object"
151
+ && parent.object === node;
175
152
  case "BinaryExpression":
176
153
  case "LogicalExpression":
177
154
  switch (parent.type) {
178
155
  case "CallExpression":
179
156
  return this.name === "callee"
180
- && parent.callee === node;
181
-
157
+ && parent.callee === node;
182
158
  case "UnaryExpression":
183
159
  case "SpreadElement":
184
160
  case "SpreadProperty":
185
161
  return true;
186
-
187
162
  case "MemberExpression":
188
163
  return this.name === "object"
189
- && parent.object === node;
190
-
164
+ && parent.object === node;
191
165
  case "BinaryExpression":
192
166
  case "LogicalExpression":
193
167
  var po = parent.operator;
194
168
  var pp = PRECEDENCE[po];
195
169
  var no = node.operator;
196
170
  var np = PRECEDENCE[no];
197
-
198
171
  if (pp > np) {
199
172
  return true;
200
173
  }
201
-
202
174
  if (pp === np && this.name === "right") {
203
175
  if (parent.right !== node) {
204
176
  throw new Error("Nodes must be equal");
205
177
  }
206
178
  return true;
207
179
  }
208
-
209
180
  default:
210
181
  return false;
211
182
  }
212
-
213
183
  case "SequenceExpression":
214
184
  switch (parent.type) {
215
185
  case "ForStatement":
@@ -218,16 +188,13 @@ module.exports = function (fork) {
218
188
  // dictates that e.g. i++, j++ should not be wrapped with
219
189
  // parentheses.
220
190
  return false;
221
-
222
191
  case "ExpressionStatement":
223
192
  return this.name !== "expression";
224
-
225
193
  default:
226
194
  // Otherwise err on the side of overparenthesization, adding
227
195
  // explicit exceptions above if this proves overzealous.
228
196
  return true;
229
197
  }
230
-
231
198
  case "YieldExpression":
232
199
  switch (parent.type) {
233
200
  case "BinaryExpression":
@@ -241,17 +208,14 @@ module.exports = function (fork) {
241
208
  case "ConditionalExpression":
242
209
  case "YieldExpression":
243
210
  return true;
244
-
245
211
  default:
246
212
  return false;
247
213
  }
248
-
249
214
  case "Literal":
250
215
  return parent.type === "MemberExpression"
251
- && isNumber.check(node.value)
252
- && this.name === "object"
253
- && parent.object === node;
254
-
216
+ && isNumber.check(node.value)
217
+ && this.name === "object"
218
+ && parent.object === node;
255
219
  case "AssignmentExpression":
256
220
  case "ConditionalExpression":
257
221
  switch (parent.type) {
@@ -261,52 +225,43 @@ module.exports = function (fork) {
261
225
  case "BinaryExpression":
262
226
  case "LogicalExpression":
263
227
  return true;
264
-
265
228
  case "CallExpression":
266
229
  return this.name === "callee"
267
- && parent.callee === node;
268
-
230
+ && parent.callee === node;
269
231
  case "ConditionalExpression":
270
232
  return this.name === "test"
271
- && parent.test === node;
272
-
233
+ && parent.test === node;
273
234
  case "MemberExpression":
274
235
  return this.name === "object"
275
- && parent.object === node;
276
-
236
+ && parent.object === node;
277
237
  default:
278
238
  return false;
279
239
  }
280
-
281
240
  default:
282
241
  if (parent.type === "NewExpression" &&
283
- this.name === "callee" &&
284
- parent.callee === node) {
242
+ this.name === "callee" &&
243
+ parent.callee === node) {
285
244
  return containsCallExpression(node);
286
245
  }
287
246
  }
288
-
289
247
  if (assumeExpressionContext !== true &&
290
- !this.canBeFirstInStatement() &&
291
- this.firstInStatement())
248
+ !this.canBeFirstInStatement() &&
249
+ this.firstInStatement())
292
250
  return true;
293
-
294
251
  return false;
295
252
  };
296
-
297
253
  function isBinary(node) {
298
254
  return n.BinaryExpression.check(node)
299
- || n.LogicalExpression.check(node);
255
+ || n.LogicalExpression.check(node);
300
256
  }
301
-
257
+ // @ts-ignore 'isUnaryLike' is declared but its value is never read. [6133]
302
258
  function isUnaryLike(node) {
303
259
  return n.UnaryExpression.check(node)
304
- // I considered making SpreadElement and SpreadProperty subtypes
305
- // of UnaryExpression, but they're not really Expression nodes.
306
- || (n.SpreadElement && n.SpreadElement.check(node))
307
- || (n.SpreadProperty && n.SpreadProperty.check(node));
260
+ // I considered making SpreadElement and SpreadProperty subtypes
261
+ // of UnaryExpression, but they're not really Expression nodes.
262
+ || (n.SpreadElement && n.SpreadElement.check(node))
263
+ || (n.SpreadProperty && n.SpreadProperty.check(node));
308
264
  }
309
-
310
265
  var PRECEDENCE = {};
311
266
  [["||"],
312
267
  ["&&"],
@@ -323,113 +278,95 @@ module.exports = function (fork) {
323
278
  PRECEDENCE[op] = i;
324
279
  });
325
280
  });
326
-
327
281
  function containsCallExpression(node) {
328
282
  if (n.CallExpression.check(node)) {
329
283
  return true;
330
284
  }
331
-
332
285
  if (isArray.check(node)) {
333
286
  return node.some(containsCallExpression);
334
287
  }
335
-
336
288
  if (n.Node.check(node)) {
337
- return types.someField(node, function (name, child) {
289
+ return types.someField(node, function (_name, child) {
338
290
  return containsCallExpression(child);
339
291
  });
340
292
  }
341
-
342
293
  return false;
343
294
  }
344
-
345
295
  NPp.canBeFirstInStatement = function () {
346
296
  var node = this.node;
347
297
  return !n.FunctionExpression.check(node)
348
- && !n.ObjectExpression.check(node);
298
+ && !n.ObjectExpression.check(node);
349
299
  };
350
-
351
300
  NPp.firstInStatement = function () {
352
301
  return firstInStatement(this);
353
302
  };
354
-
355
303
  function firstInStatement(path) {
356
304
  for (var node, parent; path.parent; path = path.parent) {
357
305
  node = path.node;
358
306
  parent = path.parent.node;
359
-
360
307
  if (n.BlockStatement.check(parent) &&
361
- path.parent.name === "body" &&
362
- path.name === 0) {
308
+ path.parent.name === "body" &&
309
+ path.name === 0) {
363
310
  if (parent.body[0] !== node) {
364
311
  throw new Error("Nodes must be equal");
365
312
  }
366
313
  return true;
367
314
  }
368
-
369
315
  if (n.ExpressionStatement.check(parent) &&
370
- path.name === "expression") {
316
+ path.name === "expression") {
371
317
  if (parent.expression !== node) {
372
318
  throw new Error("Nodes must be equal");
373
319
  }
374
320
  return true;
375
321
  }
376
-
377
322
  if (n.SequenceExpression.check(parent) &&
378
- path.parent.name === "expressions" &&
379
- path.name === 0) {
323
+ path.parent.name === "expressions" &&
324
+ path.name === 0) {
380
325
  if (parent.expressions[0] !== node) {
381
326
  throw new Error("Nodes must be equal");
382
327
  }
383
328
  continue;
384
329
  }
385
-
386
330
  if (n.CallExpression.check(parent) &&
387
- path.name === "callee") {
331
+ path.name === "callee") {
388
332
  if (parent.callee !== node) {
389
333
  throw new Error("Nodes must be equal");
390
334
  }
391
335
  continue;
392
336
  }
393
-
394
337
  if (n.MemberExpression.check(parent) &&
395
- path.name === "object") {
338
+ path.name === "object") {
396
339
  if (parent.object !== node) {
397
340
  throw new Error("Nodes must be equal");
398
341
  }
399
342
  continue;
400
343
  }
401
-
402
344
  if (n.ConditionalExpression.check(parent) &&
403
- path.name === "test") {
345
+ path.name === "test") {
404
346
  if (parent.test !== node) {
405
347
  throw new Error("Nodes must be equal");
406
348
  }
407
349
  continue;
408
350
  }
409
-
410
351
  if (isBinary(parent) &&
411
- path.name === "left") {
352
+ path.name === "left") {
412
353
  if (parent.left !== node) {
413
354
  throw new Error("Nodes must be equal");
414
355
  }
415
356
  continue;
416
357
  }
417
-
418
358
  if (n.UnaryExpression.check(parent) &&
419
- !parent.prefix &&
420
- path.name === "argument") {
359
+ !parent.prefix &&
360
+ path.name === "argument") {
421
361
  if (parent.argument !== node) {
422
362
  throw new Error("Nodes must be equal");
423
363
  }
424
364
  continue;
425
365
  }
426
-
427
366
  return false;
428
367
  }
429
-
430
368
  return true;
431
369
  }
432
-
433
370
  /**
434
371
  * Pruning certain nodes will result in empty or incomplete nodes, here we clean those nodes up.
435
372
  */
@@ -439,38 +376,36 @@ module.exports = function (fork) {
439
376
  if (!declarations || declarations.length === 0) {
440
377
  return remainingNodePath.prune();
441
378
  }
442
- } else if (n.ExpressionStatement.check(remainingNodePath.node)) {
379
+ }
380
+ else if (n.ExpressionStatement.check(remainingNodePath.node)) {
443
381
  if (!remainingNodePath.get('expression').value) {
444
382
  return remainingNodePath.prune();
445
383
  }
446
- } else if (n.IfStatement.check(remainingNodePath.node)) {
384
+ }
385
+ else if (n.IfStatement.check(remainingNodePath.node)) {
447
386
  cleanUpIfStatementAfterPrune(remainingNodePath);
448
387
  }
449
-
450
388
  return remainingNodePath;
451
389
  }
452
-
453
390
  function cleanUpIfStatementAfterPrune(ifStatement) {
454
391
  var testExpression = ifStatement.get('test').value;
455
392
  var alternate = ifStatement.get('alternate').value;
456
393
  var consequent = ifStatement.get('consequent').value;
457
-
458
394
  if (!consequent && !alternate) {
459
395
  var testExpressionStatement = b.expressionStatement(testExpression);
460
-
461
396
  ifStatement.replace(testExpressionStatement);
462
- } else if (!consequent && alternate) {
397
+ }
398
+ else if (!consequent && alternate) {
463
399
  var negatedTestExpression = b.unaryExpression('!', testExpression, true);
464
-
465
400
  if (n.UnaryExpression.check(testExpression) && testExpression.operator === '!') {
466
401
  negatedTestExpression = testExpression.argument;
467
402
  }
468
-
469
403
  ifStatement.get("test").replace(negatedTestExpression);
470
404
  ifStatement.get("consequent").replace(alternate);
471
405
  ifStatement.get("alternate").replace();
472
406
  }
473
407
  }
474
-
475
408
  return NodePath;
476
- };
409
+ }
410
+ exports.default = nodePathPlugin;
411
+ module.exports = exports["default"];
@@ -0,0 +1,52 @@
1
+ import { Fork, Omit } from "../types";
2
+ import { ASTNode } from "./types";
3
+ import { NodePath } from "./node-path";
4
+ export interface PathVisitor {
5
+ _reusableContextStack: any;
6
+ _methodNameTable: any;
7
+ _shouldVisitComments: any;
8
+ Context: any;
9
+ _visiting: any;
10
+ _changeReported: any;
11
+ _abortRequested: boolean;
12
+ visit(...args: any[]): any;
13
+ reset(...args: any[]): any;
14
+ visitWithoutReset(path: any): any;
15
+ AbortRequest: any;
16
+ abort(): void;
17
+ visitor: any;
18
+ acquireContext(path: any): any;
19
+ releaseContext(context: any): void;
20
+ reportChanged(): void;
21
+ wasChangeReported(): any;
22
+ }
23
+ export interface PathVisitorStatics {
24
+ fromMethodsObject(methods?: any): Visitor;
25
+ visit(node: ASTNode, methods?: any): any;
26
+ }
27
+ export interface PathVisitorConstructor extends PathVisitorStatics {
28
+ new (): PathVisitor;
29
+ }
30
+ export interface Visitor extends PathVisitor {
31
+ }
32
+ export interface VisitorConstructor extends PathVisitorStatics {
33
+ new (): Visitor;
34
+ }
35
+ export interface VisitorMethods {
36
+ [visitorMethod: string]: (path: NodePath) => any;
37
+ }
38
+ export interface SharedContextMethods {
39
+ currentPath: any;
40
+ needToCallTraverse: boolean;
41
+ Context: any;
42
+ visitor: any;
43
+ reset(path: any, ...args: any[]): any;
44
+ invokeVisitorMethod(methodName: string): any;
45
+ traverse(path: any, newVisitor?: VisitorMethods): any;
46
+ visit(path: any, newVisitor?: VisitorMethods): any;
47
+ reportChanged(): void;
48
+ abort(): void;
49
+ }
50
+ export interface Context extends Omit<PathVisitor, "visit" | "reset">, SharedContextMethods {
51
+ }
52
+ export default function pathVisitorPlugin(fork: Fork): PathVisitorConstructor;