是否有插件(如Gulp),我可以解析打字稿文件并读出单个部分(如成员,函数,注释)等。
所以我在这里有一个类似的./src/Foo.ts
:
class Foo {
static name: string = 'name';
/**
* This is a comment
*/
private bar: string;
/**
* This is also a comment
*/
public getBar(): string {
return this.bar;
}
}
我想在TypeScript或JavaScript(在gulp插件中)做的是读出这个文件,解析它然后可以访问这个类的成员。
伪代码:
var classTree = tsloader.parse('./src/Foo.ts');
var c = classTree.getClass(); // Returns a class object tree
for(var i = 0; i < c.getMembers().length; i++) {
c.getMembers()[i].getType(); // Returns the type
c.getMembers()[i].getComment(); // Returns the comment
// etc.
}
// etc.
是否有任何库或已经能够执行此操作的内容?
答案 0 :(得分:4)
Jed mao有一个很好的(但不被赞赏的)项目,它打开了用于共同消费的TypeScipt编译器api:https://www.npmjs.org/package/typescript-api
它附带定义文件:https://github.com/jedmao/typescript-api/blob/master/typescript-api.d.ts
其中的逻辑非常简单:https://github.com/jedmao/typescript-api/blob/master/wrapper.js#L15-L28它基本上只会打开tsc.js
module.exports = TypeScript;
您基本上需要调查TypeScript如何工作。但PullTypeSymbol
public getMembers(): PullSymbol[];
似乎值得搜索TypeScript源代码(https://typescript.codeplex.com/)