Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript code generation from the Language Service #3241

Closed
ctaggart opened this issue May 21, 2015 · 11 comments
Closed

TypeScript code generation from the Language Service #3241

ctaggart opened this issue May 21, 2015 · 11 comments
Assignees
Labels
API Relates to the public API for TypeScript Committed The team has roadmapped this issue Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@ctaggart
Copy link
Contributor

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.

@danquirk danquirk added Suggestion An idea for TypeScript API Relates to the public API for TypeScript labels May 21, 2015
@mhegazy
Copy link
Contributor

mhegazy commented May 21, 2015

@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 beautify function that takes a string and returns a string beautify(input: string, options?: ts.FormatCodeOptions): string

@ctaggart
Copy link
Contributor Author

Also helpful here is @basarat 's Compiler Overview: http://basarat.gitbooks.io/typescript/content/docs/compiler/overview.html

@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.

ctaggart added a commit to ctaggart/TsAst that referenced this issue Nov 17, 2015
@ctaggart
Copy link
Contributor Author

I attempted this again given some hints from @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
Copy link
Contributor

I would not use the formatter. I would call ts.emitFiles with 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
Copy link
Contributor Author

ctaggart commented Dec 5, 2015

@CyrusNajmabadi, I'm taking another look at this today. It looks like ts.emitFiles emits JavaScript, not TypeScript.
https://github.com/ctaggart/TsAst/blob/65d8682e3ba70944b2b4960d587e1ee921b308eb/app.ts#L89-L96
image

Any idea what the right function might be for emitting TypesScript source files? I'm going to take a look at emitDeclarations in declarationEmitter.ts next.

@ctaggart
Copy link
Contributor Author

ctaggart commented Dec 6, 2015

The emitDeclarations did do what its name implies. It exported the declaration:
https://github.com/ctaggart/TsAst/blob/91571a3e679d7c1f29a86ed6754daffd02a0f48d/app.ts
image
Pretty cool, and probably useful in the future, but not what I'm looking for just yet. I just want to create or manipulate ts files.

@DanielRosenwasser
Copy link
Member

As a heads up @ctaggart, @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
Copy link
Contributor Author

ctaggart commented Dec 6, 2015

Thanks @DanielRosenwasser, really looking forward to what @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 output ts afterwards. Here, I was hoping to produce var a = 1;, but got var ;.

image
https://github.com/ctaggart/TsAst/blob/d6fcb49711defb7bd63c5fea83f62755b7c1afa8/app.ts#L28-L43

I'll see if I can make use of parseVariableStatement and the other parse functions. Clearly, I'm doing something wrong.

@ctaggart
Copy link
Contributor Author

ctaggart commented Dec 6, 2015

I'm giving up for now. @rbuckton, I'm interested in whatever you come up with.

@mhegazy mhegazy added the Committed The team has roadmapped this issue label Dec 9, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2015

this should be part of the work in #5595

@mhegazy
Copy link
Contributor

mhegazy commented Feb 22, 2016

closing in favor of #5595

@mhegazy mhegazy closed this as completed Feb 22, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 22, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Relates to the public API for TypeScript Committed The team has roadmapped this issue Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants