Ele*_*hik 5 api syntax-error typescript
TypeScript API有获取语法错误的Program方法。getSyntacticDiagnostics但如果没有Program,SourceFile我怎样才能得到同样的信息呢?
我SourceFile通过创建
function createSourceFile(fileName: string, sourceText: string, languageVersion: ScriptTarget, setParentNodes?: boolean, scriptKind?: ScriptKind): SourceFile;
Run Code Online (Sandbox Code Playgroud)
请记住,fileNamecreateSourceFile 中的 (string) 参数是虚拟文件名。使用 TypeScript 库时,全局使用此文件名(字符串)。
您需要的最重要的是createProgam方法上的文档评论所说的内容。程序是代表编译单元的“SourceFile”和“CompilerOptions”的不可变集合。 createProgam方法作为第一个参数需要字符串列表,这些字符串是该程序中使用的文件的虚拟名称。
如果您不理解前两个理论段落,我认为示例中的注释会对您有所帮助。
// 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));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1066 次 |
| 最近记录: |