在文件中获取TypeScript类并读取其中的单个部分

时间:2014-05-30 12:55:38

标签: javascript parsing typescript

是否有插件(如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.

是否有任何库或已经能够执行此操作的内容?

1 个答案:

答案 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/