ast-types 0.7.8 → 0.8.2
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/.travis.yml +5 -1
- package/def/babel.js +101 -0
- package/def/core.js +38 -65
- package/def/e4x.js +9 -12
- package/def/es6.js +53 -104
- package/def/es7.js +6 -8
- package/def/esprima.js +96 -0
- package/def/fb-harmony.js +53 -34
- package/def/mozilla.js +7 -1
- package/lib/types.js +72 -38
- package/main.js +2 -0
- package/package.json +6 -6
package/.travis.yml
CHANGED
package/def/babel.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
require("./es7");
|
|
2
|
+
|
|
3
|
+
var types = require("../lib/types");
|
|
4
|
+
var defaults = require("../lib/shared").defaults;
|
|
5
|
+
var def = types.Type.def;
|
|
6
|
+
var or = types.Type.or;
|
|
7
|
+
|
|
8
|
+
def("Noop")
|
|
9
|
+
.bases("Node")
|
|
10
|
+
.build();
|
|
11
|
+
|
|
12
|
+
def("DoExpression")
|
|
13
|
+
.bases("Expression")
|
|
14
|
+
.build("body")
|
|
15
|
+
.field("body", [def("Statement")]);
|
|
16
|
+
|
|
17
|
+
def("Super")
|
|
18
|
+
.bases("Expression")
|
|
19
|
+
.build();
|
|
20
|
+
|
|
21
|
+
def("BindExpression")
|
|
22
|
+
.bases("Expression")
|
|
23
|
+
.build("object", "callee")
|
|
24
|
+
.field("object", or(def("Expression"), null))
|
|
25
|
+
.field("callee", def("Expression"));
|
|
26
|
+
|
|
27
|
+
def("Decorator")
|
|
28
|
+
.bases("Node")
|
|
29
|
+
.build("expression")
|
|
30
|
+
.field("expression", def("Expression"));
|
|
31
|
+
|
|
32
|
+
def("Property")
|
|
33
|
+
.field("decorators",
|
|
34
|
+
or([def("Decorator")], null),
|
|
35
|
+
defaults["null"]);
|
|
36
|
+
|
|
37
|
+
def("MethodDefinition")
|
|
38
|
+
.field("decorators",
|
|
39
|
+
or([def("Decorator")], null),
|
|
40
|
+
defaults["null"]);
|
|
41
|
+
|
|
42
|
+
def("MetaProperty")
|
|
43
|
+
.bases("Expression")
|
|
44
|
+
.build("meta", "property")
|
|
45
|
+
.field("meta", def("Identifier"))
|
|
46
|
+
.field("property", def("Identifier"));
|
|
47
|
+
|
|
48
|
+
def("ParenthesizedExpression")
|
|
49
|
+
.bases("Expression")
|
|
50
|
+
.build("expression")
|
|
51
|
+
.field("expression", def("Expression"));
|
|
52
|
+
|
|
53
|
+
def("ModuleSpecifier")
|
|
54
|
+
.bases("Specifier")
|
|
55
|
+
.field("local", def("Identifier"));
|
|
56
|
+
|
|
57
|
+
def("ImportSpecifier")
|
|
58
|
+
.bases("ModuleSpecifier")
|
|
59
|
+
.build("imported", "local")
|
|
60
|
+
.field("imported", def("Identifier"));
|
|
61
|
+
|
|
62
|
+
def("ImportDefaultSpecifier")
|
|
63
|
+
.bases("ModuleSpecifier")
|
|
64
|
+
.build("local");
|
|
65
|
+
|
|
66
|
+
def("ImportNamespaceSpecifier")
|
|
67
|
+
.bases("ModuleSpecifier")
|
|
68
|
+
.build("local");
|
|
69
|
+
|
|
70
|
+
def("ExportDefaultDeclaration")
|
|
71
|
+
.bases("Declaration")
|
|
72
|
+
.build("declaration")
|
|
73
|
+
.field("declaration", or(def("Declaration"), def("Expression")));
|
|
74
|
+
|
|
75
|
+
def("ExportNamedDeclaration")
|
|
76
|
+
.bases("Declaration")
|
|
77
|
+
.build("declaration", "specifiers", "source")
|
|
78
|
+
.field("declaration", or(def("Declaration"), null))
|
|
79
|
+
.field("specifiers", [def("ExportSpecifier")], defaults.emptyArray)
|
|
80
|
+
.field("source", or(def("Literal"), null), defaults["null"]);
|
|
81
|
+
|
|
82
|
+
def("ExportSpecifier")
|
|
83
|
+
.bases("ModuleSpecifier")
|
|
84
|
+
.build("local", "exported")
|
|
85
|
+
.field("exported", def("Identifier"));
|
|
86
|
+
|
|
87
|
+
def("ExportNamespaceSpecifier")
|
|
88
|
+
.bases("Specifier")
|
|
89
|
+
.build("exported")
|
|
90
|
+
.field("exported", def("Identifier"));
|
|
91
|
+
|
|
92
|
+
def("ExportDefaultSpecifier")
|
|
93
|
+
.bases("Specifier")
|
|
94
|
+
.build("exported")
|
|
95
|
+
.field("exported", def("Identifier"));
|
|
96
|
+
|
|
97
|
+
def("ExportAllDeclaration")
|
|
98
|
+
.bases("Declaration")
|
|
99
|
+
.build("exported", "source")
|
|
100
|
+
.field("exported", or(def("Identifier"), null))
|
|
101
|
+
.field("source", def("Literal"));
|
package/def/core.js
CHANGED
|
@@ -2,11 +2,6 @@ var types = require("../lib/types");
|
|
|
2
2
|
var Type = types.Type;
|
|
3
3
|
var def = Type.def;
|
|
4
4
|
var or = Type.or;
|
|
5
|
-
var builtin = types.builtInTypes;
|
|
6
|
-
var isString = builtin.string;
|
|
7
|
-
var isNumber = builtin.number;
|
|
8
|
-
var isBoolean = builtin.boolean;
|
|
9
|
-
var isRegExp = builtin.RegExp;
|
|
10
5
|
var shared = require("../lib/shared");
|
|
11
6
|
var defaults = shared.defaults;
|
|
12
7
|
var geq = shared.geq;
|
|
@@ -21,7 +16,7 @@ def("Printable")
|
|
|
21
16
|
|
|
22
17
|
def("Node")
|
|
23
18
|
.bases("Printable")
|
|
24
|
-
.field("type",
|
|
19
|
+
.field("type", String)
|
|
25
20
|
.field("comments", or(
|
|
26
21
|
[def("Comment")],
|
|
27
22
|
null
|
|
@@ -31,13 +26,18 @@ def("SourceLocation")
|
|
|
31
26
|
.build("start", "end", "source")
|
|
32
27
|
.field("start", def("Position"))
|
|
33
28
|
.field("end", def("Position"))
|
|
34
|
-
.field("source", or(
|
|
29
|
+
.field("source", or(String, null), defaults["null"]);
|
|
35
30
|
|
|
36
31
|
def("Position")
|
|
37
32
|
.build("line", "column")
|
|
38
33
|
.field("line", geq(1))
|
|
39
34
|
.field("column", geq(0));
|
|
40
35
|
|
|
36
|
+
def("File")
|
|
37
|
+
.bases("Node")
|
|
38
|
+
.build("program")
|
|
39
|
+
.field("program", def("Program"));
|
|
40
|
+
|
|
41
41
|
def("Program")
|
|
42
42
|
.bases("Node")
|
|
43
43
|
.build("body")
|
|
@@ -101,7 +101,7 @@ def("SwitchStatement")
|
|
|
101
101
|
.build("discriminant", "cases", "lexical")
|
|
102
102
|
.field("discriminant", def("Expression"))
|
|
103
103
|
.field("cases", [def("SwitchCase")])
|
|
104
|
-
.field("lexical",
|
|
104
|
+
.field("lexical", Boolean, defaults["false"]);
|
|
105
105
|
|
|
106
106
|
def("ReturnStatement")
|
|
107
107
|
.bases("Statement")
|
|
@@ -158,13 +158,12 @@ def("ForStatement")
|
|
|
158
158
|
|
|
159
159
|
def("ForInStatement")
|
|
160
160
|
.bases("Statement")
|
|
161
|
-
.build("left", "right", "body"
|
|
161
|
+
.build("left", "right", "body")
|
|
162
162
|
.field("left", or(
|
|
163
163
|
def("VariableDeclaration"),
|
|
164
164
|
def("Expression")))
|
|
165
165
|
.field("right", def("Expression"))
|
|
166
|
-
.field("body", def("Statement"))
|
|
167
|
-
.field("each", isBoolean);
|
|
166
|
+
.field("body", def("Statement"));
|
|
168
167
|
|
|
169
168
|
def("DebuggerStatement").bases("Statement").build();
|
|
170
169
|
|
|
@@ -183,10 +182,7 @@ def("VariableDeclaration")
|
|
|
183
182
|
.bases("Declaration")
|
|
184
183
|
.build("kind", "declarations")
|
|
185
184
|
.field("kind", or("var", "let", "const"))
|
|
186
|
-
.field("declarations", [
|
|
187
|
-
def("VariableDeclarator"),
|
|
188
|
-
def("Identifier") // TODO Esprima deviation.
|
|
189
|
-
)]);
|
|
185
|
+
.field("declarations", [def("VariableDeclarator")]);
|
|
190
186
|
|
|
191
187
|
def("VariableDeclarator")
|
|
192
188
|
.bases("Node")
|
|
@@ -215,8 +211,7 @@ def("Property")
|
|
|
215
211
|
.build("kind", "key", "value")
|
|
216
212
|
.field("kind", or("init", "get", "set"))
|
|
217
213
|
.field("key", or(def("Literal"), def("Identifier")))
|
|
218
|
-
|
|
219
|
-
.field("value", or(def("Expression"), def("Pattern")));
|
|
214
|
+
.field("value", def("Expression"));
|
|
220
215
|
|
|
221
216
|
def("SequenceExpression")
|
|
222
217
|
.bases("Expression")
|
|
@@ -232,9 +227,9 @@ def("UnaryExpression")
|
|
|
232
227
|
.build("operator", "argument", "prefix")
|
|
233
228
|
.field("operator", UnaryOperator)
|
|
234
229
|
.field("argument", def("Expression"))
|
|
235
|
-
//
|
|
236
|
-
//
|
|
237
|
-
.field("prefix",
|
|
230
|
+
// Esprima doesn't bother with this field, presumably because it's
|
|
231
|
+
// always true for unary operators.
|
|
232
|
+
.field("prefix", Boolean, defaults["true"]);
|
|
238
233
|
|
|
239
234
|
var BinaryOperator = or(
|
|
240
235
|
"==", "!=", "===", "!==",
|
|
@@ -271,7 +266,7 @@ def("UpdateExpression")
|
|
|
271
266
|
.build("operator", "argument", "prefix")
|
|
272
267
|
.field("operator", UpdateOperator)
|
|
273
268
|
.field("argument", def("Expression"))
|
|
274
|
-
.field("prefix",
|
|
269
|
+
.field("prefix", Boolean);
|
|
275
270
|
|
|
276
271
|
var LogicalOperator = or("||", "&&");
|
|
277
272
|
|
|
@@ -310,28 +305,10 @@ def("MemberExpression")
|
|
|
310
305
|
.build("object", "property", "computed")
|
|
311
306
|
.field("object", def("Expression"))
|
|
312
307
|
.field("property", or(def("Identifier"), def("Expression")))
|
|
313
|
-
.field("computed",
|
|
308
|
+
.field("computed", Boolean, defaults["false"]);
|
|
314
309
|
|
|
315
310
|
def("Pattern").bases("Node");
|
|
316
311
|
|
|
317
|
-
def("ObjectPattern")
|
|
318
|
-
.bases("Pattern")
|
|
319
|
-
.build("properties")
|
|
320
|
-
// TODO File a bug to get PropertyPattern added to the interfaces API.
|
|
321
|
-
// esprima uses Property
|
|
322
|
-
.field("properties", [or(def("PropertyPattern"), def("Property"))]);
|
|
323
|
-
|
|
324
|
-
def("PropertyPattern")
|
|
325
|
-
.bases("Pattern")
|
|
326
|
-
.build("key", "pattern")
|
|
327
|
-
.field("key", or(def("Literal"), def("Identifier")))
|
|
328
|
-
.field("pattern", def("Pattern"));
|
|
329
|
-
|
|
330
|
-
def("ArrayPattern")
|
|
331
|
-
.bases("Pattern")
|
|
332
|
-
.build("elements")
|
|
333
|
-
.field("elements", [or(def("Pattern"), null)]);
|
|
334
|
-
|
|
335
312
|
def("SwitchCase")
|
|
336
313
|
.bases("Node")
|
|
337
314
|
.build("test", "consequent")
|
|
@@ -342,48 +319,44 @@ def("Identifier")
|
|
|
342
319
|
// But aren't Expressions and Patterns already Nodes? TODO Report this.
|
|
343
320
|
.bases("Node", "Expression", "Pattern")
|
|
344
321
|
.build("name")
|
|
345
|
-
.field("name",
|
|
322
|
+
.field("name", String);
|
|
346
323
|
|
|
347
324
|
def("Literal")
|
|
348
325
|
// But aren't Expressions already Nodes? TODO Report this.
|
|
349
326
|
.bases("Node", "Expression")
|
|
350
327
|
.build("value")
|
|
351
|
-
.field("value", or(
|
|
352
|
-
isString,
|
|
353
|
-
isBoolean,
|
|
354
|
-
null, // isNull would also work here.
|
|
355
|
-
isNumber,
|
|
356
|
-
isRegExp
|
|
357
|
-
))
|
|
328
|
+
.field("value", or(String, Boolean, null, Number, RegExp))
|
|
358
329
|
.field("regex", or({
|
|
359
|
-
pattern:
|
|
360
|
-
flags:
|
|
330
|
+
pattern: String,
|
|
331
|
+
flags: String
|
|
361
332
|
}, null), function() {
|
|
362
|
-
if (
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
}
|
|
333
|
+
if (this.value instanceof RegExp) {
|
|
334
|
+
var flags = "";
|
|
335
|
+
|
|
336
|
+
if (this.value.ignoreCase) flags += "i";
|
|
337
|
+
if (this.value.multiline) flags += "m";
|
|
338
|
+
if (this.value.global) flags += "g";
|
|
339
|
+
|
|
340
|
+
return {
|
|
341
|
+
pattern: this.value.source,
|
|
342
|
+
flags: flags
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return null;
|
|
374
347
|
});
|
|
375
348
|
|
|
376
349
|
// Abstract (non-buildable) comment supertype. Not a Node.
|
|
377
350
|
def("Comment")
|
|
378
351
|
.bases("Printable")
|
|
379
|
-
.field("value",
|
|
352
|
+
.field("value", String)
|
|
380
353
|
// A .leading comment comes before the node, whereas a .trailing
|
|
381
354
|
// comment comes after it. These two fields should not both be true,
|
|
382
355
|
// but they might both be false when the comment falls inside a node
|
|
383
356
|
// and the node has no children for the comment to lead or trail,
|
|
384
357
|
// e.g. { /*dangling*/ }.
|
|
385
|
-
.field("leading",
|
|
386
|
-
.field("trailing",
|
|
358
|
+
.field("leading", Boolean, defaults["true"])
|
|
359
|
+
.field("trailing", Boolean, defaults["false"]);
|
|
387
360
|
|
|
388
361
|
// Block comment. The .type really should be BlockComment rather than
|
|
389
362
|
// Block, but that's what we're stuck with for now.
|
package/def/e4x.js
CHANGED
|
@@ -2,9 +2,6 @@ require("./core");
|
|
|
2
2
|
var types = require("../lib/types");
|
|
3
3
|
var def = types.Type.def;
|
|
4
4
|
var or = types.Type.or;
|
|
5
|
-
var builtin = types.builtInTypes;
|
|
6
|
-
var isString = builtin.string;
|
|
7
|
-
var isBoolean = builtin.boolean;
|
|
8
5
|
|
|
9
6
|
// Note that none of these types are buildable because the Mozilla Parser
|
|
10
7
|
// API doesn't specify any builder functions, and nobody uses E4X anymore.
|
|
@@ -19,12 +16,12 @@ def("XMLQualifiedIdentifier")
|
|
|
19
16
|
.bases("Expression")
|
|
20
17
|
.field("left", or(def("Identifier"), def("XMLAnyName")))
|
|
21
18
|
.field("right", or(def("Identifier"), def("Expression")))
|
|
22
|
-
.field("computed",
|
|
19
|
+
.field("computed", Boolean);
|
|
23
20
|
|
|
24
21
|
def("XMLFunctionQualifiedIdentifier")
|
|
25
22
|
.bases("Expression")
|
|
26
23
|
.field("right", or(def("Identifier"), def("Expression")))
|
|
27
|
-
.field("computed",
|
|
24
|
+
.field("computed", Boolean);
|
|
28
25
|
|
|
29
26
|
def("XMLAttributeSelector")
|
|
30
27
|
.bases("Expression")
|
|
@@ -51,7 +48,7 @@ def("XMLEscape")
|
|
|
51
48
|
|
|
52
49
|
def("XMLText")
|
|
53
50
|
.bases("XML")
|
|
54
|
-
.field("text",
|
|
51
|
+
.field("text", String);
|
|
55
52
|
|
|
56
53
|
def("XMLStartTag")
|
|
57
54
|
.bases("XML")
|
|
@@ -67,21 +64,21 @@ def("XMLPointTag")
|
|
|
67
64
|
|
|
68
65
|
def("XMLName")
|
|
69
66
|
.bases("XML")
|
|
70
|
-
.field("contents", or(
|
|
67
|
+
.field("contents", or(String, [def("XML")]));
|
|
71
68
|
|
|
72
69
|
def("XMLAttribute")
|
|
73
70
|
.bases("XML")
|
|
74
|
-
.field("value",
|
|
71
|
+
.field("value", String);
|
|
75
72
|
|
|
76
73
|
def("XMLCdata")
|
|
77
74
|
.bases("XML")
|
|
78
|
-
.field("contents",
|
|
75
|
+
.field("contents", String);
|
|
79
76
|
|
|
80
77
|
def("XMLComment")
|
|
81
78
|
.bases("XML")
|
|
82
|
-
.field("contents",
|
|
79
|
+
.field("contents", String);
|
|
83
80
|
|
|
84
81
|
def("XMLProcessingInstruction")
|
|
85
82
|
.bases("XML")
|
|
86
|
-
.field("target",
|
|
87
|
-
.field("contents", or(
|
|
83
|
+
.field("target", String)
|
|
84
|
+
.field("contents", or(String, null));
|
package/def/es6.js
CHANGED
|
@@ -2,27 +2,34 @@ require("./core");
|
|
|
2
2
|
var types = require("../lib/types");
|
|
3
3
|
var def = types.Type.def;
|
|
4
4
|
var or = types.Type.or;
|
|
5
|
-
var builtin = types.builtInTypes;
|
|
6
|
-
var isBoolean = builtin.boolean;
|
|
7
|
-
var isObject = builtin.object;
|
|
8
|
-
var isString = builtin.string;
|
|
9
5
|
var defaults = require("../lib/shared").defaults;
|
|
10
6
|
|
|
11
7
|
def("Function")
|
|
12
|
-
.field("generator",
|
|
13
|
-
.field("expression",
|
|
8
|
+
.field("generator", Boolean, defaults["false"])
|
|
9
|
+
.field("expression", Boolean, defaults["false"])
|
|
14
10
|
.field("defaults", [or(def("Expression"), null)], defaults.emptyArray)
|
|
15
|
-
// TODO This could be represented as a
|
|
11
|
+
// TODO This could be represented as a RestElement in .params.
|
|
16
12
|
.field("rest", or(def("Identifier"), null), defaults["null"]);
|
|
17
13
|
|
|
14
|
+
// The ESTree way of representing a ...rest parameter.
|
|
15
|
+
def("RestElement")
|
|
16
|
+
.bases("Pattern")
|
|
17
|
+
.build("argument")
|
|
18
|
+
.field("argument", def("Pattern"));
|
|
19
|
+
|
|
20
|
+
def("SpreadElementPattern")
|
|
21
|
+
.bases("Pattern")
|
|
22
|
+
.build("argument")
|
|
23
|
+
.field("argument", def("Pattern"));
|
|
24
|
+
|
|
18
25
|
def("FunctionDeclaration")
|
|
19
26
|
.build("id", "params", "body", "generator", "expression");
|
|
20
27
|
|
|
21
28
|
def("FunctionExpression")
|
|
22
29
|
.build("id", "params", "body", "generator", "expression");
|
|
23
30
|
|
|
24
|
-
//
|
|
25
|
-
// ArrowFunctionExpression.
|
|
31
|
+
// The Parser API calls this ArrowExpression, but Esprima and all other
|
|
32
|
+
// actual parsers use ArrowFunctionExpression.
|
|
26
33
|
def("ArrowFunctionExpression")
|
|
27
34
|
.bases("Function", "Expression")
|
|
28
35
|
.build("params", "body", "expression")
|
|
@@ -39,7 +46,7 @@ def("YieldExpression")
|
|
|
39
46
|
.bases("Expression")
|
|
40
47
|
.build("argument", "delegate")
|
|
41
48
|
.field("argument", or(def("Expression"), null))
|
|
42
|
-
.field("delegate",
|
|
49
|
+
.field("delegate", Boolean, defaults["false"]);
|
|
43
50
|
|
|
44
51
|
def("GeneratorExpression")
|
|
45
52
|
.bases("Expression")
|
|
@@ -60,32 +67,40 @@ def("ComprehensionBlock")
|
|
|
60
67
|
.build("left", "right", "each")
|
|
61
68
|
.field("left", def("Pattern"))
|
|
62
69
|
.field("right", def("Expression"))
|
|
63
|
-
.field("each",
|
|
64
|
-
|
|
65
|
-
def("ModuleSpecifier")
|
|
66
|
-
.bases("Literal")
|
|
67
|
-
.build("value")
|
|
68
|
-
.field("value", isString);
|
|
70
|
+
.field("each", Boolean);
|
|
69
71
|
|
|
70
72
|
def("Property")
|
|
71
|
-
// Esprima extensions not mentioned in the Mozilla Parser API:
|
|
72
73
|
.field("key", or(def("Literal"), def("Identifier"), def("Expression")))
|
|
73
|
-
.field("
|
|
74
|
-
.field("
|
|
75
|
-
.field("
|
|
74
|
+
.field("value", or(def("Expression"), def("Pattern")))
|
|
75
|
+
.field("method", Boolean, defaults["false"])
|
|
76
|
+
.field("shorthand", Boolean, defaults["false"])
|
|
77
|
+
.field("computed", Boolean, defaults["false"]);
|
|
76
78
|
|
|
77
79
|
def("PropertyPattern")
|
|
80
|
+
.bases("Pattern")
|
|
81
|
+
.build("key", "pattern")
|
|
78
82
|
.field("key", or(def("Literal"), def("Identifier"), def("Expression")))
|
|
79
|
-
.field("
|
|
83
|
+
.field("pattern", def("Pattern"))
|
|
84
|
+
.field("computed", Boolean, defaults["false"]);
|
|
85
|
+
|
|
86
|
+
def("ObjectPattern")
|
|
87
|
+
.bases("Pattern")
|
|
88
|
+
.build("properties")
|
|
89
|
+
.field("properties", [or(def("PropertyPattern"), def("Property"))]);
|
|
90
|
+
|
|
91
|
+
def("ArrayPattern")
|
|
92
|
+
.bases("Pattern")
|
|
93
|
+
.build("elements")
|
|
94
|
+
.field("elements", [or(def("Pattern"), null)]);
|
|
80
95
|
|
|
81
96
|
def("MethodDefinition")
|
|
82
97
|
.bases("Declaration")
|
|
83
98
|
.build("kind", "key", "value", "static")
|
|
84
|
-
.field("kind", or("
|
|
99
|
+
.field("kind", or("constructor", "method", "get", "set"))
|
|
85
100
|
.field("key", or(def("Literal"), def("Identifier"), def("Expression")))
|
|
86
101
|
.field("value", def("Function"))
|
|
87
|
-
.field("computed",
|
|
88
|
-
.field("static",
|
|
102
|
+
.field("computed", Boolean, defaults["false"])
|
|
103
|
+
.field("static", Boolean, defaults["false"]);
|
|
89
104
|
|
|
90
105
|
def("SpreadElement")
|
|
91
106
|
.bases("Node")
|
|
@@ -101,18 +116,17 @@ def("NewExpression")
|
|
|
101
116
|
def("CallExpression")
|
|
102
117
|
.field("arguments", [or(def("Expression"), def("SpreadElement"))]);
|
|
103
118
|
|
|
104
|
-
|
|
119
|
+
// Note: this node type is *not* an AssignmentExpression with a Pattern on
|
|
120
|
+
// the left-hand side! The existing AssignmentExpression type already
|
|
121
|
+
// supports destructuring assignments. AssignmentPattern nodes may appear
|
|
122
|
+
// wherever a Pattern is allowed, and the right-hand side represents a
|
|
123
|
+
// default value to be destructured against the left-hand side, if no
|
|
124
|
+
// value is otherwise provided. For example: default parameter values.
|
|
125
|
+
def("AssignmentPattern")
|
|
105
126
|
.bases("Pattern")
|
|
106
|
-
.build("
|
|
107
|
-
.field("
|
|
108
|
-
|
|
109
|
-
def("ArrayPattern")
|
|
110
|
-
.field("elements", [or(
|
|
111
|
-
def("Pattern"),
|
|
112
|
-
null,
|
|
113
|
-
// used by esprima
|
|
114
|
-
def("SpreadElement")
|
|
115
|
-
)]);
|
|
127
|
+
.build("left", "right")
|
|
128
|
+
.field("left", def("Pattern"))
|
|
129
|
+
.field("right", def("Expression"));
|
|
116
130
|
|
|
117
131
|
var ClassBodyElement = or(
|
|
118
132
|
def("MethodDefinition"),
|
|
@@ -125,7 +139,7 @@ def("ClassProperty")
|
|
|
125
139
|
.bases("Declaration")
|
|
126
140
|
.build("key")
|
|
127
141
|
.field("key", or(def("Literal"), def("Identifier"), def("Expression")))
|
|
128
|
-
.field("computed",
|
|
142
|
+
.field("computed", Boolean, defaults["false"]);
|
|
129
143
|
|
|
130
144
|
def("ClassPropertyDefinition") // static property
|
|
131
145
|
.bases("Declaration")
|
|
@@ -162,75 +176,10 @@ def("ClassImplements")
|
|
|
162
176
|
// Specifier and NamedSpecifier are abstract non-standard types that I
|
|
163
177
|
// introduced for definitional convenience.
|
|
164
178
|
def("Specifier").bases("Node");
|
|
165
|
-
def("NamedSpecifier")
|
|
166
|
-
.bases("Specifier")
|
|
167
|
-
// Note: this abstract type is intentionally not buildable.
|
|
168
|
-
.field("id", def("Identifier"))
|
|
169
|
-
.field("name", or(def("Identifier"), null), defaults["null"]);
|
|
170
|
-
|
|
171
|
-
// Like NamedSpecifier, except type:"ExportSpecifier" and buildable.
|
|
172
|
-
// export {<id [as name]>} [from ...];
|
|
173
|
-
def("ExportSpecifier")
|
|
174
|
-
.bases("NamedSpecifier")
|
|
175
|
-
.build("id", "name");
|
|
176
|
-
|
|
177
|
-
// export <*> from ...;
|
|
178
|
-
def("ExportBatchSpecifier")
|
|
179
|
-
.bases("Specifier")
|
|
180
|
-
.build();
|
|
181
|
-
|
|
182
|
-
// Like NamedSpecifier, except type:"ImportSpecifier" and buildable.
|
|
183
|
-
// import {<id [as name]>} from ...;
|
|
184
|
-
def("ImportSpecifier")
|
|
185
|
-
.bases("NamedSpecifier")
|
|
186
|
-
.build("id", "name");
|
|
187
|
-
|
|
188
|
-
// import <* as id> from ...;
|
|
189
|
-
def("ImportNamespaceSpecifier")
|
|
190
|
-
.bases("Specifier")
|
|
191
|
-
.build("id")
|
|
192
|
-
.field("id", def("Identifier"));
|
|
193
|
-
|
|
194
|
-
// import <id> from ...;
|
|
195
|
-
def("ImportDefaultSpecifier")
|
|
196
|
-
.bases("Specifier")
|
|
197
|
-
.build("id")
|
|
198
|
-
.field("id", def("Identifier"));
|
|
199
|
-
|
|
200
|
-
def("ExportDeclaration")
|
|
201
|
-
.bases("Declaration")
|
|
202
|
-
.build("default", "declaration", "specifiers", "source")
|
|
203
|
-
.field("default", isBoolean)
|
|
204
|
-
.field("declaration", or(
|
|
205
|
-
def("Declaration"),
|
|
206
|
-
def("Expression"), // Implies default.
|
|
207
|
-
null
|
|
208
|
-
))
|
|
209
|
-
.field("specifiers", [or(
|
|
210
|
-
def("ExportSpecifier"),
|
|
211
|
-
def("ExportBatchSpecifier")
|
|
212
|
-
)], defaults.emptyArray)
|
|
213
|
-
.field("source", or(
|
|
214
|
-
def("Literal"),
|
|
215
|
-
def("ModuleSpecifier"),
|
|
216
|
-
null
|
|
217
|
-
), defaults["null"]);
|
|
218
|
-
|
|
219
|
-
def("ImportDeclaration")
|
|
220
|
-
.bases("Declaration")
|
|
221
|
-
.build("specifiers", "source")
|
|
222
|
-
.field("specifiers", [or(
|
|
223
|
-
def("ImportSpecifier"),
|
|
224
|
-
def("ImportNamespaceSpecifier"),
|
|
225
|
-
def("ImportDefaultSpecifier")
|
|
226
|
-
)], defaults.emptyArray)
|
|
227
|
-
.field("source", or(
|
|
228
|
-
def("Literal"),
|
|
229
|
-
def("ModuleSpecifier")
|
|
230
|
-
));
|
|
231
179
|
|
|
232
180
|
def("TaggedTemplateExpression")
|
|
233
181
|
.bases("Expression")
|
|
182
|
+
.build("tag", "quasi")
|
|
234
183
|
.field("tag", def("Expression"))
|
|
235
184
|
.field("quasi", def("TemplateLiteral"));
|
|
236
185
|
|
|
@@ -243,5 +192,5 @@ def("TemplateLiteral")
|
|
|
243
192
|
def("TemplateElement")
|
|
244
193
|
.bases("Node")
|
|
245
194
|
.build("value", "tail")
|
|
246
|
-
.field("value", {"cooked":
|
|
247
|
-
.field("tail",
|
|
195
|
+
.field("value", {"cooked": String, "raw": String})
|
|
196
|
+
.field("tail", Boolean);
|
package/def/es7.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
require("./
|
|
1
|
+
require("./es6");
|
|
2
|
+
|
|
2
3
|
var types = require("../lib/types");
|
|
3
4
|
var def = types.Type.def;
|
|
4
5
|
var or = types.Type.or;
|
|
5
6
|
var builtin = types.builtInTypes;
|
|
6
|
-
var isBoolean = builtin.boolean;
|
|
7
7
|
var defaults = require("../lib/shared").defaults;
|
|
8
8
|
|
|
9
9
|
def("Function")
|
|
10
|
-
.field("async",
|
|
10
|
+
.field("async", Boolean, defaults["false"]);
|
|
11
11
|
|
|
12
12
|
def("SpreadProperty")
|
|
13
13
|
.bases("Node")
|
|
@@ -24,15 +24,13 @@ def("SpreadPropertyPattern")
|
|
|
24
24
|
|
|
25
25
|
def("ObjectPattern")
|
|
26
26
|
.field("properties", [or(
|
|
27
|
-
def("PropertyPattern"),
|
|
28
|
-
def("SpreadPropertyPattern"),
|
|
29
|
-
// used by esprima
|
|
30
27
|
def("Property"),
|
|
31
|
-
def("
|
|
28
|
+
def("PropertyPattern"),
|
|
29
|
+
def("SpreadPropertyPattern")
|
|
32
30
|
)]);
|
|
33
31
|
|
|
34
32
|
def("AwaitExpression")
|
|
35
33
|
.bases("Expression")
|
|
36
34
|
.build("argument", "all")
|
|
37
35
|
.field("argument", or(def("Expression"), null))
|
|
38
|
-
.field("all",
|
|
36
|
+
.field("all", Boolean, defaults["false"]);
|
package/def/esprima.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
require("./es7");
|
|
2
|
+
|
|
3
|
+
var types = require("../lib/types");
|
|
4
|
+
var defaults = require("../lib/shared").defaults;
|
|
5
|
+
var def = types.Type.def;
|
|
6
|
+
var or = types.Type.or;
|
|
7
|
+
|
|
8
|
+
def("VariableDeclaration")
|
|
9
|
+
.field("declarations", [or(
|
|
10
|
+
def("VariableDeclarator"),
|
|
11
|
+
def("Identifier") // Esprima deviation.
|
|
12
|
+
)]);
|
|
13
|
+
|
|
14
|
+
def("Property")
|
|
15
|
+
.field("value", or(
|
|
16
|
+
def("Expression"),
|
|
17
|
+
def("Pattern") // Esprima deviation.
|
|
18
|
+
));
|
|
19
|
+
|
|
20
|
+
def("ArrayPattern")
|
|
21
|
+
.field("elements", [or(
|
|
22
|
+
def("Pattern"),
|
|
23
|
+
def("SpreadElement"),
|
|
24
|
+
null
|
|
25
|
+
)]);
|
|
26
|
+
|
|
27
|
+
def("ObjectPattern")
|
|
28
|
+
.field("properties", [or(
|
|
29
|
+
def("Property"),
|
|
30
|
+
def("PropertyPattern"),
|
|
31
|
+
def("SpreadPropertyPattern"),
|
|
32
|
+
def("SpreadProperty") // Used by Esprima.
|
|
33
|
+
)]);
|
|
34
|
+
|
|
35
|
+
def("NamedSpecifier")
|
|
36
|
+
.bases("Specifier")
|
|
37
|
+
// Note: this abstract type is intentionally not buildable.
|
|
38
|
+
.field("id", def("Identifier"))
|
|
39
|
+
.field("name", or(def("Identifier"), null), defaults["null"]);
|
|
40
|
+
|
|
41
|
+
// Like NamedSpecifier, except type:"ExportSpecifier" and buildable.
|
|
42
|
+
// export {<id [as name]>} [from ...];
|
|
43
|
+
def("ExportSpecifier")
|
|
44
|
+
.bases("NamedSpecifier")
|
|
45
|
+
.build("id", "name");
|
|
46
|
+
|
|
47
|
+
// export <*> from ...;
|
|
48
|
+
def("ExportBatchSpecifier")
|
|
49
|
+
.bases("Specifier")
|
|
50
|
+
.build();
|
|
51
|
+
|
|
52
|
+
// Like NamedSpecifier, except type:"ImportSpecifier" and buildable.
|
|
53
|
+
// import {<id [as name]>} from ...;
|
|
54
|
+
def("ImportSpecifier")
|
|
55
|
+
.bases("NamedSpecifier")
|
|
56
|
+
.build("id", "name");
|
|
57
|
+
|
|
58
|
+
// import <* as id> from ...;
|
|
59
|
+
def("ImportNamespaceSpecifier")
|
|
60
|
+
.bases("Specifier")
|
|
61
|
+
.build("id")
|
|
62
|
+
.field("id", def("Identifier"));
|
|
63
|
+
|
|
64
|
+
// import <id> from ...;
|
|
65
|
+
def("ImportDefaultSpecifier")
|
|
66
|
+
.bases("Specifier")
|
|
67
|
+
.build("id")
|
|
68
|
+
.field("id", def("Identifier"));
|
|
69
|
+
|
|
70
|
+
def("ExportDeclaration")
|
|
71
|
+
.bases("Declaration")
|
|
72
|
+
.build("default", "declaration", "specifiers", "source")
|
|
73
|
+
.field("default", Boolean)
|
|
74
|
+
.field("declaration", or(
|
|
75
|
+
def("Declaration"),
|
|
76
|
+
def("Expression"), // Implies default.
|
|
77
|
+
null
|
|
78
|
+
))
|
|
79
|
+
.field("specifiers", [or(
|
|
80
|
+
def("ExportSpecifier"),
|
|
81
|
+
def("ExportBatchSpecifier")
|
|
82
|
+
)], defaults.emptyArray)
|
|
83
|
+
.field("source", or(
|
|
84
|
+
def("Literal"),
|
|
85
|
+
null
|
|
86
|
+
), defaults["null"]);
|
|
87
|
+
|
|
88
|
+
def("ImportDeclaration")
|
|
89
|
+
.bases("Declaration")
|
|
90
|
+
.build("specifiers", "source")
|
|
91
|
+
.field("specifiers", [or(
|
|
92
|
+
def("ImportSpecifier"),
|
|
93
|
+
def("ImportNamespaceSpecifier"),
|
|
94
|
+
def("ImportDefaultSpecifier")
|
|
95
|
+
)], defaults.emptyArray)
|
|
96
|
+
.field("source", def("Literal"));
|
package/def/fb-harmony.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
require("./
|
|
1
|
+
require("./es7");
|
|
2
|
+
|
|
2
3
|
var types = require("../lib/types");
|
|
3
4
|
var def = types.Type.def;
|
|
4
5
|
var or = types.Type.or;
|
|
5
|
-
var builtin = types.builtInTypes;
|
|
6
|
-
var isString = builtin.string;
|
|
7
|
-
var isNumber = builtin.number;
|
|
8
|
-
var isBoolean = builtin.boolean;
|
|
9
6
|
var defaults = require("../lib/shared").defaults;
|
|
10
7
|
|
|
11
8
|
def("JSXAttribute")
|
|
@@ -21,7 +18,7 @@ def("JSXAttribute")
|
|
|
21
18
|
def("JSXIdentifier")
|
|
22
19
|
.bases("Identifier")
|
|
23
20
|
.build("name")
|
|
24
|
-
.field("name",
|
|
21
|
+
.field("name", String);
|
|
25
22
|
|
|
26
23
|
def("JSXNamespacedName")
|
|
27
24
|
.bases("Node")
|
|
@@ -34,7 +31,7 @@ def("JSXMemberExpression")
|
|
|
34
31
|
.build("object", "property")
|
|
35
32
|
.field("object", or(def("JSXIdentifier"), def("JSXMemberExpression")))
|
|
36
33
|
.field("property", def("JSXIdentifier"))
|
|
37
|
-
.field("computed",
|
|
34
|
+
.field("computed", Boolean, defaults.false);
|
|
38
35
|
|
|
39
36
|
var JSXElementName = or(
|
|
40
37
|
def("JSXIdentifier"),
|
|
@@ -76,7 +73,7 @@ def("JSXElement")
|
|
|
76
73
|
// guaranteed to be available.
|
|
77
74
|
return this.openingElement.name;
|
|
78
75
|
})
|
|
79
|
-
.field("selfClosing",
|
|
76
|
+
.field("selfClosing", Boolean, function() {
|
|
80
77
|
return this.openingElement.selfClosing;
|
|
81
78
|
})
|
|
82
79
|
.field("attributes", JSXAttributes, function() {
|
|
@@ -88,7 +85,7 @@ def("JSXOpeningElement")
|
|
|
88
85
|
.build("name", "attributes", "selfClosing")
|
|
89
86
|
.field("name", JSXElementName)
|
|
90
87
|
.field("attributes", JSXAttributes, defaults.emptyArray)
|
|
91
|
-
.field("selfClosing",
|
|
88
|
+
.field("selfClosing", Boolean, defaults["false"]);
|
|
92
89
|
|
|
93
90
|
def("JSXClosingElement")
|
|
94
91
|
.bases("Node") // TODO Same concern.
|
|
@@ -98,46 +95,54 @@ def("JSXClosingElement")
|
|
|
98
95
|
def("JSXText")
|
|
99
96
|
.bases("Literal")
|
|
100
97
|
.build("value")
|
|
101
|
-
.field("value",
|
|
98
|
+
.field("value", String);
|
|
102
99
|
|
|
103
100
|
def("JSXEmptyExpression").bases("Expression").build();
|
|
104
101
|
|
|
105
102
|
// Type Annotations
|
|
106
|
-
def("Type")
|
|
107
|
-
.bases("Node");
|
|
103
|
+
def("Type").bases("Node");
|
|
108
104
|
|
|
109
105
|
def("AnyTypeAnnotation")
|
|
110
|
-
.bases("Type")
|
|
106
|
+
.bases("Type")
|
|
107
|
+
.build();
|
|
108
|
+
|
|
109
|
+
def("MixedTypeAnnotation")
|
|
110
|
+
.bases("Type")
|
|
111
|
+
.build();
|
|
111
112
|
|
|
112
113
|
def("VoidTypeAnnotation")
|
|
113
|
-
.bases("Type")
|
|
114
|
+
.bases("Type")
|
|
115
|
+
.build();
|
|
114
116
|
|
|
115
117
|
def("NumberTypeAnnotation")
|
|
116
|
-
.bases("Type")
|
|
118
|
+
.bases("Type")
|
|
119
|
+
.build();
|
|
117
120
|
|
|
118
121
|
def("NumberLiteralTypeAnnotation")
|
|
119
122
|
.bases("Type")
|
|
120
123
|
.build("value", "raw")
|
|
121
|
-
.field("value",
|
|
122
|
-
.field("raw",
|
|
124
|
+
.field("value", Number)
|
|
125
|
+
.field("raw", String);
|
|
123
126
|
|
|
124
127
|
def("StringTypeAnnotation")
|
|
125
|
-
.bases("Type")
|
|
128
|
+
.bases("Type")
|
|
129
|
+
.build();
|
|
126
130
|
|
|
127
131
|
def("StringLiteralTypeAnnotation")
|
|
128
132
|
.bases("Type")
|
|
129
133
|
.build("value", "raw")
|
|
130
|
-
.field("value",
|
|
131
|
-
.field("raw",
|
|
134
|
+
.field("value", String)
|
|
135
|
+
.field("raw", String);
|
|
132
136
|
|
|
133
137
|
def("BooleanTypeAnnotation")
|
|
134
|
-
.bases("Type")
|
|
138
|
+
.bases("Type")
|
|
139
|
+
.build();
|
|
135
140
|
|
|
136
141
|
def("BooleanLiteralTypeAnnotation")
|
|
137
142
|
.bases("Type")
|
|
138
143
|
.build("value", "raw")
|
|
139
|
-
.field("value",
|
|
140
|
-
.field("raw",
|
|
144
|
+
.field("value", Boolean)
|
|
145
|
+
.field("raw", String);
|
|
141
146
|
|
|
142
147
|
def("TypeAnnotation")
|
|
143
148
|
.bases("Node")
|
|
@@ -162,7 +167,7 @@ def("FunctionTypeParam")
|
|
|
162
167
|
.build("name", "typeAnnotation", "optional")
|
|
163
168
|
.field("name", def("Identifier"))
|
|
164
169
|
.field("typeAnnotation", def("Type"))
|
|
165
|
-
.field("optional",
|
|
170
|
+
.field("optional", Boolean);
|
|
166
171
|
|
|
167
172
|
def("ArrayTypeAnnotation")
|
|
168
173
|
.bases("Type")
|
|
@@ -174,14 +179,16 @@ def("ObjectTypeAnnotation")
|
|
|
174
179
|
.build("properties")
|
|
175
180
|
.field("properties", [def("ObjectTypeProperty")])
|
|
176
181
|
.field("indexers", [def("ObjectTypeIndexer")], defaults.emptyArray)
|
|
177
|
-
.field("callProperties",
|
|
182
|
+
.field("callProperties",
|
|
183
|
+
[def("ObjectTypeCallProperty")],
|
|
184
|
+
defaults.emptyArray);
|
|
178
185
|
|
|
179
186
|
def("ObjectTypeProperty")
|
|
180
187
|
.bases("Node")
|
|
181
188
|
.build("key", "value", "optional")
|
|
182
189
|
.field("key", or(def("Literal"), def("Identifier")))
|
|
183
190
|
.field("value", def("Type"))
|
|
184
|
-
.field("optional",
|
|
191
|
+
.field("optional", Boolean);
|
|
185
192
|
|
|
186
193
|
def("ObjectTypeIndexer")
|
|
187
194
|
.bases("Node")
|
|
@@ -194,12 +201,14 @@ def("ObjectTypeCallProperty")
|
|
|
194
201
|
.bases("Node")
|
|
195
202
|
.build("value")
|
|
196
203
|
.field("value", def("FunctionTypeAnnotation"))
|
|
197
|
-
.field("static",
|
|
204
|
+
.field("static", Boolean, false);
|
|
198
205
|
|
|
199
206
|
def("QualifiedTypeIdentifier")
|
|
200
207
|
.bases("Node")
|
|
201
208
|
.build("qualification", "id")
|
|
202
|
-
.field("qualification",
|
|
209
|
+
.field("qualification",
|
|
210
|
+
or(def("Identifier"),
|
|
211
|
+
def("QualifiedTypeIdentifier")))
|
|
203
212
|
.field("id", def("Identifier"));
|
|
204
213
|
|
|
205
214
|
def("GenericTypeAnnotation")
|
|
@@ -212,7 +221,9 @@ def("MemberTypeAnnotation")
|
|
|
212
221
|
.bases("Type")
|
|
213
222
|
.build("object", "property")
|
|
214
223
|
.field("object", def("Identifier"))
|
|
215
|
-
.field("property",
|
|
224
|
+
.field("property",
|
|
225
|
+
or(def("MemberTypeAnnotation"),
|
|
226
|
+
def("GenericTypeAnnotation")));
|
|
216
227
|
|
|
217
228
|
def("UnionTypeAnnotation")
|
|
218
229
|
.bases("Type")
|
|
@@ -243,22 +254,30 @@ def("TypeParameterInstantiation")
|
|
|
243
254
|
.field("params", [def("Type")]);
|
|
244
255
|
|
|
245
256
|
def("Function")
|
|
246
|
-
.field("returnType",
|
|
247
|
-
|
|
257
|
+
.field("returnType",
|
|
258
|
+
or(def("TypeAnnotation"), null),
|
|
259
|
+
defaults["null"])
|
|
260
|
+
.field("typeParameters",
|
|
261
|
+
or(def("TypeParameterDeclaration"), null),
|
|
262
|
+
defaults["null"]);
|
|
248
263
|
|
|
249
264
|
def("ClassProperty")
|
|
250
265
|
.build("key", "typeAnnotation")
|
|
251
266
|
.field("typeAnnotation", def("TypeAnnotation"))
|
|
252
|
-
.field("static",
|
|
267
|
+
.field("static", Boolean, false);
|
|
253
268
|
|
|
254
269
|
def("ClassImplements")
|
|
255
|
-
.field("typeParameters",
|
|
270
|
+
.field("typeParameters",
|
|
271
|
+
or(def("TypeParameterInstantiation"), null),
|
|
272
|
+
defaults["null"]);
|
|
256
273
|
|
|
257
274
|
def("InterfaceDeclaration")
|
|
258
275
|
.bases("Statement")
|
|
259
276
|
.build("id", "body", "extends")
|
|
260
277
|
.field("id", def("Identifier"))
|
|
261
|
-
.field("typeParameters",
|
|
278
|
+
.field("typeParameters",
|
|
279
|
+
or(def("TypeParameterDeclaration"), null),
|
|
280
|
+
defaults["null"])
|
|
262
281
|
.field("body", def("ObjectTypeAnnotation"))
|
|
263
282
|
.field("extends", [def("InterfaceExtends")]);
|
|
264
283
|
|
package/def/mozilla.js
CHANGED
|
@@ -2,12 +2,18 @@ require("./core");
|
|
|
2
2
|
var types = require("../lib/types");
|
|
3
3
|
var def = types.Type.def;
|
|
4
4
|
var or = types.Type.or;
|
|
5
|
-
var
|
|
5
|
+
var shared = require("../lib/shared");
|
|
6
|
+
var geq = shared.geq;
|
|
7
|
+
var defaults = shared.defaults;
|
|
6
8
|
|
|
7
9
|
def("Function")
|
|
8
10
|
// SpiderMonkey allows expression closures: function(x) x+1
|
|
9
11
|
.field("body", or(def("BlockStatement"), def("Expression")));
|
|
10
12
|
|
|
13
|
+
def("ForInStatement")
|
|
14
|
+
.build("left", "right", "body", "each")
|
|
15
|
+
.field("each", Boolean, defaults["false"]);
|
|
16
|
+
|
|
11
17
|
def("ForOfStatement")
|
|
12
18
|
.bases("Statement")
|
|
13
19
|
.build("left", "right", "body")
|
package/lib/types.js
CHANGED
|
@@ -80,27 +80,33 @@ Tp.toString = function() {
|
|
|
80
80
|
return name + " type";
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
+
var builtInCtorFns = [];
|
|
84
|
+
var builtInCtorTypes = [];
|
|
83
85
|
var builtInTypes = {};
|
|
84
86
|
exports.builtInTypes = builtInTypes;
|
|
85
87
|
|
|
86
88
|
function defBuiltInType(example, name) {
|
|
87
89
|
var objStr = objToStr.call(example);
|
|
88
90
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
var type = new Type(function(value) {
|
|
92
|
+
return objToStr.call(value) === objStr;
|
|
93
|
+
}, name);
|
|
94
|
+
|
|
95
|
+
builtInTypes[name] = type;
|
|
96
|
+
|
|
97
|
+
if (example && typeof example.constructor === "function") {
|
|
98
|
+
builtInCtorFns.push(example.constructor);
|
|
99
|
+
builtInCtorTypes.push(type);
|
|
100
|
+
}
|
|
95
101
|
|
|
96
|
-
return
|
|
102
|
+
return type;
|
|
97
103
|
}
|
|
98
104
|
|
|
99
105
|
// These types check the underlying [[Class]] attribute of the given
|
|
100
106
|
// value, rather than using the problematic typeof operator. Note however
|
|
101
107
|
// that no subtyping is considered; so, for instance, isObject.check
|
|
102
108
|
// returns false for [], /./, new Date, and null.
|
|
103
|
-
var isString = defBuiltInType("", "string");
|
|
109
|
+
var isString = defBuiltInType("truthy", "string");
|
|
104
110
|
var isFunction = defBuiltInType(function(){}, "function");
|
|
105
111
|
var isArray = defBuiltInType([], "array");
|
|
106
112
|
var isObject = defBuiltInType({}, "object");
|
|
@@ -132,10 +138,17 @@ function toType(from, name) {
|
|
|
132
138
|
if (isObject.check(from))
|
|
133
139
|
return Type.fromObject(from);
|
|
134
140
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
141
|
+
if (isFunction.check(from)) {
|
|
142
|
+
var bicfIndex = builtInCtorFns.indexOf(from);
|
|
143
|
+
if (bicfIndex >= 0) {
|
|
144
|
+
return builtInCtorTypes[bicfIndex];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// If isFunction.check(from), and from is not a built-in
|
|
148
|
+
// constructor, assume from is a binary predicate function we can
|
|
149
|
+
// use to define the type.
|
|
138
150
|
return new Type(from, name);
|
|
151
|
+
}
|
|
139
152
|
|
|
140
153
|
// As a last resort, toType returns a type that matches any value that
|
|
141
154
|
// is === from. This is primarily useful for literal values like
|
|
@@ -324,7 +337,7 @@ exports.computeSupertypeLookupTable = function(candidates) {
|
|
|
324
337
|
for (var i = 0; i < typeNameCount; ++i) {
|
|
325
338
|
var typeName = typeNames[i];
|
|
326
339
|
var d = defCache[typeName];
|
|
327
|
-
assert.strictEqual(d.finalized, true);
|
|
340
|
+
assert.strictEqual(d.finalized, true, typeName);
|
|
328
341
|
for (var j = 0; j < d.supertypeList.length; ++j) {
|
|
329
342
|
var superTypeName = d.supertypeList[j];
|
|
330
343
|
if (hasOwn.call(candidates, superTypeName)) {
|
|
@@ -339,7 +352,7 @@ exports.computeSupertypeLookupTable = function(candidates) {
|
|
|
339
352
|
|
|
340
353
|
Dp.checkAllFields = function(value, deep) {
|
|
341
354
|
var allFields = this.allFields;
|
|
342
|
-
assert.strictEqual(this.finalized, true);
|
|
355
|
+
assert.strictEqual(this.finalized, true, this.typeName);
|
|
343
356
|
|
|
344
357
|
function checkFieldByName(name) {
|
|
345
358
|
var field = allFields[name];
|
|
@@ -403,11 +416,15 @@ Dp.check = function(value, deep) {
|
|
|
403
416
|
};
|
|
404
417
|
|
|
405
418
|
Dp.bases = function() {
|
|
419
|
+
var args = slice.call(arguments);
|
|
406
420
|
var bases = this.baseNames;
|
|
407
421
|
|
|
408
|
-
|
|
422
|
+
if (this.finalized) {
|
|
423
|
+
assert.deepEqual(args, bases);
|
|
424
|
+
return this;
|
|
425
|
+
}
|
|
409
426
|
|
|
410
|
-
|
|
427
|
+
args.forEach(function(baseName) {
|
|
411
428
|
isString.assert(baseName);
|
|
412
429
|
|
|
413
430
|
// This indexOf lookup may be O(n), but the typical number of base
|
|
@@ -450,6 +467,8 @@ exports.defineMethod = function(name, func) {
|
|
|
450
467
|
return old;
|
|
451
468
|
};
|
|
452
469
|
|
|
470
|
+
var isArrayOfString = isString.arrayOf();
|
|
471
|
+
|
|
453
472
|
// Calling the .build method of a Def simultaneously marks the type as
|
|
454
473
|
// buildable (by defining builders[getBuilderName(typeName)]) and
|
|
455
474
|
// specifies the order of arguments that should be passed to the builder
|
|
@@ -457,18 +476,18 @@ exports.defineMethod = function(name, func) {
|
|
|
457
476
|
Dp.build = function(/* param1, param2, ... */) {
|
|
458
477
|
var self = this;
|
|
459
478
|
|
|
479
|
+
var newBuildParams = slice.call(arguments);
|
|
480
|
+
isArrayOfString.assert(newBuildParams);
|
|
481
|
+
|
|
460
482
|
// Calling Def.prototype.build multiple times has the effect of merely
|
|
461
483
|
// redefining this property.
|
|
462
484
|
Object.defineProperty(self, "buildParams", {
|
|
463
|
-
value:
|
|
485
|
+
value: newBuildParams,
|
|
464
486
|
writable: false,
|
|
465
487
|
enumerable: false,
|
|
466
488
|
configurable: true
|
|
467
489
|
});
|
|
468
490
|
|
|
469
|
-
assert.strictEqual(self.finalized, false);
|
|
470
|
-
isString.arrayOf().assert(self.buildParams);
|
|
471
|
-
|
|
472
491
|
if (self.buildable) {
|
|
473
492
|
// If this Def is already buildable, update self.buildParams and
|
|
474
493
|
// continue using the old builder function.
|
|
@@ -478,7 +497,7 @@ Dp.build = function(/* param1, param2, ... */) {
|
|
|
478
497
|
// Every buildable type will have its "type" field filled in
|
|
479
498
|
// automatically. This includes types that are not subtypes of Node,
|
|
480
499
|
// like SourceLocation, but that seems harmless (TODO?).
|
|
481
|
-
self.field("type",
|
|
500
|
+
self.field("type", String, function() { return self.typeName });
|
|
482
501
|
|
|
483
502
|
// Override Dp.buildable for this Def instance.
|
|
484
503
|
Object.defineProperty(self, "buildable", { value: true });
|
|
@@ -583,7 +602,12 @@ exports.getStatementBuilderName = getStatementBuilderName;
|
|
|
583
602
|
// support only one key and one value, but with .field(...) we can pass
|
|
584
603
|
// any number of arguments to specify the field.
|
|
585
604
|
Dp.field = function(name, type, defaultFn, hidden) {
|
|
586
|
-
|
|
605
|
+
if (this.finalized) {
|
|
606
|
+
console.error("Ignoring attempt to redefine field " +
|
|
607
|
+
JSON.stringify(name) + " of finalized type " +
|
|
608
|
+
JSON.stringify(this.typeName));
|
|
609
|
+
return this;
|
|
610
|
+
}
|
|
587
611
|
this.ownFields[name] = new Field(name, type, defaultFn, hidden);
|
|
588
612
|
return this; // For chaining.
|
|
589
613
|
};
|
|
@@ -650,44 +674,54 @@ exports.someField = function(object, callback, context) {
|
|
|
650
674
|
Object.defineProperty(Dp, "finalized", { value: false });
|
|
651
675
|
|
|
652
676
|
Dp.finalize = function() {
|
|
677
|
+
var self = this;
|
|
678
|
+
|
|
653
679
|
// It's not an error to finalize a type more than once, but only the
|
|
654
680
|
// first call to .finalize does anything.
|
|
655
|
-
if (!
|
|
656
|
-
var allFields =
|
|
657
|
-
var allSupertypes =
|
|
681
|
+
if (!self.finalized) {
|
|
682
|
+
var allFields = self.allFields;
|
|
683
|
+
var allSupertypes = self.allSupertypes;
|
|
658
684
|
|
|
659
|
-
|
|
685
|
+
self.baseNames.forEach(function(name) {
|
|
660
686
|
var def = defCache[name];
|
|
661
|
-
def
|
|
662
|
-
|
|
663
|
-
|
|
687
|
+
if (def instanceof Def) {
|
|
688
|
+
def.finalize();
|
|
689
|
+
extend(allFields, def.allFields);
|
|
690
|
+
extend(allSupertypes, def.allSupertypes);
|
|
691
|
+
} else {
|
|
692
|
+
var message = "unknown supertype name " +
|
|
693
|
+
JSON.stringify(name) +
|
|
694
|
+
" for subtype " +
|
|
695
|
+
JSON.stringify(self.typeName);
|
|
696
|
+
assert.ok(false, message);
|
|
697
|
+
}
|
|
664
698
|
});
|
|
665
699
|
|
|
666
700
|
// TODO Warn if fields are overridden with incompatible types.
|
|
667
|
-
extend(allFields,
|
|
668
|
-
allSupertypes[
|
|
701
|
+
extend(allFields, self.ownFields);
|
|
702
|
+
allSupertypes[self.typeName] = self;
|
|
669
703
|
|
|
670
|
-
|
|
704
|
+
self.fieldNames.length = 0;
|
|
671
705
|
for (var fieldName in allFields) {
|
|
672
706
|
if (hasOwn.call(allFields, fieldName) &&
|
|
673
707
|
!allFields[fieldName].hidden) {
|
|
674
|
-
|
|
708
|
+
self.fieldNames.push(fieldName);
|
|
675
709
|
}
|
|
676
710
|
}
|
|
677
711
|
|
|
678
712
|
// Types are exported only once they have been finalized.
|
|
679
|
-
Object.defineProperty(namedTypes,
|
|
713
|
+
Object.defineProperty(namedTypes, self.typeName, {
|
|
680
714
|
enumerable: true,
|
|
681
|
-
value:
|
|
715
|
+
value: self.type
|
|
682
716
|
});
|
|
683
717
|
|
|
684
|
-
Object.defineProperty(
|
|
718
|
+
Object.defineProperty(self, "finalized", { value: true });
|
|
685
719
|
|
|
686
720
|
// A linearization of the inheritance hierarchy.
|
|
687
|
-
populateSupertypeList(
|
|
721
|
+
populateSupertypeList(self.typeName, self.supertypeList);
|
|
688
722
|
|
|
689
|
-
if (
|
|
690
|
-
wrapExpressionBuilderWithStatement(
|
|
723
|
+
if (self.buildable && self.supertypeList.lastIndexOf("Expression") >= 0) {
|
|
724
|
+
wrapExpressionBuilderWithStatement(self.typeName);
|
|
691
725
|
}
|
|
692
726
|
}
|
|
693
727
|
};
|
package/main.js
CHANGED
package/package.json
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"transformation",
|
|
19
19
|
"syntax"
|
|
20
20
|
],
|
|
21
|
-
"version": "0.
|
|
21
|
+
"version": "0.8.2",
|
|
22
22
|
"homepage": "http://github.com/benjamn/ast-types",
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
@@ -27,16 +27,16 @@
|
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"main": "main.js",
|
|
29
29
|
"scripts": {
|
|
30
|
-
"test": "mocha --reporter spec test/run.js"
|
|
31
|
-
},
|
|
32
|
-
"dependencies": {
|
|
30
|
+
"test": "mocha --reporter spec --full-trace test/run.js"
|
|
33
31
|
},
|
|
32
|
+
"dependencies": {},
|
|
34
33
|
"devDependencies": {
|
|
34
|
+
"babel-core": "^5.6.15",
|
|
35
35
|
"esprima": "~1.2.2",
|
|
36
36
|
"esprima-fb": "~14001.1.0-dev-harmony-fb",
|
|
37
|
-
"mocha": "~
|
|
37
|
+
"mocha": "~2.2.5"
|
|
38
38
|
},
|
|
39
39
|
"engines": {
|
|
40
|
-
"node": ">= 0.
|
|
40
|
+
"node": ">= 0.8"
|
|
41
41
|
}
|
|
42
42
|
}
|