TypeScript-使用Compiler API时异步/等待(ts.forEachChild)

时间:2019-02-15 22:13:44

标签: typescript typescript-compiler-api

我正在使用TypeScript编译器API(https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API) 并且我希望能够在TypeScript仪表器周围进行某种异步/等待(或者只是一个Promise)。

在下面的代码中,这意味着init()函数可能应标记为“异步”,并且在调用init()之后的下一行;应该只在所有节点的迭代完成后执行。

关于如何实现此目标的任何想法? 谢谢大家!和快乐的TypeScripting。

import * as ts from "typescript";
const fileNames = ['filename1.ts', 'filename2.ts'];
const program = ts.createProgram(fileNames, {
    target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS
});

// TODO: make this function async:
// End result should be:
/*
    init();
    // the following line must execute only when iteration through all nodes is completed.
    console.log('init complete.');
*/
function init() {
    for (const sourceFile of program.getSourceFiles()) {
        ts.forEachChild(sourceFile, walkNode);
    }
}

function walkNode(node: ts.Node) {
    // some code here...
    if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
        ts.forEachChild(node, walkNode);
    }
    // some code here...
}

0 个答案:

没有答案