问题描述
我需要有关Typescript编译器理解的帮助。我想编写一个脚本来解析每个打字稿文件,寻找一个导入声明,并且如果使用barrel-file脚本的导入声明是否应该抛出有关它的消息。我深入研究并找到了解决方案:
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
}
});
}
问题:有没有办法检测从桶中的进口?
解决方法
我解决了这个问题。在github-repo中有更多详细信息:https://github.com/alxpsr/barrel-finder