mtcute/packages/tl-utils/src/stringify.ts
Alina Tumanova f5976a2d74
ESM + end-to-end tests (#11)
* feat: moved tl-runtime to esm and native ArrayBuffers

* feat: migration to esm

* fix(core): web-related fixes

* test: finally, some good fucking e2e

* chore: fixed linters etc

* ci: added e2e to ci

* build(tl): fixed gen-code on node 20

* fix: codegen Uint8Array, not Buffer

never `git reset --hard` kids

* build: only do type-aware linting for `packages/*`

* build: ignore no-unresolved in ci for e2e

* fix: node 16 doesn't have subtle crypto apparently?

* fix(tests): use Uint8Array

for gods sake please can i just merge this already

* ci: don't parallel tasks in ci

because machines are utter garbage and it may just randomly break

* ci: pass secrets to e2e tests

* ci: separate cli command for ci

apparently im retarded

* fix: run codegen in e2e

im actually retarded

* ci: more fixes for e2e

* ci: debugging stuff

* ci: still debugging

* ci: hopefully fix ci???
2023-10-16 19:23:53 +03:00

64 lines
1.7 KiB
TypeScript

import { TlEntry } from './types.js'
import { stringifyArgumentType } from './utils.js'
function normalizeType(s: string): string {
return s
.replace(/(?<=^|\?)bytes/, 'string')
.replace(/</g, ' ')
.replace(/>/g, '')
.replace('int53', 'long')
}
/**
* Generate TL definition for a given entry.
*
* @param entry Entry to generate definition for
* @param forIdComputation
* Whether to generate definition for constructor ID computation
* (it has slightly different syntax, will not contain `true` flags, etc.)
*/
export function writeTlEntryToString(entry: TlEntry, forIdComputation = false): string {
let str = entry.name
if (!forIdComputation && entry.id) {
str += '#' + entry.id.toString(16)
}
str += ' '
if (entry.generics) {
for (const g of entry.generics) {
if (forIdComputation) {
str += g.name + ':' + g.type + ' '
} else {
str += '{' + g.name + ':' + g.type + '} '
}
}
}
for (const arg of entry.arguments) {
if (forIdComputation && arg.typeModifiers?.predicate && arg.type === 'true') {
continue
}
str += arg.name + ':'
const type = stringifyArgumentType(arg.type, arg.typeModifiers)
if (forIdComputation) {
str += normalizeType(type) + ' '
} else {
str += type + ' '
}
}
const type = entry.typeModifiers ? stringifyArgumentType(entry.type, entry.typeModifiers) : entry.type
if (forIdComputation) {
str += '= ' + normalizeType(type)
} else {
str += '= ' + type + ';'
}
return str
}