Repository navigation
TypeScript code generation from the Language Service #3241
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptDomain: APIRelates to the public API for TypeScriptRelates to the public API for TypeScript
on May 21, 2015 Cameron Taggart (@ctaggart) can you provide more details? you want some AST factory functions to allow you to generate AST on the fly?
A pretty printer is missing that consumes only the AST
It is not missing, it is just hidden :) see this example for how it can be done. We would be open to accept a PR to add a new
beautifyfunction that takes a string and returns a stringbeautify(input: string, options?: ts.FormatCodeOptions): stringctaggart commented
on Jul 16, 2015 ContributorAuthorMore actionsAlso helpful here is Basarat Ali Syed (@basarat) 's Compiler Overview: http://basarat.gitbooks.io/typescript/content/docs/compiler/overview.html
Mohamed Hegazy (@mhegazy) Yes, I want to generate AST on the fly and then be able to print/emit the generated TypeScript. I could use some help getting started. Any idea what the tests might look like for this.
- added a commit that references this issue
on Nov 17, 2015 ctaggart commented
on Nov 17, 2015 ContributorAuthorMore actionsI attempted this again given some hints from Cyrus Najmabadi (@CyrusNajmabadi) at the MVP Summit, but no luck. You can clone https://github.com/ctaggart/TsAst to see what I tried or just view the commit linked above. I would love a review and some help.
CyrusNajmabadi commented
on Nov 17, 2015 ContributorMore actionsI would not use the formatter. I would call
ts.emitFileswith your target source file. Note: you'll need to pass in an EmitResolver and EmitHost as well. EmitHost is very basic (and is just the way you provide the writer that the emitter will write into). EmitResolver is much more complex unfortunately. You'll probably want to just provide a dummy one that does almost nothing in all the functions (i.e. mostly returning 'false' or 'undefined').Hope that helps!
ctaggart commented
on Dec 5, 2015 ContributorAuthorMore actionsCyrus Najmabadi (@CyrusNajmabadi), I'm taking another look at this today. It looks like
ts.emitFilesemits JavaScript, not TypeScript.
https://github.com/ctaggart/TsAst/blob/65d8682e3ba70944b2b4960d587e1ee921b308eb/app.ts#L89-L96

Any idea what the right function might be for emitting TypesScript source files? I'm going to take a look at
emitDeclarationsindeclarationEmitter.tsnext.ctaggart commented
on Dec 6, 2015 ContributorAuthorMore actionsThe
emitDeclarationsdid do what its name implies. It exported the declaration:
https://github.com/ctaggart/TsAst/blob/91571a3e679d7c1f29a86ed6754daffd02a0f48d/app.ts

Pretty cool, and probably useful in the future, but not what I'm looking for just yet. I just want to create or manipulatetsfiles.DanielRosenwasser commented
on Dec 6, 2015 MemberMore actionsAs a heads up Cameron Taggart (@ctaggart), Ron Buckton (@rbuckton) is working on tree transformation infrastructure and a pretty printer. It's to help us with general codegen between emit targets, but we're hoping that down the line it can be used in the language service as well. That might be useful for your purposes, so we'll try to keep you posted. 😃
ctaggart commented
on Dec 6, 2015 ContributorAuthorMore actionsThanks Daniel Rosenwasser (@DanielRosenwasser), really looking forward to what Ron Buckton (@rbuckton) puts together!
Meanwhile, I'm still hacking my way though. My current thought is that if I can build up the AST from scratch and get it to output
js, may I will be able to get it to outputtsafterwards. Here, I was hoping to producevar a = 1;, but gotvar ;.
https://github.com/ctaggart/TsAst/blob/d6fcb49711defb7bd63c5fea83f62755b7c1afa8/app.ts#L28-L43I'll see if I can make use of
parseVariableStatementand the other parse functions. Clearly, I'm doing something wrong.ctaggart commented
on Dec 6, 2015 ContributorAuthorMore actionsI'm giving up for now. Ron Buckton (@rbuckton), I'm interested in whatever you come up with.
- addedCommittedThe team has roadmapped this issueThe team has roadmapped this issue
on Dec 9, 2015 this should be part of the work in #5595
closing in favor of #5595
- addedDuplicateAn existing issue was already createdAn existing issue was already created
on Feb 22, 2016 - locked and limited conversation to collaborators
on Jun 19, 2018
It would be great if we could generate TypeScript code using the Language Service. This is a feature request continuing on #1651. A pretty printer is missing that consumes only the AST. Like #3136, a use case would be generating TypeScript code from a Swagger/RAML/REST api that is well defined.