typescript - 如何以编程方式检测从桶文件(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
}
});
}
问题:有没有办法检测从桶的进口?
解决方案
我解决了这个问题。github-repo 中的更多详细信息:https ://github.com/alxpsr/barrel-finder
推荐阅读
- c# - 赢表格。线程和递归
- python - 具有大量选择最佳实践的 MultipleChoiceField
- javascript - 为什么内部方法的内部最后执行?
- c# - 当只有一件事可以返回时,还有什么可以返回?
- dialogflow-es - 在 Dialoglflow 中设置时,我不知道上下文中的参数将持续多长时间
- java - JavaFX - 刷新图表
- python - 如何使我的 Tkinter EXE 文本编辑器应用程序成为某些扩展的默认应用程序
- node.js - 在 Node.js 应用程序中保持开放连接
- javascript - 我如何使用 $.post() 发送表单数据?
- php - 更改图像或使用 php 表单保留旧图像