Refactoring of uncertain benefit: allow multiple function definitions at once
This commit is contained in:
parent
e12d978915
commit
6576f30639
|
@ -31,13 +31,13 @@ export function compile(
|
|||
` = `, renderType(mod, t), `;`));
|
||||
|
||||
if (t.kind === 'union') {
|
||||
mod.defineFunction(_ctx =>
|
||||
seq(`export namespace ${nameStr} `, block(
|
||||
... Array.from(t.variants).map(([vn, vt]) =>
|
||||
mod.defineFunctions(_ctx =>
|
||||
[seq(`export namespace ${nameStr} `, block(
|
||||
... Array.from(t.variants).flatMap(([vn, vt]) =>
|
||||
genConstructor(mod, vn, vn, vt, t, resultTypeItem))
|
||||
)));
|
||||
))]);
|
||||
} else {
|
||||
mod.defineFunction(_ctx =>
|
||||
mod.defineFunctions(_ctx =>
|
||||
genConstructor(mod, nameStr, void 0, t, t, resultTypeItem));
|
||||
}
|
||||
}
|
||||
|
@ -46,26 +46,26 @@ export function compile(
|
|||
const t = typeForDefinition(mod.resolver(), def);
|
||||
const name = name0 as symbol;
|
||||
|
||||
mod.defineFunction(ctx =>
|
||||
seq(`export function as${name.description!}`, mod.genericParameters(),
|
||||
`(v: _.Value<_embedded>): `, name.description!, mod.genericArgsFor(t), ` `,
|
||||
ctx.block(() => [
|
||||
seq(`let result = to${name.description!}(v)`),
|
||||
seq(`if (result === void 0) `,
|
||||
`throw new TypeError(\`Invalid ${name.description!}: \${_.stringify(v)}\`)`),
|
||||
seq(`return result`)])));
|
||||
mod.defineFunctions(ctx =>
|
||||
[seq(`export function as${name.description!}`, mod.genericParameters(),
|
||||
`(v: _.Value<_embedded>): `, name.description!, mod.genericArgsFor(t), ` `,
|
||||
ctx.block(() => [
|
||||
seq(`let result = to${name.description!}(v)`),
|
||||
seq(`if (result === void 0) `,
|
||||
`throw new TypeError(\`Invalid ${name.description!}: \${_.stringify(v)}\`)`),
|
||||
seq(`return result`)]))]);
|
||||
|
||||
mod.defineFunction(ctx =>
|
||||
seq(`export function to${name.description!}`, mod.genericParameters(),
|
||||
`(v: _.Value<_embedded>): undefined | `, name.description!, mod.genericArgsFor(t), ` `,
|
||||
ctx.block(() => [seq(`let result: undefined | `, name.description!, mod.genericArgsFor(t)),
|
||||
... converterForDefinition(ctx, def, `v`, `result`),
|
||||
seq(`return result`)])));
|
||||
mod.defineFunctions(ctx =>
|
||||
[seq(`export function to${name.description!}`, mod.genericParameters(),
|
||||
`(v: _.Value<_embedded>): undefined | `, name.description!, mod.genericArgsFor(t), ` `,
|
||||
ctx.block(() => [seq(`let result: undefined | `, name.description!, mod.genericArgsFor(t)),
|
||||
... converterForDefinition(ctx, def, `v`, `result`),
|
||||
seq(`return result`)]))]);
|
||||
|
||||
mod.defineFunction(ctx =>
|
||||
seq(`export function from${name.description!}`, mod.genericParameters(),
|
||||
`(_v: `, name.description!, mod.genericArgsFor(t), `): _.Value<_embedded> `,
|
||||
ctx.block(() => unconverterForDefinition(ctx, def, `_v`))));
|
||||
mod.defineFunctions(ctx =>
|
||||
[seq(`export function from${name.description!}`, mod.genericParameters(),
|
||||
`(_v: `, name.description!, mod.genericArgsFor(t), `): _.Value<_embedded> `,
|
||||
ctx.block(() => unconverterForDefinition(ctx, def, `_v`)))]);
|
||||
}
|
||||
|
||||
const f = new Formatter();
|
||||
|
|
|
@ -83,8 +83,8 @@ export class ModuleContext {
|
|||
this.typedefs.push(f);
|
||||
}
|
||||
|
||||
defineFunction(f: (ctx: FunctionContext) => Item): void {
|
||||
this.functiondefs.push(f(new FunctionContext(this)));
|
||||
defineFunctions(f: (ctx: FunctionContext) => Item[]): void {
|
||||
this.functiondefs.push(... f(new FunctionContext(this)));
|
||||
}
|
||||
|
||||
resolver(modulePath?: M.ModulePath): (ref: M.Ref) => RefType {
|
||||
|
|
|
@ -11,7 +11,7 @@ export function genConstructor(
|
|||
arg: SimpleType,
|
||||
resultType: Type,
|
||||
resultTypeItem: Item,
|
||||
): Item {
|
||||
): Item[] {
|
||||
const formals: Array<[string, FieldType]> = [];
|
||||
let simpleValue = false;
|
||||
|
||||
|
@ -38,13 +38,13 @@ export function genConstructor(
|
|||
braces(...formals.map(f => seq(M.jsId(f[0]), ': ', renderType(mod, f[1])))))]
|
||||
: formals.map(f => seq(M.jsId(f[0]), ': ', renderType(mod, f[1])));
|
||||
|
||||
return seq(`export function ${M.jsId(name)}`, mod.genericParametersFor(resultType),
|
||||
parens(... declArgs),
|
||||
': ', resultTypeItem, ' ', block(
|
||||
seq(`return `,
|
||||
((arg.kind === 'unit' && initializers.length === 0)
|
||||
? 'null'
|
||||
: (simpleValue
|
||||
? 'value'
|
||||
: braces(... initializers))))));
|
||||
return [seq(`export function ${M.jsId(name)}`, mod.genericParametersFor(resultType),
|
||||
parens(...declArgs),
|
||||
': ', resultTypeItem, ' ', block(
|
||||
seq(`return `,
|
||||
((arg.kind === 'unit' && initializers.length === 0)
|
||||
? 'null'
|
||||
: (simpleValue
|
||||
? 'value'
|
||||
: braces(...initializers))))))];
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue