2014-06-03

TypeScript AST from Node.js

One great thing about TypeScript is that it can parse TypeScript files and provide an abstract syntax tree (AST) for them. I created a Node.js console app that will read in the typescriptServices.d.ts definition file and then print all the top level module and class declarations.

The output isn't terribly exciting, but this is what it looks like when run from node on the command line or when starting (F5) via the Node.js Tools for Visual Studio. I upgraded to the latest 2014-05-14 build to make it work.



I'm using the latest Visual Studio 2013 Update 2 that has built in TypeScript support. Basarat says the "Typescript compiler api / or / language service is not readily consumable from node.js." I'm hoping the TypeScript team makes it easier to consume soon. I had to comment out a few lines in typescriptServices.d.ts. I also had to keep commenting out the require statement and put the code in export functions to keep the TypeScript variable namespace from conflicting. I pushed the code to GitHub and here is a gist:

Loading gist ...
The ability to read TypeScript and JavaScript into an AST is very powerful and useful. I'm also hoping it is just as easy to create an AST and emit TypeScript.