を通じて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));