cevek / ttypescript

Over TypeScript tool to use custom transformers in the tsconfig.json

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`ttsc` is not working on TS 4.9

samchon opened this issue · comments

ts-node -C ttypescript works, but ttsc is not working.

code transformation does not working.

It looks related to microsoft/TypeScript#51542
probably not an issue of ttypescript

4.9.4 is now out FWIW

TS has been updated to 4.9.4, but transformation is not working yet.

How about you @nonara ?

@DanielRosenwasser May I ask you one thing?

Looking at code of this ttypescript, it transforms TS source code by replacing ts.createProgram function. But after TS 4.9 patch, the transformed.createTransform function never be called. Is there any criticial change on TS not to call the createProgram function or overwrite the createProgram function variable when compiling?

export function patchCreateProgram(tsm: typeof ts, forceReadConfig = false, projectDir = process.cwd()) {
const originCreateProgram = tsm.createProgram as any;
function createProgram(createProgramOptions: ts.CreateProgramOptions): ts.Program;
function createProgram(
rootNames: ReadonlyArray<string>,
options: ts.CompilerOptions,
host?: ts.CompilerHost,
oldProgram?: ts.Program,
configFileParsingDiagnostics?: ReadonlyArray<ts.Diagnostic>
): ts.Program;
function createProgram(
rootNamesOrOptions: ReadonlyArray<string> | ts.CreateProgramOptions,
options?: ts.CompilerOptions,
host?: ts.CompilerHost,
oldProgram?: ts.Program,
configFileParsingDiagnostics?: ReadonlyArray<ts.Diagnostic>
): ts.Program {
let rootNames;
let createOpts: ts.CreateProgramOptions | undefined;
if (!Array.isArray(rootNamesOrOptions)) {
createOpts = rootNamesOrOptions as ts.CreateProgramOptions;
}
if (createOpts) {
rootNames = createOpts.rootNames;
options = createOpts.options;
host = createOpts.host;
oldProgram = createOpts.oldProgram;
configFileParsingDiagnostics = createOpts.configFileParsingDiagnostics;
} else {
options = options!;
rootNames = rootNamesOrOptions as ReadonlyArray<string>;
}
if (forceReadConfig) {
const info = getConfig(tsm, options, rootNames, projectDir);
options = info.compilerOptions;
if (createOpts) {
createOpts.options = options;
}
projectDir = info.projectDir;
}
const program: ts.Program = createOpts
? originCreateProgram(createOpts)
: originCreateProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics);
const plugins = preparePluginsFromCompilerOptions(options.plugins);
const pluginCreator = new PluginCreator(tsm, plugins, projectDir);
const originEmit = program.emit;
/**
* The emit method has the following declaration:
* https://github.com/microsoft/TypeScript/blob/bfc55b5762443c37ecdef08a3b5a4e057b4d1e85/src/compiler/builderPublic.ts#L101
* The declaration specifies 5 arguments, but it's not true. Sometimes the emit method takes 6 arguments.
*/
program.emit = function newEmit(
targetSourceFile?: ts.SourceFile,
writeFile?: ts.WriteFileCallback,
cancellationToken?: ts.CancellationToken,
emitOnlyDtsFiles?: boolean,
customTransformers?: ts.CustomTransformers,
arg?: boolean
): ts.EmitResult {
const mergedTransformers = pluginCreator.createTransformers({ program }, customTransformers);
const result: ts.EmitResult = (originEmit as any)(
targetSourceFile,
writeFile,
cancellationToken,
emitOnlyDtsFiles,
mergedTransformers,
arg
);
// todo: doesn't work with 3.7
// result.diagnostics = [...result.diagnostics, ...transformerErrors.get(program)!];
return result;
};
return program;
}
tsm.createProgram = createProgram;
return tsm;
}

@samchon I'll take a look tomorrow and work it out.

@cevek I just analyzed what makes ttypescript broken for a while. Hope my analysis to be helpful.


I'd tried hard coding special editing on tsc.js of TS and succeeded to transforming by modifing performCompilation function to be exported function. However, the performCompilation function is not exported member, but an internal function of executeCommandLine.ts file.

Therefore, to fix this bug occured after TS 4.9 update, ttypescript should update not only createProgram file, but also executeCommandLine function too.


@DanielRosenwasser Is it possible to change the executeCommandLine function provides an additional and optional prarameter that creating the Program class instance? Or can you re-patch the TS 4.9 (maybe 4.9.5) that only wrapping ts.createProgram can be working again? I think it would be really helpful for @cevek, developer of this ttypescript project and the others using this ttypescript library.

@samchon Libraries like tts and ts-patch augment the TypeScript libraries. It isn't the TS team's responsibility to modify the compiler to support our tools. The functions we modify are internal and not a part of public API, so it's on us to keep up with changes.

If this is an issue, I'll have ts-patch working later today. Unfortunately, I don't have access to modify this library, so you'll have to wait on @cevek to follow suit.

I will update in this thread when I've investigated the issue.

ts 4.9.4 support is added in ts-patch v2.1.0

ts 4.9.4 support is added in ts-patch v2.1.0

ts 4.9.4 support is added in ts-patch v2.1.0

So downgrading typescript from ^5.x.x to ^4.x.x works 🎉

No solution yet for ts5, downgrading to 4 works

Latest ts-patch rc (beta) supports ts v5, and I added some functionality so you don't have to do anything major to migrate from ttypescript. Just install and use tspc instead of ttsc

Here's more detail: