|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @fileoverview Helper functions and typings for utilizing internal TypeScript printer |
| 11 | + * and sourcemap generation APIs. |
| 12 | + */ |
| 13 | + |
| 14 | +import assert from 'node:assert'; |
| 15 | +import { dirname } from 'node:path'; |
| 16 | +import ts from 'typescript'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Partial interface representing the internal TypeScript `EmitTextWriter` API. |
| 20 | + * Used to collect printed text output from the printer. |
| 21 | + */ |
| 22 | +export interface EmitTextWriter { |
| 23 | + write(s: string): void; |
| 24 | + rawWrite(s: string): void; |
| 25 | + writeLine(force?: boolean): void; |
| 26 | + getText(): string; |
| 27 | + clear(): void; |
| 28 | + isAtStartOfLine(): boolean; |
| 29 | + getTextPos(): number; |
| 30 | + writeComment(s: string): void; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Partial interface representing the internal TypeScript `SourceMapGenerator` API. |
| 35 | + * Used to construct mappings and serialize a sourcemap. |
| 36 | + */ |
| 37 | +export interface SourceMapGenerator { |
| 38 | + getSources(): string[]; |
| 39 | + addSource(fileName: string): number; |
| 40 | + setSourceContent(sourceIndex: number, content: string | null): void; |
| 41 | + addMapping( |
| 42 | + generatedLine: number, |
| 43 | + generatedCharacter: number, |
| 44 | + sourceIndex: number, |
| 45 | + sourceLine: number, |
| 46 | + sourceCharacter: number, |
| 47 | + nameIndex?: number, |
| 48 | + ): void; |
| 49 | + toJSON(): object; |
| 50 | + toString(): string; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Extended `ts.Printer` interface containing the internal `writeFile` method |
| 55 | + * which supports sourcemap generation. |
| 56 | + */ |
| 57 | +export interface ExtendedPrinter extends ts.Printer { |
| 58 | + writeFile( |
| 59 | + sourceFile: ts.SourceFile, |
| 60 | + writer: EmitTextWriter, |
| 61 | + sourceMapGenerator?: SourceMapGenerator, |
| 62 | + ): void; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Typing structure for internal TypeScript module exports that are not exposed |
| 67 | + * in the public `@types/typescript` package. |
| 68 | + */ |
| 69 | +export interface TypeScriptInternals { |
| 70 | + createTextWriter(newLine: string): EmitTextWriter; |
| 71 | + createSourceMapGenerator( |
| 72 | + host: { |
| 73 | + getCurrentDirectory(): string; |
| 74 | + getCanonicalFileName(fileName: string): string; |
| 75 | + }, |
| 76 | + file: string, |
| 77 | + sourceRoot: string | undefined, |
| 78 | + sourcesDirectoryPath: string, |
| 79 | + generatorOptions: ts.CompilerOptions, |
| 80 | + ): SourceMapGenerator; |
| 81 | +} |
| 82 | + |
| 83 | +const tsInternals = ts as unknown as TypeScriptInternals; |
| 84 | + |
| 85 | +/** |
| 86 | + * Asserts that the required internal TypeScript APIs are present in the currently |
| 87 | + * loaded TypeScript module. |
| 88 | + * |
| 89 | + * @throws {AssertionError} If any required internal API is missing. |
| 90 | + */ |
| 91 | +export function assertTypeScriptPrinterInternals(): void { |
| 92 | + assert( |
| 93 | + typeof tsInternals.createTextWriter === 'function', |
| 94 | + 'TypeScript internal "createTextWriter" is missing.', |
| 95 | + ); |
| 96 | + assert( |
| 97 | + typeof tsInternals.createSourceMapGenerator === 'function', |
| 98 | + 'TypeScript internal "createSourceMapGenerator" is missing.', |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Result object returned from printing a source file. |
| 104 | + */ |
| 105 | +export interface PrintResult { |
| 106 | + /** The printed source code text. */ |
| 107 | + code: string; |
| 108 | + |
| 109 | + /** The generated sourcemap JSON string, if sourcemap generation was requested. */ |
| 110 | + map?: string; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Prints a TypeScript source file AST to a string, optionally generating a sourcemap |
| 115 | + * using internal TypeScript APIs. |
| 116 | + * |
| 117 | + * @param sourceFile The TypeScript AST node representing the file to print. |
| 118 | + * @param printer The printer instance to print the file with. |
| 119 | + * @param compilerHost The TypeScript compiler host, used for path canonicalization and context. |
| 120 | + * @param compilerOptions The compiler options configured for the build target. |
| 121 | + * @returns A result containing the printed code and optional sourcemap text. |
| 122 | + */ |
| 123 | +export function printSourceFileWithMap( |
| 124 | + sourceFile: ts.SourceFile, |
| 125 | + printer: ts.Printer, |
| 126 | + compilerHost: ts.CompilerHost, |
| 127 | + compilerOptions: ts.CompilerOptions, |
| 128 | +): PrintResult { |
| 129 | + const shouldGenerateMap = compilerOptions.sourceMap || compilerOptions.inlineSourceMap; |
| 130 | + if (!shouldGenerateMap) { |
| 131 | + return { code: printer.printFile(sourceFile) }; |
| 132 | + } |
| 133 | + |
| 134 | + assertTypeScriptPrinterInternals(); |
| 135 | + |
| 136 | + const extendedPrinter = printer as ExtendedPrinter; |
| 137 | + assert( |
| 138 | + typeof extendedPrinter.writeFile === 'function', |
| 139 | + 'TypeScript Printer is missing internal "writeFile" method.', |
| 140 | + ); |
| 141 | + |
| 142 | + const writer = tsInternals.createTextWriter(compilerHost.getNewLine()); |
| 143 | + |
| 144 | + const sourceMapGenerator = tsInternals.createSourceMapGenerator( |
| 145 | + { |
| 146 | + getCurrentDirectory: () => compilerHost.getCurrentDirectory(), |
| 147 | + getCanonicalFileName: (fileName) => compilerHost.getCanonicalFileName(fileName), |
| 148 | + }, |
| 149 | + sourceFile.fileName, |
| 150 | + compilerOptions.sourceRoot, |
| 151 | + dirname(sourceFile.fileName), |
| 152 | + compilerOptions, |
| 153 | + ); |
| 154 | + |
| 155 | + extendedPrinter.writeFile(sourceFile, writer, sourceMapGenerator); |
| 156 | + |
| 157 | + const code = writer.getText(); |
| 158 | + const map = sourceMapGenerator.toString(); |
| 159 | + |
| 160 | + return { code, map }; |
| 161 | +} |
0 commit comments