Skip to content

Commit

Permalink
Improve optional type matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
lapo-luchini committed Apr 20, 2024
1 parent 89a776d commit 6f4b911
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Defs {
// return r.types[name];
return Defs.moduleAndType(mod, name);
}
throw 'Type not found: ' + name;
throw new Error('Type not found: ' + name);
}

static match(value, def, stats = { total: 0, recognized: 0, defs: {} }) {
Expand All @@ -90,12 +90,20 @@ export class Defs {
type = def.content[0];
else {
let tn = subval.typeName().replaceAll('_', ' ');
do {
while (true) {
type = def.content[j++];
// type = translate(type, tn);
if (!type || typeof type != 'object') break;
if (type?.type?.type)
type = type.type;
} while (type && typeof type == 'object' && ('optional' in type || 'default' in type) && type.name != 'ANY' && type.name != tn);
if (type.type == 'defined') {
let t2 = translate(type, tn);
if (t2.type.name == tn) break; // exact match
if (t2.type.name == 'ANY') break; // good enough
}
if (type.name == tn) break; // exact match
if (type.name == 'ANY') break; // good enough
if (!('optional' in type || 'default' in type)) break;
}
if (type?.type == 'builtin' || type?.type == 'defined') {
let v = subval.content();
if (typeof v == 'string')
Expand Down

0 comments on commit 6f4b911

Please sign in to comment.