lambdajson-js 3.1.0 → 3.1.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/README.md +7 -0
- package/package.json +1 -1
- package/src/infer.js +17 -3
- package/src/primitives.js +3 -2
- package/src/types.js +2 -1
package/README.md
CHANGED
|
@@ -32,6 +32,13 @@ and the errors and warnings (impossible paths, possibly missing operands, mismat
|
|
|
32
32
|
comparisons, unbound variables) found from the type of the input, without running the program.
|
|
33
33
|
See the language documentation.
|
|
34
34
|
|
|
35
|
+
## Changes in 3.1.1
|
|
36
|
+
|
|
37
|
+
* `infer`: an impossible segment in some members of a union yields a missing value (`T | null`) instead of being dropped.
|
|
38
|
+
* `infer`: the names of `as` are bound to any value in the arguments of a primitive whose parameters aren't typed.
|
|
39
|
+
* `$get`, `$keys` and `$values` are typed to accept arrays as well as objects; `$get`'s `path` may be a number.
|
|
40
|
+
* `infer`: `$merge` with an operand of unknown type is open, and earlier properties become unknown.
|
|
41
|
+
|
|
35
42
|
## Changes in 3.1
|
|
36
43
|
|
|
37
44
|
* `infer(program, { input, vars, primitives })`: type inference with errors and warnings before a program runs.
|
package/package.json
CHANGED
package/src/infer.js
CHANGED
|
@@ -198,7 +198,8 @@ function createEngine(prims, report) {
|
|
|
198
198
|
lambdas[k] = [v, argPath];
|
|
199
199
|
continue;
|
|
200
200
|
}
|
|
201
|
-
|
|
201
|
+
// an untyped argument may be a lambda of `as`: bind its names to any value
|
|
202
|
+
types[k] = tree(takes[k] === undefined && value.as !== undefined ? bind(env, value.as) : env, v, argPath);
|
|
202
203
|
if (takes[k] !== undefined) check(toType(takes[k]), types[k], argPath, `'${k}'`);
|
|
203
204
|
}
|
|
204
205
|
for (const spec of prim.params) {
|
|
@@ -218,6 +219,11 @@ function createEngine(prims, report) {
|
|
|
218
219
|
return returnsOf(signature, types);
|
|
219
220
|
}
|
|
220
221
|
|
|
222
|
+
function bind(env, as) {
|
|
223
|
+
const names = isArray(as) ? as : [as];
|
|
224
|
+
return { ...env, vars: { ...env.vars, ...Object.fromEntries(names.map(name => [name, ANY])) } };
|
|
225
|
+
}
|
|
226
|
+
|
|
221
227
|
function lambda(env, [node, path], as, args, input = args[0] ?? ANY) {
|
|
222
228
|
const scope = as === undefined
|
|
223
229
|
? { ...env, input }
|
|
@@ -280,11 +286,17 @@ function createEngine(prims, report) {
|
|
|
280
286
|
|
|
281
287
|
const mergeObjects = types => {
|
|
282
288
|
const objects = types.map(nonNull);
|
|
283
|
-
if (!objects.every(t => t.type === 'object')) return OBJECT;
|
|
289
|
+
if (!objects.every(t => isAny(t) || t.type === 'object')) return OBJECT;
|
|
284
290
|
const properties = {};
|
|
285
291
|
const required = new Set();
|
|
286
292
|
let closed = true;
|
|
287
293
|
for (const t of objects) {
|
|
294
|
+
if (isAny(t)) {
|
|
295
|
+
// an unknown operand may override anything declared before it
|
|
296
|
+
Object.keys(properties).forEach(k => { properties[k] = ANY; });
|
|
297
|
+
closed = false;
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
288
300
|
for (const [k, p] of Object.entries(t.properties ?? {})) {
|
|
289
301
|
const previous = properties[k];
|
|
290
302
|
properties[k] = previous && nonNull(previous).type === 'object' && nonNull(p).type === 'object' ? mergeObjects([previous, p]) : p;
|
|
@@ -309,6 +321,8 @@ function createEngine(prims, report) {
|
|
|
309
321
|
return result;
|
|
310
322
|
};
|
|
311
323
|
|
|
324
|
+
const isArrayType = t => nonNull(t).type === 'array';
|
|
325
|
+
|
|
312
326
|
const RESULTS = {
|
|
313
327
|
$in: a => a,
|
|
314
328
|
$return: a => a,
|
|
@@ -327,7 +341,7 @@ function createEngine(prims, report) {
|
|
|
327
341
|
const keys = o.type === 'object' && o.additionalProperties === false ? Object.keys(o.properties ?? {}) : null;
|
|
328
342
|
return arrayOf(keys && keys.length > 0 ? { type: 'string', enum: keys } : STRING);
|
|
329
343
|
},
|
|
330
|
-
$values: a => arrayOf(valueOf(a)),
|
|
344
|
+
$values: a => arrayOf(isArrayType(a) ? itemOf(a) : valueOf(a)),
|
|
331
345
|
$merge: a => mergeObjects(operands(undefined, a, '').map(o => o.type)),
|
|
332
346
|
$coalesce: a => {
|
|
333
347
|
const list = nonNull(a);
|
package/src/primitives.js
CHANGED
|
@@ -200,6 +200,7 @@ export const typed = (primitive, signature, infer) => {
|
|
|
200
200
|
const sig = (takes, returns) => ({ takes, returns });
|
|
201
201
|
const over = (fn, collection, returns, extra = {}) => sig({ [fn]: 'function', [collection]: collection === 'of' ? 'object' : 'array', ...extra, as: 'names' }, returns);
|
|
202
202
|
const numbers = 'array<number>';
|
|
203
|
+
const container = { anyOf: [{ type: 'object' }, { type: 'array' }] };
|
|
203
204
|
|
|
204
205
|
const signatures = {
|
|
205
206
|
$literal: sig('any', 'any'), $: sig('any', 'function'), $in: sig('any', 'any'), $return: sig('any', 'any'),
|
|
@@ -223,9 +224,9 @@ const signatures = {
|
|
|
223
224
|
$zip: sig({ with: 'function', sequences: 'array<array>', as: 'names' }, 'array'),
|
|
224
225
|
$fold: sig({ with: 'function', init: 'any', collection: 'array', as: 'names' }, 'any'),
|
|
225
226
|
$head: sig('array', 'any'), $tail: sig('array', 'array'), $concat: sig('array<array>', 'array'),
|
|
226
|
-
$keys: sig(
|
|
227
|
+
$keys: sig(container, 'array<string>'), $values: sig(container, 'array'), $merge: sig('array<object>', 'object'),
|
|
227
228
|
$coalesce: sig('array', 'any'),
|
|
228
|
-
$get: sig({ object:
|
|
229
|
+
$get: sig({ object: container, path: { anyOf: [{ type: 'string' }, { type: 'number' }] } }, 'any'),
|
|
229
230
|
$includes: sig({ collection: 'array', value: 'any' }, 'boolean'),
|
|
230
231
|
$str: sig('any', 'string'),
|
|
231
232
|
$join: sig({ separator: 'string', strings: 'array' }, 'string'),
|
package/src/types.js
CHANGED
|
@@ -265,7 +265,8 @@ export function walkSegment(t, k) {
|
|
|
265
265
|
: walkMember(m, k));
|
|
266
266
|
const real = results.filter(r => !r.isNull);
|
|
267
267
|
if (real.length > 0 && real.every(r => r.error)) return { error: real[0].error };
|
|
268
|
-
|
|
268
|
+
// a member where the segment can't exist yields a missing value at run time
|
|
269
|
+
return { type: union(...results.filter(r => !r.error).map(r => r.type), ...(results.some(r => r.error) ? [NULL] : [])) };
|
|
269
270
|
}
|
|
270
271
|
|
|
271
272
|
/** Walks a path; calls `fail(message)` at the first impossible segment and continues with null. */
|