Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript -> AST -> TypeScript

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.

like image 569
cape Avatar asked Oct 07 '16 12:10

cape


People also ask

What is AST in TypeScript?

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.

What is AST string?

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.

What is AST expression?

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).

What is AST in JavaScript?

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.


1 Answers

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.

like image 80
Andzej Maciusovic Avatar answered Oct 14 '22 23:10

Andzej Maciusovic