Generate as*() alongside is*()
This commit is contained in:
parent
a0d51fab4c
commit
ba2c7e9978
|
@ -17,7 +17,7 @@ export type CompileEnvEntry = {
|
|||
export function compile(env: Array<CompileEnvEntry>, schema: Schema, preservesModule = '@preserves/core'): string {
|
||||
const literals = new Dictionary<string, never>();
|
||||
const types: Array<Item> = [];
|
||||
const predicates: Array<Item> = [];
|
||||
const functions: Array<Item> = [];
|
||||
let temps: Array<string> = [];
|
||||
|
||||
function environmentWith<R>(name: symbol,
|
||||
|
@ -234,12 +234,20 @@ export function compile(env: Array<CompileEnvEntry>, schema: Schema, preservesMo
|
|||
}
|
||||
types.push(
|
||||
seq(`export type ${name.description!} = `, typeFor(pattern), `;`));
|
||||
predicates.push(
|
||||
functions.push(
|
||||
seq('export function ', `is${name.description!}`,
|
||||
'(v: any): v is ', name.description!, ' ',
|
||||
block(
|
||||
... temps.length > 0 ? [seq('let ', commas(... temps), ': any')] : [],
|
||||
seq('return ', recognizer))));
|
||||
functions.push(
|
||||
seq('export function ', `as${name.description!}`,
|
||||
'(v: any): ', name.description!, ' ',
|
||||
block(
|
||||
seq(`if (!is${name.description!}(v)) `,
|
||||
block(`throw new TypeError("${name.description!}")`),
|
||||
' else ',
|
||||
block(`return v`)))));
|
||||
}
|
||||
|
||||
const f = new Formatter();
|
||||
|
@ -260,7 +268,7 @@ export function compile(env: Array<CompileEnvEntry>, schema: Schema, preservesMo
|
|||
});
|
||||
f.newline();
|
||||
|
||||
predicates.forEach(p => {
|
||||
functions.forEach(p => {
|
||||
f.write(p);
|
||||
f.newline();
|
||||
f.newline();
|
||||
|
|
|
@ -54,9 +54,7 @@ export function parseSchema(toplevelTokens: Array<Input>): Schema {
|
|||
}
|
||||
definitions.set(name, parseDefinition(name, clause.slice(2).map(peel)));
|
||||
} else if (clause.length === 2 && is(clause[0], M.$version)) {
|
||||
// TODO: use asVersion
|
||||
if (!M.isVersion(clause[1])) invalidClause(clause);
|
||||
version = clause[1];
|
||||
version = M.asVersion(clause[1]);
|
||||
} else {
|
||||
invalidClause(clause);
|
||||
}
|
||||
|
|
|
@ -110,8 +110,12 @@ export function isSchema(v: any): v is Schema {
|
|||
);
|
||||
}
|
||||
|
||||
export function asSchema(v: any): Schema {if (!isSchema(v)) {throw new TypeError("Schema");} else {return v;};}
|
||||
|
||||
export function isVersion(v: any): v is Version {return v === $1;}
|
||||
|
||||
export function asVersion(v: any): Version {if (!isVersion(v)) {throw new TypeError("Version");} else {return v;};}
|
||||
|
||||
export function isPattern(v: any): v is Pattern {
|
||||
return (
|
||||
(
|
||||
|
@ -222,6 +226,8 @@ export function isPattern(v: any): v is Pattern {
|
|||
);
|
||||
}
|
||||
|
||||
export function asPattern(v: any): Pattern {if (!isPattern(v)) {throw new TypeError("Pattern");} else {return v;};}
|
||||
|
||||
export function isNamedPattern(v: any): v is NamedPattern {
|
||||
return (
|
||||
(
|
||||
|
@ -233,4 +239,8 @@ export function isNamedPattern(v: any): v is NamedPattern {
|
|||
);
|
||||
}
|
||||
|
||||
export function asNamedPattern(v: any): NamedPattern {
|
||||
if (!isNamedPattern(v)) {throw new TypeError("NamedPattern");} else {return v;};
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue