Skip to content

Commit

Permalink
Show types correctly in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
GoogleFeud committed Aug 21, 2023
1 parent 481fc3b commit 7b667b3
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/program.ts → src/type-resolve/program.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as ts from "typescript";
import type { ProgramTransformerExtras, PluginConfig } from "ts-patch";
import { MacroTransformer } from "./transformer";
import { TsMacrosConfig, macros } from "./index";
import { MacroTransformer } from "../transformer";
import { TsMacrosConfig, macros } from "../index";

export function printAsTS(printer: ts.Printer, source: ts.SourceFile) : string {
export function printAsTS(printer: ts.Printer, statements: ts.NodeArray<ts.Statement>, source: ts.SourceFile) : string {
let fileText = "";
for (const fileItem of source.statements) {
for (const fileItem of statements) {
fileText += printer.printNode(ts.EmitHint.Unspecified, fileItem, source);
}
return fileText;
}
}

export function patchCompilerHost(host: ts.CompilerHost | undefined, config: ts.CompilerOptions | undefined, newSourceFiles: Map<string, ts.SourceFile>, instance: typeof ts) : ts.CompilerHost {
const compilerHost = host || instance.createCompilerHost(config || instance.getDefaultCompilerOptions(), true);
Expand All @@ -29,26 +29,37 @@ export default function (
options: PluginConfig,
extras: ProgramTransformerExtras
) : ts.Program {
const isTSC = process.argv[1]?.endsWith("tsc");

const instance = extras.ts as typeof ts;
const transformer = new MacroTransformer(instance.nullTransformationContext, program.getTypeChecker(), macros, options as TsMacrosConfig);
const newSourceFiles = new Map();

const compilerOptions = program.getCompilerOptions();
const printer = instance.createPrinter();

for (const sourceFile of program.getSourceFiles()) {
if (sourceFile.isDeclarationFile) continue;

const parsed = transformer.run(sourceFile);
if (!instance.isSourceFile(parsed)) continue;
newSourceFiles.set(sourceFile.fileName, instance.createSourceFile(sourceFile.fileName, printAsTS(printer, parsed), sourceFile.languageVersion));
if (isTSC) newSourceFiles.set(sourceFile.fileName, instance.createSourceFile(sourceFile.fileName, printAsTS(printer, parsed.statements, parsed), sourceFile.languageVersion, true, ts.ScriptKind.TS));
else {
const newNodes = [];
for (const statement of parsed.statements) {
if (statement.pos === -1 && (ts.isTypeDeclaration(statement) || ts.isVariableStatement(statement))) {
newNodes.push(statement);
}
}
const newNodesOnly = printAsTS(printer, instance.factory.createNodeArray(newNodes), parsed);
const newNodesSource = instance.createSourceFile(sourceFile.fileName, sourceFile.text + "\n" + newNodesOnly, sourceFile.languageVersion, true, ts.ScriptKind.TS);
ts.sys.writeFile(`${sourceFile.fileName}_log.txt`, newNodesSource.text);
newSourceFiles.set(sourceFile.fileName, newNodesSource);
}
}

const compilerOptions = program.getCompilerOptions();

return instance.createProgram(
program.getRootFileNames(),
compilerOptions,
patchCompilerHost(host, compilerOptions, newSourceFiles, instance),
program
patchCompilerHost(host, compilerOptions, newSourceFiles, instance)
);
}

0 comments on commit 7b667b3

Please sign in to comment.