[英]Analyze javascript file using Typescript Compiler
请参阅: https : //github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API ,我想获取AST中的节点类型 。
因为它是使用tsconfig.json
编写的, tsconfig.json
存在tsconfig.json
所以它对angular
项目很成功。
当我尝试分析使用JavaScript编写的react app
const program = ts.createProgram({
rootNames: [fileName],
options: {
strict: true,
target: ts.ScriptTarget.ES2015,
allowJs: true,
checkJs: true
}
})
const typeChecker = program.getTypeChecker();
... ...
我得到: typechecker.getTypeAtLocation(node).getSymbol()
是undefined
。
我认为ts.createProgram
options
是错误的。
选项: moduleResolution: ts.ModuleResolutionKind.NodeJs
是关键。
const program = ts.createProgram({
rootNames: [file1],
options: {
strict: true,
target: ts.ScriptTarget.ES2015,
allowJs: true,
checkJs: true,
moduleResolution: ts.ModuleResolutionKind.NodeJs
}
})
以上适用于React JS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.