TypeScript: get syntax tree TypeScript: get syntax tree typescript typescript

TypeScript: get syntax tree


The TypeScript parser doesn't directly produce a tree like that, but you can still use its object model to do all sorts of things. We use it in some tools to do syntax transforms for testing purposes, for example. Here's a snippet that you can use to print the syntax tree:

import ts = require('typescript');const code = "enum { x = 1 }"const sc = ts.createSourceFile('x.ts', code, ts.ScriptTarget.Latest, true);let indent = 0;function print(node: ts.Node) {    console.log(new Array(indent + 1).join(' ') + ts.SyntaxKind[node.kind]);    indent++;    ts.forEachChild(node, print);    indent--;}print(sc);


This question came up before back in September.

There isn't currently something that will do this for you - there is no magic getSyntaxTree method to call that will do this.

The TypeScript compiler is open-source, though - and written entirely in TypeScript so you can scan it to find out if there is something you can use / add a handle to.

The up-side of this is that you have a big opportunity to release your work as an open-source project as judging by the up-votes on the two questions, there is some demand for this.

Alternatively, grab the syntax tree from the compiled JavaScript (which is the code that will actually execute at runtime) using Esprima or SpiderMonkey.


Using recast and babylon@next is possible. Although you will have to trust in the syntax defined by these technologies for representing TypeScript code AST and that they will keep up to date - since TypeScript has new language features release by release (short period of time) - is not like other languages (JavaScript) where you have well defined versions and released in a standard - so if your users start using new language features these technologies (I guess babylon) should keep up to date or the parsing will fail

// npm install recast babylon@nextconst source = `interface I {  color: string}class C implements I{  color: string='blue'}`const recast = require('recast')const tsParser = require("recast/parsers/typescript")const ast = recast.parse(source, {  parser: tsParser});console.log(`CODE: ${source}AST: ${JSON.stringify(ast)}`);