Encapsulate the TypeScript parser into a separate lib #46
Replies: 9 comments 3 replies
-
Hello, today i came across your project while searching GitHub for C# to Javascript converters that use Roslyn. I used Script# and Saltaralle for a long period in the past before switching to TypeScript. Now thinking about writing some source generator converting C# to Javascript similar to others but without the .NET corelib/runtime part, e.g. it will have a custom stdlib and almost one to one conversion from C# to JS (of course by only supporting a subset of .NET). It's just at idea phase for now. Anway that's not why i'm writing this. I saw this issue which indicates you are looking to convert TypeScript parser to C#. I was also looking to do that months ago but it seemed like a big task, then i found this: https://github.com/ToCSharp/TypeScriptAST and used a optimized / updated version of it successfully in my Serenity code generator (below) / source generator (that part is a private repo atm) which parses TypeScript files (not just .d.ts) and extract some metadata like user defined types etc. https://github.com/serenity-is/Serenity/tree/master/src/Serenity.Net.CodeGenerator/TypeScript TypeScriptAST was written a few years ago, so it might not be up to date with current TypeScript compiler but it can be a starting point for you if you are really considering this task. |
Beta Was this translation helpful? Give feedback.
-
This is an outdated library and has problems with some newer features when parsing the latest versions of I myself am currently using this library to parse As the TypeScript compiler is rapidly evolving, it might become very hard for a port like this to keep track of upstream changes. It might be more desirable to write a TypeScript analyzer in TypeScript using the official compiler code, and then run it in a Roslyn analyzer using a JavaScript runtime for .NET like jint. Generating an abstract syntax tree for TypeScript declarations is only one small part of the problem. The other part is analyzing the semantics of the declaration. While Roslyn provides a |
Beta Was this translation helpful? Give feedback.
-
If this project is still active, I wish to collaborate on this issue, at least for the front-end part that analyzes TypeScript code for For the backend I would need to tweak a bit for it to suit my JavaScript interop strategy (using |
Beta Was this translation helpful? Give feedback.
-
Hi @trungnt2910 - yes, very much active. I have a branch where I started work on the TypeScript AST, see https://github.com/IEvangelist/blazorators/tree/ast. This was a huge effort, and it's still very much up in the air. I love collaborating with others, happy for the help. I've been planning on updating this to use the newer |
Beta Was this translation helpful? Give feedback.
-
My project manages to use I am curious how you managed to create those hundreds of classes. Were they generated somehow or were they hand-written? Also is the intended work limited to porting Finally, please tell me if you know some documentation/guides on consuming the TypeScript compiler API from an outside program. I can find things like this but they mostly document the compiler internals and not how to consume those APIs like how one can use Roslyn. |
Beta Was this translation helpful? Give feedback.
-
Which classes specifically are you referring to? If you're talking about the AST branch, then yes, those are hand-written.
The intended work is to make a TypeScript type declaration parser capable of parsing raw |
Beta Was this translation helpful? Give feedback.
-
FYI, I just converted TypeScript Scanner and Parser from latest master of TypeScript repository: https://github.com/serenity-is/Serenity/tree/master/src/Serenity.Net.CodeGenerator/TypeScript It produces same set of tokens and nodes for about 6k compiler test cases in TS repository: Above test contains the code I used to generate test cases. |
Beta Was this translation helpful? Give feedback.
-
Well, the TypeScriptAST I used before worked well enough even though it was a conversion 7 years ago. The lexer and parser does not actually change that often. And they are relatively small compared to type checker, transformer parts etc |
Beta Was this translation helpful? Give feedback.
-
Here is the NuGet package: https://www.nuget.org/packages/Serenity.TypeScript And the new source link: https://github.com/serenity-is/Serenity/tree/master/src/Serenity.TypeScript |
Beta Was this translation helpful? Give feedback.
-
Create a new project named
TypeScript.SyntaxTreeParser
and manually convert the TypeScriptparser.ts
bits into a C# TypeScript parser (from the compiler itself).parser.ts
types.ts
This library will be capable of representing TypeScript type declarations as an abstract syntax tree.
Beta Was this translation helpful? Give feedback.
All reactions