Skip to content

Commit

Permalink
tdl-install-types: Update the generator
Browse files Browse the repository at this point in the history
Added $FunctionResultByName, $FunctionInputByName special object types
that are now used in Invoke and Execute instead of chains of extends +
conditional types (should be faster to typecheck, also less repetitive,
and can be used in flow). Also added $FunctionName and $SyncFunctionName
that are used in "extends" in the type parameters of Invoke and Execute
respectively.
  • Loading branch information
eilvelia committed Apr 30, 2024
1 parent 4bb189d commit 8cd53e2
Show file tree
Hide file tree
Showing 7 changed files with 5,016 additions and 3,815 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@
generated files.
- No longer generates `null` for return types of `Execute`.
- Functions not marked as synchronous are no longer generated in `Execute`.
- Added `$Function`.
- Added some special types: `$Function`, `$FunctionResultByName`,
`$FunctionInputByName`, `$FunctionName`, `$SyncFunctionName`. `Invoke` and
`Execute` are implemented in terms of these types and may be somewhat faster
now (in typecheck time).

## [email protected] (2024-02-16)

Expand Down
2,279 changes: 1,513 additions & 766 deletions flow-typed/tdlib-types_vx.x.x.js

Large diffs are not rendered by default.

89 changes: 45 additions & 44 deletions packages/tdl-install-types/src/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ function generate (
const READONLY_ARRAY = TS ? 'ReadonlyArray' : '$ReadOnlyArray'
const READONLY = TS ? 'readonly ' : '+'
const EXACT = TS ? '' : '|'
const INEXACT = TS ? '' : ', ...'
const EXPORT = TS ? 'export' : 'declare export'
// const EMPTY_OBJ = TS ? '{}' : '{...}'
const EXTENDS = TS ? ' extends' : ':'

// console.log(JSON.stringify(tldoc(source), null, ' '))

Expand Down Expand Up @@ -192,18 +193,6 @@ function generate (
.filter(x => x != null)
.join(EOL + EOL)

function genFunctionIntersection (
name/*: string */,
getReturnType/*: (name: string) => string */,
classes/*: TdClass[] */
) {
return [
`${EXPORT} type ${name} =`,
...classes
.map(c => ` & ((query: ${c.name}) => ${getReturnType(c.result)})`)
].join(EOL)
}

// function genApiObjectFunction (c/*: TdClass */) {
// const param = c.parameters.length < 1
// ? `params?: ${EMPTY_OBJ}`
Expand All @@ -213,39 +202,48 @@ function generate (
// ` ${c.name}(${param}): Promise<${c.result}>,`
// ].join(EOL)
// }

// const apiObject = [
// `${EXPORT} type ApiObject = {${EXACT}`,
// ...funcs.map(genApiObjectFunction),
// `${EXACT}}`
// ].join(EOL)

let invoke
let execute
const functionUnionType = [
`${EXPORT} type $Function =`,
funcs.map(c => ' | ' + c.name).join(EOL)
].join(EOL)

if (TS) {
invoke = [
`${EXPORT} type Invoke = <T>(query: { readonly _: T } & (`,
' ' + funcs.map(c => c.name).join(`${EOL} | `),
')) => Promise<',
...funcs.map(c => ` T extends '${c.name}' ? ${c.result} :`),
'never>'
].join(EOL)
execute = [
`${EXPORT} type Execute = <T>(query: { readonly _: T } & (`,
' ' + syncFuncs.map(c => c.name).join(`${EOL} | `),
')) => error | (',
...syncFuncs.map(c => ` T extends '${c.name}' ? ${c.result} :`),
'never)'
].join(EOL)
} else {
invoke = genFunctionIntersection('Invoke', t => `Promise<${t}>`, funcs)
execute = genFunctionIntersection('Execute', t => `${t} | error`, syncFuncs)
}
const nameToResultTable = [
`${EXPORT} type $FunctionResultByName = {`,
funcs.map(c => ` ${c.name}: ${c.result},`).join(EOL),
'}'
].join(EOL)

const functionType = [
'type $Function =',
funcs.map(c => ' | ' + c.name).join(EOL)
const nameToInputTable = [
`${EXPORT} type $FunctionInputByName = {`,
funcs.map(c => ` ${c.name}: ${c.name},`).join(EOL),
'}'
].join(EOL)

const funName = TS
? `${EXPORT} type $FunctionName = keyof $FunctionResultByName`
: `${EXPORT} type $FunctionName = $Keys<$FunctionResultByName>`

const invoke = [
`${EXPORT} type Invoke = <T${EXTENDS} $FunctionName>(`,
` query: { ${READONLY}_: T${INEXACT} } & $FunctionInputByName[T]`,
') => Promise<$FunctionResultByName[T]>'
].join(EOL)

const syncFuncName = [
`${EXPORT} type $SyncFunctionName =`,
syncFuncs.map(c => ` | '${c.name}'`).join(EOL)
].join(EOL)

const execute = [
`${EXPORT} type Execute = <T${EXTENDS} $SyncFunctionName>(`,
` query: { ${READONLY}_: T${INEXACT} } & $FunctionInputByName[T]`,
') => error | $FunctionResultByName[T]'
].join(EOL)

return [
Expand All @@ -260,12 +258,15 @@ function generate (
unionTypes,
'',
'// --- Special types ---',
'',
invoke,
'',
execute,
'',
functionType
...[
functionUnionType,
nameToResultTable,
nameToInputTable,
funName,
invoke,
syncFuncName,
execute
].flatMap(x => ['', x])
].join(EOL)),
'}',
''
Expand Down
Loading

0 comments on commit 8cd53e2

Please sign in to comment.