2013-11-25 12 views
35

「インターネット全体」を読んでいましたが、TypeScryptソースから構文ツリー(Esprimaのようなもの)を取得する例は見つかりませんでした。 私はどのように私は唯一の活字体ソーステキスト用のJavaScriptコードTypeScript:構文ツリーを取得

var answer = 6 * 7; 

からこのようなオブジェクト(Esprima Parser例)

{ 
    "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" 
     } 
    ] 
} 

得ることができます意味ですか?

P.S.私はあなた自身の恐ろしい自転車を書くことを望まないので、あなたの助けを大いに願っています)

P.P.私は、libにファイルtypescript.ts(.jsファイル)とtypescriptServices.ts(.jsファイル)が私を助けるためにと思うが、私は:(

は、ユーザーのスティーブに

おかげで多くのことを解決方法がわかりませんフェントンに興味がある人ならばここで は、私のコードです:

// 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)); 
+3

? tsc.js、typescript.js、またはtypescriptServices.jsのどこにでも「ハーネス」は表示されません。または、より広義には、このSolvedコードサンプルをどのように実行しますか? –

+0

あなたが待っていたらすみません。/src/harness/ (https://github.com/Microsoft/TypeScript)。しかし、今では私のコードが無効になる可能性があります – bukvaG

+0

[TypeScriptのシンタクスツリーを入手するにはどうすればいいですか?](http://stackoverflow.com/questions/18714501/how-can-weget-the-syntax-tree-タイプコピー) – ColinE

答えて

7

は、この質問はback in September前に思いついた

あなたのためにこれを行います何かは現在ありません - 。。それを行うにはgetSyntaxTreeメソッドを呼び出す方法はありません。

TypeScriptコンパイラはオープンソースですが、TypeScriptコンパイラは完全にTypeScriptで書かれているので、それをスキャンして、何かがあるかどうかを調べることができます。

これは、あなたがオープンソースプロジェクトとしてあなたの作品を公開する大きなチャンスを持っていることです。

EsprimaまたはSpiderMonkeyを使用して、コンパイル済みのJavaScript(実行時に実際に実行されるコード)から構文ツリーを取得することもできます。

+1

+1私はすでに他の質問を持っています:) – basarat

+0

あなたの助言に多くのおかげで、それは私をたくさん助けました。 – bukvaG

12

TypeScriptパーサは、そのようなツリーを直接生成しませんが、オブジェクトモデルを使用してあらゆる種類の処理を実行できます。いくつかのツールでこれを使用して、たとえばテスト目的で構文変換を行います。ここでは、構文木を印刷するのに使用することができ抜粋です:

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); 
+0

これをどのようにしてapp.jsファイルに入れますか?:var TypeScript = require( './ typescriptServices');実行するように? –

+0

私はここで詳細を説明しました:http://stackoverflow.com/questions/23983998/how-do-i-require-the-typescriptservices-d-ts-in-a-node-js-app –

+2

私は考えましたhttp://blog.ctaggart.com/2014/06/typescript-ast-from-nodejs.html –

0

、私は非常に良い仕事ができることrecastを発見しました。例:

var recast = require('recast'); 
var ast = recast.parse(`var answer = 6 * 7;`); 
console.log(ast); 

これが出力されますすべての必要な情報やイベントTypeAnnotationので、このlibには本当に素晴らしいです:)

`Harness`が展開されたファイルで定義されている
[ 
    { 
     "type": "VariableDeclaration", 
     "declarations": [ 
     { 
      "type": "VariableDeclarator", 
      "id": { 
       "type": "Identifier", 
       "name": "answer", 
       "typeAnnotation": { 
        "type": "TypeAnnotation", 
        "typeAnnotation": { 
        "type": "NumberTypeAnnotation", 
        "loc": { 
         "start": { 
          "line": 1, 
          "column": 12 
         }, 
         "end": { 
          "line": 1, 
          "column": 18 
         }, 
         "lines": {}, 
         "indent": 0 
        } 
        }, 
        "loc": { 
        "start": { 
         "line": 1, 
         "column": 10 
        }, 
        "end": { 
         "line": 1, 
         "column": 18 
        }, 
        "lines": {}, 
        "indent": 0 
        } 
       }, 
       "loc": { 
        "start": { 
        "line": 1, 
        "column": 4 
        }, 
        "end": { 
        "line": 1, 
        "column": 18 
        }, 
        "lines": {}, 
        "indent": 0 
       } 
      }, 
      "init": { 
       "type": "BinaryExpression", 
       "operator": "*", 
       "left": { 
        "type": "Literal", 
        "value": 6, 
        "raw": "6", 
        "loc": { 
        "start": { 
         "line": 1, 
         "column": 21 
        }, 
        "end": { 
         "line": 1, 
         "column": 22 
        }, 
        "lines": {}, 
        "indent": 0 
        } 
       }, 
       "right": { 
        "type": "Literal", 
        "value": 7, 
        "raw": "7", 
        "loc": { 
        "start": { 
         "line": 1, 
         "column": 25 
        }, 
        "end": { 
         "line": 1, 
         "column": 26 
        }, 
        "lines": {}, 
        "indent": 0 
        } 
       }, 
       "loc": { 
        "start": { 
        "line": 1, 
        "column": 21 
        }, 
        "end": { 
        "line": 1, 
        "column": 26 
        }, 
        "lines": {}, 
        "indent": 0 
       } 
      }, 
      "loc": { 
       "start": { 
        "line": 1, 
        "column": 4 
       }, 
       "end": { 
        "line": 1, 
        "column": 26 
       }, 
       "lines": {}, 
       "indent": 0 
      } 
     } 
     ], 
     "kind": "var", 
     "loc": { 
     "start": { 
      "line": 1, 
      "column": 0 
     }, 
     "end": { 
      "line": 1, 
      "column": 27 
     }, 
     "lines": {}, 
     "indent": 0 
     } 
    } 
] 
+0

私が試した限り、リキャストは完全なタイスクリプト構文を認識しません。 – Jaime

関連する問題