我试图在编译器API之后构建一个代码分析工具。
现在,下面的应用程序可以打印出p,Person,age,walk。
但是如何认识Person是接口,walk是函数等呢?谢谢
// app.ts
import * as ts from 'typescript';
const userAppFile = './user-app.ts';
const apiFile = './api.d.ts';
const program = ts.createProgram([userAppFile, apiFile], ts.getDefaultCompilerOptions());
const checker = program.getTypeChecker();
const sourceFile = program.getSourceFile(userAppFile);
function printApi(node) {
const symbol = checker.getSymbolAtLocation(node);
if (symbol) console.log(symbol.name);
ts.forEachChild(node, printApi);
}
printApi(sourceFile);// api.d.ts
interface Person {
age: number;
}
declare function walk(speed: number): void;// user-app.ts
const p: Person = { age: 10 };
walk(3);发布于 2017-06-07 21:14:23
你检查符号上的标志。
即:
if(symbol.flags & ts.SymbolFlags.Class) {
// this symbol is a class
} else if (symbol.flags & ts.SymbolFlags.Interface) {
// this is an interface
}https://stackoverflow.com/questions/44421735
复制相似问题