Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: get syntax tree

I had read "whole internet", but can't find any examples about getting syntax tree (just like in Esprima) from TypeScrypt source. I mean how can i get object like this (Esprima Parser example)

{     "type": "Program",     "body": [         {             "type": "VariableDeclaration",             "declarations": [                 {                     "type": "VariableDeclarator",                     "id": {                         "type": "Identifier",                         "name": "answer"                     },                     "init": {                         "type": "BinaryExpression",                         "operator": "*",                         "left": {                             "type": "Literal",                             "value": 6,                             "raw": "6"                         },                         "right": {                             "type": "Literal",                             "value": 7,                             "raw": "7"                         }                     }                 }             ],             "kind": "var"         }     ] } 

from javascript code

var answer = 6 * 7; 

only for TypeScript source text?

P.S. I hope very much for your help, because I do not want to write your own terrible bicycle)

P.P.S. I think the lib files typescript.ts(.js) and typescriptServices.ts(.js) to help me, but I do not know how :(

Solved

Thanks a lot to the user Steve Fenton. Here is my code, if anyone interested in:

// uses var typeScriptLS =  new Harness.TypeScriptLS(); var ServicesFactory = new Services.TypeScriptServicesFactory(); var serviceShim = ServicesFactory.createLanguageServiceShim(typeScriptLS);  // add lib.d.ts var _libText = window.document.getElementById('lib.d.ts').innerText; typeScriptLS.addScript('lib.d.ts', _libText.replace(/\r\n?/g,"\n"), true);  // add greeter.ts var _sourceText = window.document.getElementById('greeter.ts').innerText; typeScriptLS.addScript('greeter.ts', _sourceText.replace(/\r\n?/g,"\n"), true);  // script name var _scriptName = 'greeter.ts'; // get syntax tree var _st = serviceShim.languageService.getSyntaxTree(_scriptName); //console.log(_st); console.log(JSON.stringify(_st, "", 2)); 
like image 287
bukvaG Avatar asked Nov 25 '13 08:11

bukvaG


2 Answers

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); 
like image 61
Ryan Cavanaugh Avatar answered Sep 21 '22 18:09

Ryan Cavanaugh


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.

like image 44
Fenton Avatar answered Sep 24 '22 18:09

Fenton