Using the TypeScript language service to parse source files

Topics: General
Mar 25, 2013 at 5:48 PM
Edited Mar 25, 2013 at 5:55 PM
I am looking for some direction on using the TypeScript language service to parse TypeScript source files. I have browsed the source, however I am hoping someone can point me in the right direction for what I am trying to accomplish...

Ideally what I would like to accomplish is the following: pass a TypeScript file to be parsed and have a data structure returned that allows me to access the body of a method, either as a string or as a collection of tokens I can use to reconstruct the source.

I have a C# to TypeScript code generator which compiles C# interfaces and classes to TypeScript, however it leaves the body of methods empty. If a user implements a method in an automatically generated output file I want to be able to preserve that implementation and put it back into the newly generated output file instead of the default empty implementation.

I would prefer to use the TypeScript language service to do this instead of generating my own parser with a tool like ANTLR however I am not finding much in terms of documentation or examples.
Mar 25, 2013 at 6:00 PM
I do not understand your use case (specifically the last sentence of the third paragraph. I have not used it from the language service, but I have programmatically parsed, type-checked, and emitted code using the TypeScript compiler. It's really easy, just dig into the source and see what TypeScript is doing. Basically construct a new TypeScript.TypeScriptCompiler (need to give error info and some settings), add source units (including lib.d.ts) which are parsed on add, then just call typeCheck (unless you don't want type checking). The compiler has a scripts property with all the scripts as a complete AST complete w/ type info.
Mar 25, 2013 at 6:18 PM
Edited Mar 25, 2013 at 6:20 PM
Prehaps that wasn't as clear as I could of made it, let me try and explain in more details. When my code generator converts a C# class to a TypeScript class it converts the following C#:
public class ExampleClass {
    public string ExampleMethod(int arg1) {
        return arg1.ToString()
    }
}
Into the following TypeScript:
class ExampleClass {
    ExampleMethod(arg1: number) : string {
       return "";
    }
}
When it generates the TypeScript it automatically adds it to the target project in a file called ExampleClass.ts. However, if I edit the C# ExampleClass it will cause the TypeScript class to be regenerated.

It is at this point that I want to parse the TypeScript file to see if the user has added any code, and if they have I want to read it out of the file, and put it back into the newly generated TypeScript file.

Basically I want to be able to take the following TypeScript
class ExampleClass {
    ExampleMethod(arg1: number) : string {
       return "Hello World"
    }
}
And extract the following for ExampleMethod:
return "Hello World"
Otherwise when the file is regenerated I will lose my changes to the TypeScript implementation.
Mar 28, 2013 at 2:20 PM
Edited Mar 29, 2013 at 8:07 PM
After researching the options I've decided to parse the TypeScript myself in C#. I plan to generate a parser with ANTLR, this will be more work upfront but should give me more flexibility in the future.

I am working on a open source ANTLR grammar definition for TypeScript at https://github.com/davemckeown/TypeScriptParser