I'd like to build a tool to perform transformations on typescript code and emit them as typescript (rather than javascript) as part of one-time upgrade path on an existing code base. Is this possible and if so, how? I've found no comprehensive and clear references on the compiler API. Any pointers to references or actual code would be appreciated.
You could do the following:
ts.createSourceFile
—create an AST/ts.SourceFile
for every file.ts.transform
. Provide this with your transforms to use.ts.createPrinter
to create a printer and print out the transformed source files.Some example code is in my answer here.
Alternative
An important point to note about the above solution is that when the printer prints an AST, it will print it with its own formatting in mind for the most part.
If you want to maintain formatting in the files, then you might want to do the following instead:
FileTextChanges
.An example is in my answer here.
Alternative 2
Since you'll only be executing this once on the code base, you will probably save a lot of time by using my library ts-morph instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With