2017-07-21 9 views
2

TypeScript API Programには、構文エラーを取得する方法getSyntacticDiagnosticsがあります。しかし、Programがない場合、ちょうどSourceFile私はどのように同じ種類の情報を得ることができますか?SourceFile作成用TypeScript API:構文エラー情報を取得する方法

私はcreateSourceFileでfileName(文字列)パラメータは仮想ファイル名であることを忘れないでください

function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean, scriptKind?: ScriptKind): SourceFile; 

答えて

1

を通じてSourceFileを作成しました。このfileName(文字列)は、TypeScriptライブラリの使用中にグローバルに使用されます。

あなたが必要とする最も重要なことは、createProgamメソッドに書かれている文書コメントです。 プログラムは、コンパイル単位を表す 'SourceFile'と 'CompilerOptions'の不変なコレクションです。createProgamメソッドは、このプログラムで使用されるファイルの仮想名である文字列のリストを必要とします。

前の2つの理論的な段落を理解できない場合は、サンプルのコメントを参考にしてください。

// this is the real code of file. Use fs.readFile, fs.readFileSync or something other to load file real source code. 
var code = "class 1X {}"; 

// I will use this virtual name to reference SourceFile while working with TypeScript API. 
var virtualFileName = "aaa.ts"; 

// initialize SourceFile instance 
var sf = ts.createSourceFile(virtualFileName, code, ts.ScriptTarget.ES5); 

// Make program as collection of my one virtual file 
var prog = ts.createProgram([virtualFileName], {}); 

// process response as you need. 
console.log(prog.getSyntacticDiagnostics(sf)); 
関連する問題