是否有一个插件(如 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.
是否有任何图书馆或其他东西已经能够做到这一点?
Jed mao 有一个优秀的(但未被重视)项目,它为 commonjs 的使用开放了 TypeScript 编译器 api :https://www.npmjs.org/package/typescript-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/typescript-api.d.ts
它里面的逻辑非常简单:https://github.com/jedmao/typescript-api/blob/master/wrapper.js#L15-L28 https://github.com/jedmao/typescript-api/blob/master/wrapper.js#L15-L28它基本上只是打开tsc.js
with a module.exports = TypeScript;
Usage
您基本上需要研究 TypeScript 是如何工作的。但PullTypeSymbol
其中有public getMembers(): PullSymbol[];
似乎在 TypeScript 源代码中值得搜索(https://typescript.codeplex.com/ https://typescript.codeplex.com/)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)