Skip to content

TypeScript code generation from the Language Service #3241

Description

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.

Activity

  1. mhegazy commented on May 21, 2015

    @mhegazy
    Contributor

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

  2. ctaggart commented on Jul 16, 2015

    @ctaggart
    ContributorAuthor

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

  3. ctaggart commented on Nov 17, 2015

    @ctaggart
    ContributorAuthor

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

  4. CyrusNajmabadi commented on Nov 17, 2015

    @CyrusNajmabadi
    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!

  5. ctaggart commented on Dec 5, 2015

    @ctaggart
    ContributorAuthor

    Cyrus Najmabadi (@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.

  6. ctaggart commented on Dec 6, 2015

    @ctaggart
    ContributorAuthor

    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.

  7. DanielRosenwasser commented on Dec 6, 2015

    @DanielRosenwasser
    Member

    As 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. 😃

  8. ctaggart commented on Dec 6, 2015

    @ctaggart
    ContributorAuthor

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

  9. ctaggart commented on Dec 6, 2015

    @ctaggart
    ContributorAuthor

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

  10. mhegazy commented on Dec 9, 2015

    @mhegazy
    Contributor

    this should be part of the work in #5595

  11. mhegazy commented on Feb 22, 2016

    @mhegazy
    Contributor

    closing in favor of #5595

  12. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

CommittedThe team has roadmapped this issueDomain: APIRelates to the public API for TypeScriptDuplicateAn existing issue was already createdSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions