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.
The AST (Abstract Syntax Tree) quickly comes to your mind to approach this problem. The AST is a data structure to represent the structure of your source file in a format readable by machines. Indeed, if I throw the above example in the TypeScript AST Viewer I get immediate access to the AST.
Source code: Lib/ast.py. The ast module helps Python applications to process trees of the Python abstract syntax grammar. The abstract syntax itself might change with each Python release; this module helps to find out programmatically what the current grammar looks like.
The AST expressions are conceptually divided into two classes: constructive expressions (those that can be used in parsing rules) and destructive expressions (those that can be used in pretty printing rules).
An AST is the result of parsing code. For JavaScript, an AST is a JavaScript object containing a tree representation of your source. Before we use it, we have to create it. Depending on the code we are parsing, we choose the appropriate parser. Here since the code is ES5-compatible, we can choose the acorn parser.
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.
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