lambdajson-js 3.1.1 → 3.2.0
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 +11 -0
- package/package.json +1 -1
- package/src/infer.js +42 -3
- package/src/types.js +2 -2
package/README.md
CHANGED
|
@@ -32,6 +32,17 @@ 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.2.0
|
|
36
|
+
|
|
37
|
+
* `infer`: the argument of a typed function is checked property by property, recursively. A property whose type is definitely disjoint from the declared one (wrong kind, literal outside `enum`/`const`) is a `type-mismatch` naming its path (`the argument.entityType is 5, expected string`). `any`, unknown and overlapping properties are not reported; optional properties of the argument type are skipped.
|
|
38
|
+
* `infer`: new problem codes `missing-property` (a required key is absent) and `unknown-property` (a key where `additionalProperties` is `false`), reported only when the argument's type is exact (`additionalProperties: false`, as for object literals).
|
|
39
|
+
* `infer`: items of an array literal are checked against the declared `items`/`prefixItems`.
|
|
40
|
+
* Programs that passed before may now report these errors; each newly reported problem is a mismatch between the argument and the declared signature.
|
|
41
|
+
|
|
42
|
+
## Changes in 3.1.2
|
|
43
|
+
|
|
44
|
+
* `infer`: after an impossible path the walk continues with any value, so the problem isn't reported a second time where the value is used.
|
|
45
|
+
|
|
35
46
|
## Changes in 3.1.1
|
|
36
47
|
|
|
37
48
|
* `infer`: an impossible segment in some members of a union yields a missing value (`T | null`) instead of being dropped.
|
package/package.json
CHANGED
package/src/infer.js
CHANGED
|
@@ -169,10 +169,49 @@ function createEngine(prims, report) {
|
|
|
169
169
|
return [{ type: itemOf(array), path }];
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
function check(expected, actual, path, what) {
|
|
172
|
+
function check(expected, actual, path, what, value) {
|
|
173
173
|
if (disjoint(actual, expected)) {
|
|
174
174
|
error('type-mismatch', path, `${what} is ${describe(actual)}, expected ${describe(expected)}`);
|
|
175
|
+
return;
|
|
175
176
|
}
|
|
177
|
+
const e = members(expected);
|
|
178
|
+
const a = members(nonNull(actual));
|
|
179
|
+
if (e.length !== 1 || a.length !== 1 || isAny(e[0]) || isAny(a[0])) return;
|
|
180
|
+
if (e[0].type === 'object' && a[0].type === 'object') checkObject(e[0], a[0], path, what, value);
|
|
181
|
+
else if (e[0].type === 'array' && a[0].type === 'array') checkArray(e[0], a[0], path, what, value);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
185
|
+
|
|
186
|
+
/** Compares the properties both types know; keys are compared only when the argument's type is exact. */
|
|
187
|
+
function checkObject(expected, actual, path, what, value) {
|
|
188
|
+
const expectedProps = expected.properties ?? {};
|
|
189
|
+
const actualProps = actual.properties ?? {};
|
|
190
|
+
const exact = actual.additionalProperties === false;
|
|
191
|
+
const literal = isPlainObject(value);
|
|
192
|
+
const at = k => literal && has(value, k) ? pointer(path, k) : path;
|
|
193
|
+
for (const k of expected.required ?? []) {
|
|
194
|
+
if (exact && !has(actualProps, k)) error('missing-property', path, `${what} has no '${k}', which is required`);
|
|
195
|
+
}
|
|
196
|
+
for (const [k, type] of Object.entries(actualProps)) {
|
|
197
|
+
// an optional property may be missing, which is not a mismatch
|
|
198
|
+
if (!(actual.required ?? []).includes(k)) continue;
|
|
199
|
+
const target = has(expectedProps, k) ? expectedProps[k] : expected.additionalProperties;
|
|
200
|
+
if (target === false) {
|
|
201
|
+
if (exact) error('unknown-property', at(k), `${what} has '${k}', which is not a property of the expected object (declared: ${Object.keys(expectedProps).join(', ') || 'none'})`);
|
|
202
|
+
} else if (target !== undefined) {
|
|
203
|
+
check(target, type, at(k), `${what}.${k}`, literal ? value[k] : undefined);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Compares the items of an array literal. */
|
|
209
|
+
function checkArray(expected, actual, path, what, value) {
|
|
210
|
+
if (!isArray(value) || actual.prefixItems === undefined) return;
|
|
211
|
+
actual.prefixItems.forEach((type, i) => {
|
|
212
|
+
const target = expected.prefixItems?.[i] ?? expected.items;
|
|
213
|
+
if (target && target !== false) check(target, type, pointer(path, i), `${what}[${i}]`, value[i]);
|
|
214
|
+
});
|
|
176
215
|
}
|
|
177
216
|
|
|
178
217
|
function checkTakes(expected, arg, value, path) {
|
|
@@ -181,7 +220,7 @@ function createEngine(prims, report) {
|
|
|
181
220
|
if (disjoint(arg, ARRAY)) return;
|
|
182
221
|
for (const operand of operands(value, arg, path)) check(expected.items, operand.type, operand.path, 'an item');
|
|
183
222
|
} else {
|
|
184
|
-
check(expected, arg, path, 'the argument');
|
|
223
|
+
check(expected, arg, path, 'the argument', value);
|
|
185
224
|
}
|
|
186
225
|
}
|
|
187
226
|
|
|
@@ -200,7 +239,7 @@ function createEngine(prims, report) {
|
|
|
200
239
|
}
|
|
201
240
|
// an untyped argument may be a lambda of `as`: bind its names to any value
|
|
202
241
|
types[k] = tree(takes[k] === undefined && value.as !== undefined ? bind(env, value.as) : env, v, argPath);
|
|
203
|
-
if (takes[k] !== undefined) check(toType(takes[k]), types[k], argPath, `'${k}'
|
|
242
|
+
if (takes[k] !== undefined) check(toType(takes[k]), types[k], argPath, `'${k}'`, v);
|
|
204
243
|
}
|
|
205
244
|
for (const spec of prim.params) {
|
|
206
245
|
const name = spec.replace(/(=#|\?)$/, '');
|
package/src/types.js
CHANGED
|
@@ -269,14 +269,14 @@ export function walkSegment(t, k) {
|
|
|
269
269
|
return { type: union(...results.filter(r => !r.error).map(r => r.type), ...(results.some(r => r.error) ? [NULL] : [])) };
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
-
/** Walks a path; calls `fail(message)` at the first impossible segment and continues with
|
|
272
|
+
/** Walks a path; calls `fail(message)` at the first impossible segment and continues with any value, so the problem isn't reported twice. */
|
|
273
273
|
export function walkPath(t, segments, fail) {
|
|
274
274
|
let current = t;
|
|
275
275
|
for (const segment of segments) {
|
|
276
276
|
const step = walkSegment(current, segment);
|
|
277
277
|
if (step.error) {
|
|
278
278
|
fail(step.error);
|
|
279
|
-
return
|
|
279
|
+
return ANY;
|
|
280
280
|
}
|
|
281
281
|
current = step.type;
|
|
282
282
|
}
|