cevek / ttypescript

Over TypeScript tool to use custom transformers in the tsconfig.json
1.53k stars 56 forks source link

Run a transformer from nodejs #114

Closed alber70g closed 3 years ago

alber70g commented 3 years ago

I'm working on scripts that helps do major refactorings in our applications. It involves also moving files, rewriting imports, rewriting code etc. I've created a transformer, and would like to run this as step within a bigger process.

The thing is, currently we just parse the file and get a ts.SourceFile and modify this when needed, and write it back to disk. I'm missing the context parameter that's needed in the ts.visitEachChild(node, visitor, context).

Is it possible to run this program from nodejs? If so, how?

alber70g commented 3 years ago

Nevermind, I've found a solution. I can just run ts.transform(sourceFile, [transformer]);

nonara commented 3 years ago

@alber70g Yep - that works! Another option is to pass nullTransformationContext, which is exported from the typescript package.

I think it's an internal type, but you can use ts-expose-internals to access its type

alber70g commented 3 years ago

@nonara Thanks. The alternative nullTransformationContext is nice as well. That way I don't need to use the ts.TransformerFactory and just return a ts.Transformer. Thank you!