首页 > 解决方案 > 如何以编程方式检测从桶文件(index.ts)的导入

问题描述

我需要帮助理解 Typescript 编译器。我想编写一个解析每个打字稿文件的脚本,寻找一个导入声明,如果使用桶文件脚本的导入声明应该抛出有关它的消息。我深入研究并找到了一些解决方案:

function main3() {
    // create a program instance, which is a collection of source files
    // in this case we only have one source file
    const filePath = path.resolve('./file.ts');
    const program = _ts.createProgram([filePath], {});
    const source = program.getSourceFile(filePath);
    // helper to give us Node string type given kind
    const syntaxToKind = (kind) => {
        return _ts.SyntaxKind[kind];
    };
    // visit each node in the root AST and log its kind
    _ts.forEachChild(source, node => {
        if (_ts.isImportDeclaration(node)) {
            console.log(syntaxToKind(node.kind)); // print ImportDeclaration        
        }
    });
}

问题:有没有办法检测从桶的进口?

标签: typescripttypescript-compiler-api

解决方案


我解决了这个问题。github-repo 中的更多详细信息:https ://github.com/alxpsr/barrel-finder


推荐阅读