是否有一个插件(例如 Gulp),我可以在其中解析 typescript 文件并读出单个部分(例如成员、函数、注释)等。
这样我就可以在 ./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.
是否有任何库或其他东西已经能够做到这一点?
最佳答案
Jed mao 有一个优秀的(但未被重视的)项目,它开放了 TypeScipt 编译器 API 以供 commonjs 使用: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它基本上只是用 module.exports = TypeScript;
tsc.js
用法
您基本上需要研究 TypeScript 是如何工作的。但是具有 public getMembers(): PullSymbol[];
的 PullTypeSymbol
似乎值得在 TypeScript 源代码中搜索 ( https://typescript.codeplex.com/ )
关于javascript - 获取文件中的 TypeScript 类并读取其中的单个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23955169/