0

Is there a plugin (like for Gulp) where I can parse a typescript file and read out single parts (like a member, a function, comments), etc.

So that I have a class like this in let's say ./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;
    }
}

What I want to do in TypeScript or JavaScript (inside a gulp plugin) is to read out this file, parse it and then have access to the member of this class.

Pseudocode:

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.

Is there any library or something that is already capable of doing this?

Johannes Klauß
  • 10,676
  • 16
  • 68
  • 122

1 Answers1

4

Jed mao has an excellent (but under appreciated) project that open's up the TypeScipt compiler api for commonjs consumption : https://www.npmjs.org/package/typescript-api

It ships with a definition file : https://github.com/jedmao/typescript-api/blob/master/typescript-api.d.ts

The logic inside it is blatantly simple : https://github.com/jedmao/typescript-api/blob/master/wrapper.js#L15-L28 it bascially just opens up tsc.js with a module.exports = TypeScript;

Usage

You will basically need to investigate how TypeScript does its work. But PullTypeSymbol which has public getMembers(): PullSymbol[]; seems to be in the worth searching in the TypeScript Source code (https://typescript.codeplex.com/)

Community
  • 1
  • 1
basarat
  • 261,912
  • 58
  • 460
  • 511
  • Thanks for the kind words, @basarat. A huge complexity that you're going to run into is TypeScript files that import other TypeScript files. BTW, TypeScript is planning on releasing an API down the line. See https://typescript.codeplex.com/workitem/2078. For now, however, things are going to be difficult. – jedmao May 30 '14 at 15:45