13

Is there a way to parse a TypeScript file to an AST, modify the AST, and parse it then back to TypeScript as the tools Esprima + Escodegen are able to?

Important is that I do NOT want to compile/transpile the TypeScript code first into JavaScript.

cape
  • 171
  • 1
  • 9
  • 1
    Might https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter be what you're after? – Joe Clay Oct 07 '16 at 13:00
  • @JoeClay The problem is that there is apparently no way to modify the AST of the .ts file with the public API. I think this is what is discussed also here in [#7580](https://github.com/Microsoft/TypeScript/issues/7580). Am I right? – cape Oct 13 '16 at 14:49
  • Looks like it ): You might have to do some digging through the private API if you want to achieve this right now, by the looks of things. – Joe Clay Oct 13 '16 at 15:50
  • In https://astexplorer.net/ you could see your AST (under menu 'acorn' choose 'typescript') – Stefan van de Vooren Nov 07 '18 at 10:18

1 Answers1

7

Yes, with Typescript 2.x you can transform ast. Here is a good blog post about it http://blog.scottlogic.com/2017/05/02/typescript-compiler-api-revisited.html. In the official typescript wiki it is not well documented yet.

Andzej Maciusovic
  • 4,306
  • 1
  • 29
  • 40