/** @experimental */ import type ts from "typescript"; /** Simplified type. Does not include all fields. */ export interface SourceMap { mappings: ""; } /** * Using undocumented TypeScript APIs can break at any time. Moreover, with the * transition to tsgo, the code using these APIs will definitely need to be * refactored. * * However, we are still using them for now because: * - until tsgo provides better API, this is the best way we have * - the migration to tsgo may require significant refactoring anyway, as * IPC overhead requires TypeScript to rethink the public API. * * Use cases for undocumented TypeScript APIs: * - workaround for there being no public API for printing a file with source * maps. See: https://github.com/microsoft/TypeScript/issues/51329#issuecomment-2197859814 * - TS has alternative API (program.emit), but it is very slow and does * type-checking. If tsgo has an API we need, we could use it. Otherwise * would have to use an alternative AST+transformer+printer in Lumina. * - print TypeNode object literals across multiple lines rather than single * line * - alternative: use regex hacks * - keep track of the location of each TypeReferenceNode as it is printed to * populate the ApiTypeReference.start and .end fields. Alternatives: * - alternative 1: write custom printer for type nodes, with fallback to * TypeScript's printer for unknown nodes and `typeof ` syntax. * Much more control, much better performance, and less exposure to tsgo. * But is much more complexity. * - alternative 2: before printing or as part of printing, replace all * TypeReferenceNodes with markers (`__ref_1__`), then post-process the * print result. * - alternative 3: collect list of type references before printing. after * printing, do a regex search on print string and try matching each type * reference to places where it is printed. */ export const unsafeUndocumentedTs: Required; export interface UndocumentedTypeScript { /** * @param host * @param basename * @param sourceRoot * @param dirname * @param compilerOptions */ createSourceMapGenerator?(host: Pick, basename: string, sourceRoot: string, dirname: string, compilerOptions: Record | ts.CompilerOptions): SourceMapGenerator; /** @param newLine */ createTextWriter?(newLine: string): EmitTextWriter; /** * @param options - Printer has undocumented options for printing source maps: * https://github.com/microsoft/TypeScript/blob/7319968e90600102892a79142fb804bcbe384160/src/compiler/types.ts#L9689C18-L9707 * @param handlers */ createPrinter(options: ts.CompilerOptions & ts.PrinterOptions, handlers?: ts.PrintHandlers): ReturnType & UndocumentedPrinter; } export interface SourceMapGenerator { toJSON(): SourceMap; } export interface UndocumentedPrinter { /** * @param writeFile * @param writer * @param sourceMapGenerator */ writeFile?(writeFile: ts.SourceFile, writer: EmitTextWriter, sourceMapGenerator: SourceMapGenerator | undefined): void; /** * @param hint * @param node * @param sourceFile * @param output */ writeNode?(hint: ts.EmitHint, node: ts.Node, sourceFile: ts.SourceFile | undefined, output: EmitTextWriter): void; } export interface EmitTextWriter { getText(): string; getTextPos(): number; clear(): void; } /** * @param compilerOptions * @param handlers */ export function unsafeGetUndocumentedPrinter(compilerOptions: ts.CompilerOptions, handlers?: ts.PrintHandlers): ReturnType & UndocumentedPrinter;