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.
- package/.travis.yml +2 -2
- package/def/babel-core.d.ts +2 -0
- package/def/babel-core.js +250 -290
- package/def/babel.d.ts +2 -0
- package/def/babel.js +12 -3
- package/def/core.d.ts +2 -0
- package/def/core.js +57 -126
- package/def/es-proposals.d.ts +2 -0
- package/def/es-proposals.js +34 -32
- package/def/es6.d.ts +2 -0
- package/def/es6.js +210 -253
- package/def/es7.d.ts +2 -0
- package/def/es7.js +35 -41
- package/def/esprima.d.ts +2 -0
- package/def/esprima.js +50 -71
- package/def/flow.d.ts +2 -0
- package/def/flow.js +289 -393
- package/def/jsx.d.ts +2 -0
- package/def/jsx.js +99 -125
- package/def/type-annotations.d.ts +7 -0
- package/def/type-annotations.js +42 -0
- package/def/typescript.d.ts +2 -0
- package/def/typescript.js +326 -444
- package/fork.d.ts +46 -0
- package/fork.js +40 -31
- package/gen/builders.d.ts +2539 -0
- package/gen/builders.js +2 -0
- package/gen/kinds.d.ts +259 -0
- package/gen/kinds.js +2 -0
- package/gen/namedTypes.d.ts +262 -0
- package/gen/namedTypes.js +2 -0
- package/gen/nodes.d.ts +1255 -0
- package/gen/nodes.js +2 -0
- package/gen/visitor.d.ts +263 -0
- package/gen/visitor.js +2 -0
- package/lib/equiv.d.ts +5 -0
- package/lib/equiv.js +19 -49
- package/lib/node-path.d.ts +22 -0
- package/lib/node-path.js +60 -125
- package/lib/path-visitor.d.ts +52 -0
- package/lib/path-visitor.js +117 -194
- package/lib/path.d.ts +25 -0
- package/lib/path.js +71 -107
- package/lib/scope.d.ts +26 -0
- package/lib/scope.js +100 -143
- package/lib/shared.d.ts +13 -0
- package/lib/shared.js +34 -31
- package/lib/types.d.ts +140 -0
- package/lib/types.js +584 -676
- package/main.d.ts +16 -0
- package/main.js +33 -18
- package/package.json +24 -7
- package/tsconfig.json +20 -0
- package/types.d.ts +6 -0
- package/types.js +2 -0
- package/def/e4x.js +0 -86
- package/def/mozilla.js +0 -42
package/lib/scope.js
CHANGED
|
@@ -1,35 +1,34 @@
|
|
|
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"));
|
|
1
7
|
var hasOwn = Object.prototype.hasOwnProperty;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var types = fork.use(require("./types"));
|
|
8
|
+
function scopePlugin(fork) {
|
|
9
|
+
var types = fork.use(types_1.default);
|
|
5
10
|
var Type = types.Type;
|
|
6
11
|
var namedTypes = types.namedTypes;
|
|
7
12
|
var Node = namedTypes.Node;
|
|
8
13
|
var Expression = namedTypes.Expression;
|
|
9
14
|
var isArray = types.builtInTypes.array;
|
|
10
15
|
var b = types.builders;
|
|
11
|
-
|
|
12
|
-
function Scope(path, parentScope) {
|
|
16
|
+
var Scope = function Scope(path, parentScope) {
|
|
13
17
|
if (!(this instanceof Scope)) {
|
|
14
18
|
throw new Error("Scope constructor cannot be invoked without 'new'");
|
|
15
19
|
}
|
|
16
|
-
if (!(path instanceof fork.use(require("./node-path")))) {
|
|
17
|
-
throw new Error("");
|
|
18
|
-
}
|
|
19
20
|
ScopeType.assert(path.value);
|
|
20
|
-
|
|
21
21
|
var depth;
|
|
22
|
-
|
|
23
22
|
if (parentScope) {
|
|
24
23
|
if (!(parentScope instanceof Scope)) {
|
|
25
24
|
throw new Error("");
|
|
26
25
|
}
|
|
27
26
|
depth = parentScope.depth + 1;
|
|
28
|
-
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
29
|
parentScope = null;
|
|
30
30
|
depth = 0;
|
|
31
31
|
}
|
|
32
|
-
|
|
33
32
|
Object.defineProperties(this, {
|
|
34
33
|
path: { value: path },
|
|
35
34
|
node: { value: path.value },
|
|
@@ -39,85 +38,62 @@ module.exports = function (fork) {
|
|
|
39
38
|
bindings: { value: {} },
|
|
40
39
|
types: { value: {} },
|
|
41
40
|
});
|
|
42
|
-
}
|
|
43
|
-
|
|
41
|
+
};
|
|
44
42
|
var scopeTypes = [
|
|
45
43
|
// Program nodes introduce global scopes.
|
|
46
44
|
namedTypes.Program,
|
|
47
|
-
|
|
48
45
|
// Function is the supertype of FunctionExpression,
|
|
49
46
|
// FunctionDeclaration, ArrowExpression, etc.
|
|
50
47
|
namedTypes.Function,
|
|
51
|
-
|
|
52
48
|
// In case you didn't know, the caught parameter shadows any variable
|
|
53
49
|
// of the same name in an outer scope.
|
|
54
50
|
namedTypes.CatchClause
|
|
55
51
|
];
|
|
56
|
-
|
|
57
52
|
var ScopeType = Type.or.apply(Type, scopeTypes);
|
|
58
|
-
|
|
59
|
-
Scope.isEstablishedBy = function(node) {
|
|
53
|
+
Scope.isEstablishedBy = function (node) {
|
|
60
54
|
return ScopeType.check(node);
|
|
61
55
|
};
|
|
62
|
-
|
|
63
56
|
var Sp = Scope.prototype;
|
|
64
|
-
|
|
65
|
-
// Will be overridden after an instance lazily calls scanScope.
|
|
57
|
+
// Will be overridden after an instance lazily calls scanScope.
|
|
66
58
|
Sp.didScan = false;
|
|
67
|
-
|
|
68
|
-
Sp.declares = function(name) {
|
|
59
|
+
Sp.declares = function (name) {
|
|
69
60
|
this.scan();
|
|
70
61
|
return hasOwn.call(this.bindings, name);
|
|
71
62
|
};
|
|
72
|
-
|
|
73
|
-
Sp.declaresType = function(name) {
|
|
63
|
+
Sp.declaresType = function (name) {
|
|
74
64
|
this.scan();
|
|
75
65
|
return hasOwn.call(this.types, name);
|
|
76
66
|
};
|
|
77
|
-
|
|
78
|
-
Sp.declareTemporary = function(prefix) {
|
|
67
|
+
Sp.declareTemporary = function (prefix) {
|
|
79
68
|
if (prefix) {
|
|
80
69
|
if (!/^[a-z$_]/i.test(prefix)) {
|
|
81
70
|
throw new Error("");
|
|
82
71
|
}
|
|
83
|
-
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
84
74
|
prefix = "t$";
|
|
85
75
|
}
|
|
86
|
-
|
|
87
76
|
// Include this.depth in the name to make sure the name does not
|
|
88
77
|
// collide with any variables in nested/enclosing scopes.
|
|
89
78
|
prefix += this.depth.toString(36) + "$";
|
|
90
|
-
|
|
91
79
|
this.scan();
|
|
92
|
-
|
|
93
80
|
var index = 0;
|
|
94
81
|
while (this.declares(prefix + index)) {
|
|
95
82
|
++index;
|
|
96
83
|
}
|
|
97
|
-
|
|
98
84
|
var name = prefix + index;
|
|
99
85
|
return this.bindings[name] = types.builders.identifier(name);
|
|
100
86
|
};
|
|
101
|
-
|
|
102
|
-
Sp.injectTemporary = function(identifier, init) {
|
|
87
|
+
Sp.injectTemporary = function (identifier, init) {
|
|
103
88
|
identifier || (identifier = this.declareTemporary());
|
|
104
|
-
|
|
105
89
|
var bodyPath = this.path.get("body");
|
|
106
90
|
if (namedTypes.BlockStatement.check(bodyPath.value)) {
|
|
107
91
|
bodyPath = bodyPath.get("body");
|
|
108
92
|
}
|
|
109
|
-
|
|
110
|
-
bodyPath.unshift(
|
|
111
|
-
b.variableDeclaration(
|
|
112
|
-
"var",
|
|
113
|
-
[b.variableDeclarator(identifier, init || null)]
|
|
114
|
-
)
|
|
115
|
-
);
|
|
116
|
-
|
|
93
|
+
bodyPath.unshift(b.variableDeclaration("var", [b.variableDeclarator(identifier, init || null)]));
|
|
117
94
|
return identifier;
|
|
118
95
|
};
|
|
119
|
-
|
|
120
|
-
Sp.scan = function(force) {
|
|
96
|
+
Sp.scan = function (force) {
|
|
121
97
|
if (force || !this.didScan) {
|
|
122
98
|
for (var name in this.bindings) {
|
|
123
99
|
// Empty out this.bindings, just in cases.
|
|
@@ -127,78 +103,68 @@ module.exports = function (fork) {
|
|
|
127
103
|
this.didScan = true;
|
|
128
104
|
}
|
|
129
105
|
};
|
|
130
|
-
|
|
131
106
|
Sp.getBindings = function () {
|
|
132
107
|
this.scan();
|
|
133
108
|
return this.bindings;
|
|
134
109
|
};
|
|
135
|
-
|
|
136
110
|
Sp.getTypes = function () {
|
|
137
111
|
this.scan();
|
|
138
112
|
return this.types;
|
|
139
113
|
};
|
|
140
|
-
|
|
141
114
|
function scanScope(path, bindings, scopeTypes) {
|
|
142
115
|
var node = path.value;
|
|
143
116
|
ScopeType.assert(node);
|
|
144
|
-
|
|
145
117
|
if (namedTypes.CatchClause.check(node)) {
|
|
146
118
|
// A catch clause establishes a new scope but the only variable
|
|
147
119
|
// bound in that scope is the catch parameter. Any other
|
|
148
120
|
// declarations create bindings in the outer scope.
|
|
149
121
|
addPattern(path.get("param"), bindings);
|
|
150
|
-
|
|
151
|
-
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
152
124
|
recursiveScanScope(path, bindings, scopeTypes);
|
|
153
125
|
}
|
|
154
126
|
}
|
|
155
|
-
|
|
156
127
|
function recursiveScanScope(path, bindings, scopeTypes) {
|
|
157
128
|
var node = path.value;
|
|
158
|
-
|
|
159
129
|
if (path.parent &&
|
|
160
|
-
|
|
161
|
-
|
|
130
|
+
namedTypes.FunctionExpression.check(path.parent.node) &&
|
|
131
|
+
path.parent.node.id) {
|
|
162
132
|
addPattern(path.parent.get("id"), bindings);
|
|
163
133
|
}
|
|
164
|
-
|
|
165
134
|
if (!node) {
|
|
166
135
|
// None of the remaining cases matter if node is falsy.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
path.each(function(childPath) {
|
|
136
|
+
}
|
|
137
|
+
else if (isArray.check(node)) {
|
|
138
|
+
path.each(function (childPath) {
|
|
170
139
|
recursiveScanChild(childPath, bindings, scopeTypes);
|
|
171
140
|
});
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
path.get("params").each(function(paramPath) {
|
|
141
|
+
}
|
|
142
|
+
else if (namedTypes.Function.check(node)) {
|
|
143
|
+
path.get("params").each(function (paramPath) {
|
|
175
144
|
addPattern(paramPath, bindings);
|
|
176
145
|
});
|
|
177
|
-
|
|
178
146
|
recursiveScanChild(path.get("body"), bindings, scopeTypes);
|
|
179
|
-
|
|
180
|
-
|
|
147
|
+
}
|
|
148
|
+
else if (namedTypes.TypeAlias && namedTypes.TypeAlias.check(node)) {
|
|
181
149
|
addTypePattern(path.get("id"), scopeTypes);
|
|
182
|
-
|
|
183
|
-
|
|
150
|
+
}
|
|
151
|
+
else if (namedTypes.VariableDeclarator.check(node)) {
|
|
184
152
|
addPattern(path.get("id"), bindings);
|
|
185
153
|
recursiveScanChild(path.get("init"), bindings, scopeTypes);
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
154
|
+
}
|
|
155
|
+
else if (node.type === "ImportSpecifier" ||
|
|
156
|
+
node.type === "ImportNamespaceSpecifier" ||
|
|
157
|
+
node.type === "ImportDefaultSpecifier") {
|
|
190
158
|
addPattern(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
node.name ? "name" : "id"),
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
} else if (Node.check(node) && !Expression.check(node)) {
|
|
201
|
-
types.eachField(node, function(name, child) {
|
|
159
|
+
// Esprima used to use the .name field to refer to the local
|
|
160
|
+
// binding identifier for ImportSpecifier nodes, but .id for
|
|
161
|
+
// ImportNamespaceSpecifier and ImportDefaultSpecifier nodes.
|
|
162
|
+
// ESTree/Acorn/ESpree use .local for all three node types.
|
|
163
|
+
path.get(node.local ? "local" :
|
|
164
|
+
node.name ? "name" : "id"), bindings);
|
|
165
|
+
}
|
|
166
|
+
else if (Node.check(node) && !Expression.check(node)) {
|
|
167
|
+
types.eachField(node, function (name, child) {
|
|
202
168
|
var childPath = path.get(name);
|
|
203
169
|
if (!pathHasValue(childPath, child)) {
|
|
204
170
|
throw new Error("");
|
|
@@ -207,48 +173,41 @@ module.exports = function (fork) {
|
|
|
207
173
|
});
|
|
208
174
|
}
|
|
209
175
|
}
|
|
210
|
-
|
|
211
176
|
function pathHasValue(path, value) {
|
|
212
177
|
if (path.value === value) {
|
|
213
178
|
return true;
|
|
214
179
|
}
|
|
215
|
-
|
|
216
180
|
// Empty arrays are probably produced by defaults.emptyArray, in which
|
|
217
181
|
// case is makes sense to regard them as equivalent, if not ===.
|
|
218
182
|
if (Array.isArray(path.value) &&
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
183
|
+
path.value.length === 0 &&
|
|
184
|
+
Array.isArray(value) &&
|
|
185
|
+
value.length === 0) {
|
|
222
186
|
return true;
|
|
223
187
|
}
|
|
224
|
-
|
|
225
188
|
return false;
|
|
226
189
|
}
|
|
227
|
-
|
|
228
190
|
function recursiveScanChild(path, bindings, scopeTypes) {
|
|
229
191
|
var node = path.value;
|
|
230
|
-
|
|
231
192
|
if (!node || Expression.check(node)) {
|
|
232
193
|
// Ignore falsy values and Expressions.
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
194
|
+
}
|
|
195
|
+
else if (namedTypes.FunctionDeclaration.check(node) &&
|
|
196
|
+
node.id !== null) {
|
|
236
197
|
addPattern(path.get("id"), bindings);
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
198
|
+
}
|
|
199
|
+
else if (namedTypes.ClassDeclaration &&
|
|
200
|
+
namedTypes.ClassDeclaration.check(node)) {
|
|
240
201
|
addPattern(path.get("id"), bindings);
|
|
241
|
-
|
|
242
|
-
|
|
202
|
+
}
|
|
203
|
+
else if (ScopeType.check(node)) {
|
|
243
204
|
if (namedTypes.CatchClause.check(node)) {
|
|
244
205
|
var catchParamName = node.param.name;
|
|
245
206
|
var hadBinding = hasOwn.call(bindings, catchParamName);
|
|
246
|
-
|
|
247
207
|
// Any declarations that occur inside the catch body that do
|
|
248
208
|
// not have the same name as the catch parameter should count
|
|
249
209
|
// as bindings in the outer scope.
|
|
250
210
|
recursiveScanScope(path.get("body"), bindings, scopeTypes);
|
|
251
|
-
|
|
252
211
|
// If a new binding matching the catch parameter name was
|
|
253
212
|
// created while scanning the catch body, ignore it because it
|
|
254
213
|
// actually refers to the catch parameter and not the outer
|
|
@@ -257,99 +216,97 @@ module.exports = function (fork) {
|
|
|
257
216
|
delete bindings[catchParamName];
|
|
258
217
|
}
|
|
259
218
|
}
|
|
260
|
-
|
|
261
|
-
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
262
221
|
recursiveScanScope(path, bindings, scopeTypes);
|
|
263
222
|
}
|
|
264
223
|
}
|
|
265
|
-
|
|
266
224
|
function addPattern(patternPath, bindings) {
|
|
267
225
|
var pattern = patternPath.value;
|
|
268
226
|
namedTypes.Pattern.assert(pattern);
|
|
269
|
-
|
|
270
227
|
if (namedTypes.Identifier.check(pattern)) {
|
|
271
228
|
if (hasOwn.call(bindings, pattern.name)) {
|
|
272
229
|
bindings[pattern.name].push(patternPath);
|
|
273
|
-
}
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
274
232
|
bindings[pattern.name] = [patternPath];
|
|
275
233
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
patternPath.get('properties').each(function(propertyPath) {
|
|
234
|
+
}
|
|
235
|
+
else if (namedTypes.AssignmentPattern &&
|
|
236
|
+
namedTypes.AssignmentPattern.check(pattern)) {
|
|
237
|
+
addPattern(patternPath.get('left'), bindings);
|
|
238
|
+
}
|
|
239
|
+
else if (namedTypes.ObjectPattern &&
|
|
240
|
+
namedTypes.ObjectPattern.check(pattern)) {
|
|
241
|
+
patternPath.get('properties').each(function (propertyPath) {
|
|
284
242
|
var property = propertyPath.value;
|
|
285
243
|
if (namedTypes.Pattern.check(property)) {
|
|
286
244
|
addPattern(propertyPath, bindings);
|
|
287
|
-
}
|
|
245
|
+
}
|
|
246
|
+
else if (namedTypes.Property.check(property)) {
|
|
288
247
|
addPattern(propertyPath.get('value'), bindings);
|
|
289
|
-
}
|
|
290
|
-
|
|
248
|
+
}
|
|
249
|
+
else if (namedTypes.SpreadProperty &&
|
|
250
|
+
namedTypes.SpreadProperty.check(property)) {
|
|
291
251
|
addPattern(propertyPath.get('argument'), bindings);
|
|
292
252
|
}
|
|
293
253
|
});
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
patternPath.get('elements').each(function(elementPath) {
|
|
254
|
+
}
|
|
255
|
+
else if (namedTypes.ArrayPattern &&
|
|
256
|
+
namedTypes.ArrayPattern.check(pattern)) {
|
|
257
|
+
patternPath.get('elements').each(function (elementPath) {
|
|
298
258
|
var element = elementPath.value;
|
|
299
259
|
if (namedTypes.Pattern.check(element)) {
|
|
300
260
|
addPattern(elementPath, bindings);
|
|
301
|
-
}
|
|
302
|
-
|
|
261
|
+
}
|
|
262
|
+
else if (namedTypes.SpreadElement &&
|
|
263
|
+
namedTypes.SpreadElement.check(element)) {
|
|
303
264
|
addPattern(elementPath.get("argument"), bindings);
|
|
304
265
|
}
|
|
305
266
|
});
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
267
|
+
}
|
|
268
|
+
else if (namedTypes.PropertyPattern &&
|
|
269
|
+
namedTypes.PropertyPattern.check(pattern)) {
|
|
309
270
|
addPattern(patternPath.get('pattern'), bindings);
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
271
|
+
}
|
|
272
|
+
else if ((namedTypes.SpreadElementPattern &&
|
|
273
|
+
namedTypes.SpreadElementPattern.check(pattern)) ||
|
|
274
|
+
(namedTypes.SpreadPropertyPattern &&
|
|
275
|
+
namedTypes.SpreadPropertyPattern.check(pattern))) {
|
|
315
276
|
addPattern(patternPath.get('argument'), bindings);
|
|
316
277
|
}
|
|
317
278
|
}
|
|
318
|
-
|
|
319
279
|
function addTypePattern(patternPath, types) {
|
|
320
280
|
var pattern = patternPath.value;
|
|
321
281
|
namedTypes.Pattern.assert(pattern);
|
|
322
|
-
|
|
323
282
|
if (namedTypes.Identifier.check(pattern)) {
|
|
324
283
|
if (hasOwn.call(types, pattern.name)) {
|
|
325
284
|
types[pattern.name].push(patternPath);
|
|
326
|
-
}
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
327
287
|
types[pattern.name] = [patternPath];
|
|
328
288
|
}
|
|
329
|
-
|
|
330
289
|
}
|
|
331
290
|
}
|
|
332
|
-
|
|
333
|
-
Sp.lookup = function(name) {
|
|
291
|
+
Sp.lookup = function (name) {
|
|
334
292
|
for (var scope = this; scope; scope = scope.parent)
|
|
335
293
|
if (scope.declares(name))
|
|
336
294
|
break;
|
|
337
295
|
return scope;
|
|
338
296
|
};
|
|
339
|
-
|
|
340
|
-
Sp.lookupType = function(name) {
|
|
297
|
+
Sp.lookupType = function (name) {
|
|
341
298
|
for (var scope = this; scope; scope = scope.parent)
|
|
342
299
|
if (scope.declaresType(name))
|
|
343
300
|
break;
|
|
344
301
|
return scope;
|
|
345
302
|
};
|
|
346
|
-
|
|
347
|
-
Sp.getGlobalScope = function() {
|
|
303
|
+
Sp.getGlobalScope = function () {
|
|
348
304
|
var scope = this;
|
|
349
305
|
while (!scope.isGlobal)
|
|
350
306
|
scope = scope.parent;
|
|
351
307
|
return scope;
|
|
352
308
|
};
|
|
353
|
-
|
|
354
309
|
return Scope;
|
|
355
|
-
}
|
|
310
|
+
}
|
|
311
|
+
exports.default = scopePlugin;
|
|
312
|
+
module.exports = exports["default"];
|
package/lib/shared.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Fork } from "../types";
|
|
2
|
+
export default function (fork: Fork): {
|
|
3
|
+
geq: (than: any) => import("./types").Type<{}>;
|
|
4
|
+
defaults: {
|
|
5
|
+
"null": () => null;
|
|
6
|
+
"emptyArray": () => never[];
|
|
7
|
+
"false": () => boolean;
|
|
8
|
+
"true": () => boolean;
|
|
9
|
+
"undefined": () => void;
|
|
10
|
+
"use strict": () => string;
|
|
11
|
+
};
|
|
12
|
+
isPrimitive: import("./types").Type<{}>;
|
|
13
|
+
};
|
package/lib/shared.js
CHANGED
|
@@ -1,46 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
+
function default_1(fork) {
|
|
8
|
+
var types = fork.use(types_1.default);
|
|
4
9
|
var Type = types.Type;
|
|
5
10
|
var builtin = types.builtInTypes;
|
|
6
11
|
var isNumber = builtin.number;
|
|
7
|
-
|
|
8
12
|
// An example of constructing a new type with arbitrary constraints from
|
|
9
13
|
// an existing type.
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
|
|
14
|
+
function geq(than) {
|
|
15
|
+
return Type.from(function (value) { return isNumber.check(value) && value >= than; }, isNumber + " >= " + than);
|
|
16
|
+
}
|
|
17
|
+
;
|
|
16
18
|
// Default value-returning functions that may optionally be passed as a
|
|
17
19
|
// third argument to Def.prototype.field.
|
|
18
|
-
|
|
20
|
+
var defaults = {
|
|
19
21
|
// Functions were used because (among other reasons) that's the most
|
|
20
22
|
// elegant way to allow for the emptyArray one always to give a new
|
|
21
23
|
// array instance.
|
|
22
|
-
"null": function () { return null },
|
|
23
|
-
"emptyArray": function () { return [] },
|
|
24
|
-
"false": function () { return false },
|
|
25
|
-
"true": function () { return true },
|
|
26
|
-
"undefined": function () {}
|
|
24
|
+
"null": function () { return null; },
|
|
25
|
+
"emptyArray": function () { return []; },
|
|
26
|
+
"false": function () { return false; },
|
|
27
|
+
"true": function () { return true; },
|
|
28
|
+
"undefined": function () { },
|
|
29
|
+
"use strict": function () { return "use strict"; }
|
|
27
30
|
};
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
builtin.string,
|
|
31
|
-
builtin.number,
|
|
32
|
-
builtin.boolean,
|
|
33
|
-
builtin.null,
|
|
34
|
-
builtin.undefined
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
exports.isPrimitive = new Type(function (value) {
|
|
31
|
+
var naiveIsPrimitive = Type.or(builtin.string, builtin.number, builtin.boolean, builtin.null, builtin.undefined);
|
|
32
|
+
var isPrimitive = Type.from(function (value) {
|
|
38
33
|
if (value === null)
|
|
39
34
|
return true;
|
|
40
35
|
var type = typeof value;
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
if (type === "object" ||
|
|
37
|
+
type === "function") {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
43
41
|
}, naiveIsPrimitive.toString());
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
return {
|
|
43
|
+
geq: geq,
|
|
44
|
+
defaults: defaults,
|
|
45
|
+
isPrimitive: isPrimitive,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
exports.default = default_1;
|
|
49
|
+
module.exports = exports["default"];
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Fork } from "../types";
|
|
2
|
+
declare type Deep = boolean | ((type: Type<any>, value: any) => void);
|
|
3
|
+
export declare type Type<T> = ArrayType<T> | IdentityType<T> | ObjectType<T> | OrType<T> | PredicateType<T>;
|
|
4
|
+
export interface AnyType {
|
|
5
|
+
toString(): string;
|
|
6
|
+
check(value: any, deep?: Deep): boolean;
|
|
7
|
+
assert(value: any, deep?: Deep): boolean;
|
|
8
|
+
arrayOf(): AnyType;
|
|
9
|
+
}
|
|
10
|
+
declare abstract class BaseType<T> {
|
|
11
|
+
abstract toString(): string;
|
|
12
|
+
abstract check(value: any, deep?: Deep): value is T;
|
|
13
|
+
assert(value: any, deep?: Deep): value is T;
|
|
14
|
+
arrayOf(): Type<T[]>;
|
|
15
|
+
}
|
|
16
|
+
declare class ArrayType<T> extends BaseType<T> {
|
|
17
|
+
readonly elemType: Type<T extends (infer E)[] ? E : never>;
|
|
18
|
+
readonly kind: "ArrayType";
|
|
19
|
+
constructor(elemType: Type<T extends (infer E)[] ? E : never>);
|
|
20
|
+
toString(): string;
|
|
21
|
+
check(value: any, deep?: Deep): value is T;
|
|
22
|
+
}
|
|
23
|
+
declare class IdentityType<T> extends BaseType<T> {
|
|
24
|
+
readonly value: T;
|
|
25
|
+
readonly kind: "IdentityType";
|
|
26
|
+
constructor(value: T);
|
|
27
|
+
toString(): string;
|
|
28
|
+
check(value: any, deep?: Deep): value is T;
|
|
29
|
+
}
|
|
30
|
+
declare class ObjectType<T> extends BaseType<T> {
|
|
31
|
+
readonly fields: Field<any>[];
|
|
32
|
+
readonly kind: "ObjectType";
|
|
33
|
+
constructor(fields: Field<any>[]);
|
|
34
|
+
toString(): string;
|
|
35
|
+
check(value: any, deep?: Deep): value is T;
|
|
36
|
+
}
|
|
37
|
+
declare class OrType<T> extends BaseType<T> {
|
|
38
|
+
readonly types: Type<any>[];
|
|
39
|
+
readonly kind: "OrType";
|
|
40
|
+
constructor(types: Type<any>[]);
|
|
41
|
+
toString(): string;
|
|
42
|
+
check(value: any, deep?: Deep): value is T;
|
|
43
|
+
}
|
|
44
|
+
declare class PredicateType<T> extends BaseType<T> {
|
|
45
|
+
readonly name: string;
|
|
46
|
+
readonly predicate: (value: any, deep?: Deep) => boolean;
|
|
47
|
+
readonly kind: "PredicateType";
|
|
48
|
+
constructor(name: string, predicate: (value: any, deep?: Deep) => boolean);
|
|
49
|
+
toString(): string;
|
|
50
|
+
check(value: any, deep?: Deep): value is T;
|
|
51
|
+
}
|
|
52
|
+
export declare abstract class Def<T = any> {
|
|
53
|
+
readonly type: Type<T>;
|
|
54
|
+
readonly typeName: string;
|
|
55
|
+
baseNames: string[];
|
|
56
|
+
ownFields: {
|
|
57
|
+
[name: string]: Field<any>;
|
|
58
|
+
};
|
|
59
|
+
allSupertypes: {
|
|
60
|
+
[name: string]: Def<any>;
|
|
61
|
+
};
|
|
62
|
+
supertypeList: string[];
|
|
63
|
+
allFields: {
|
|
64
|
+
[name: string]: Field<any>;
|
|
65
|
+
};
|
|
66
|
+
fieldNames: string[];
|
|
67
|
+
finalized: boolean;
|
|
68
|
+
buildable: boolean;
|
|
69
|
+
buildParams: string[];
|
|
70
|
+
constructor(type: Type<T>, typeName: string);
|
|
71
|
+
isSupertypeOf(that: Def<any>): boolean;
|
|
72
|
+
checkAllFields(value: any, deep?: any): boolean;
|
|
73
|
+
abstract check(value: any, deep?: any): boolean;
|
|
74
|
+
bases(...supertypeNames: string[]): this;
|
|
75
|
+
abstract build(...buildParams: string[]): this;
|
|
76
|
+
abstract field(name: string, type: any, defaultFn?: Function, hidden?: boolean): this;
|
|
77
|
+
abstract finalize(): void;
|
|
78
|
+
}
|
|
79
|
+
declare class Field<T> {
|
|
80
|
+
readonly name: string;
|
|
81
|
+
readonly type: Type<T>;
|
|
82
|
+
readonly defaultFn?: Function | undefined;
|
|
83
|
+
readonly hidden: boolean;
|
|
84
|
+
constructor(name: string, type: Type<T>, defaultFn?: Function | undefined, hidden?: boolean);
|
|
85
|
+
toString(): string;
|
|
86
|
+
getValue(obj: {
|
|
87
|
+
[key: string]: any;
|
|
88
|
+
}): any;
|
|
89
|
+
}
|
|
90
|
+
declare type FieldType<T> = Field<T>;
|
|
91
|
+
export { FieldType as Field };
|
|
92
|
+
export interface ASTNode {
|
|
93
|
+
type: string;
|
|
94
|
+
}
|
|
95
|
+
export interface Builder {
|
|
96
|
+
(...args: any[]): ASTNode;
|
|
97
|
+
from(obj: {
|
|
98
|
+
[param: string]: any;
|
|
99
|
+
}): ASTNode;
|
|
100
|
+
}
|
|
101
|
+
export default function typesPlugin(_fork: Fork): {
|
|
102
|
+
Type: {
|
|
103
|
+
or(...types: any[]): Type<any>;
|
|
104
|
+
from<T>(value: any, name?: string | undefined): Type<T>;
|
|
105
|
+
def(typeName: string): Def<any>;
|
|
106
|
+
hasDef(typeName: string): boolean;
|
|
107
|
+
};
|
|
108
|
+
builtInTypes: {
|
|
109
|
+
string: Type<string>;
|
|
110
|
+
function: Type<Function>;
|
|
111
|
+
array: Type<any[]>;
|
|
112
|
+
object: Type<{
|
|
113
|
+
[key: string]: any;
|
|
114
|
+
}>;
|
|
115
|
+
RegExp: Type<RegExp>;
|
|
116
|
+
Date: Type<Date>;
|
|
117
|
+
number: Type<number>;
|
|
118
|
+
boolean: Type<boolean>;
|
|
119
|
+
null: Type<null>;
|
|
120
|
+
undefined: Type<undefined>;
|
|
121
|
+
};
|
|
122
|
+
getSupertypeNames: (typeName: string) => string[];
|
|
123
|
+
computeSupertypeLookupTable: (candidates: any) => {
|
|
124
|
+
[typeName: string]: any;
|
|
125
|
+
};
|
|
126
|
+
builders: {
|
|
127
|
+
[name: string]: Builder;
|
|
128
|
+
};
|
|
129
|
+
defineMethod: (name: any, func?: Function | undefined) => Function;
|
|
130
|
+
getBuilderName: (typeName: any) => any;
|
|
131
|
+
getStatementBuilderName: (typeName: any) => any;
|
|
132
|
+
namedTypes: {
|
|
133
|
+
[name: string]: AnyType;
|
|
134
|
+
};
|
|
135
|
+
getFieldNames: (object: any) => string[];
|
|
136
|
+
getFieldValue: (object: any, fieldName: any) => any;
|
|
137
|
+
eachField: (object: any, callback: (name: any, value: any) => any, context?: any) => void;
|
|
138
|
+
someField: (object: any, callback: (name: any, value: any) => any, context?: any) => boolean;
|
|
139
|
+
finalize: () => void;
|
|
140
|
+
};
|