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

macros (AST rewriters) #11536

Closed
zpdDG4gta8XKpMCd opened this issue Oct 11, 2016 · 3 comments
Closed

macros (AST rewriters) #11536

zpdDG4gta8XKpMCd opened this issue Oct 11, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Oct 11, 2016

situation:

  • sometimes i wish i could generate a piece of code based on some existing one, for example a construction function for an given interface

    interface MyConfig {
       name: string;
       values: number[];
    }
    function myConfigFrom(name: string, values: number[]) : MyConfig {
       return { name, values };
    }

problem: currently my options are

  • either write it by hands (tedious monotonous work)
  • put together a homemade code generator and run it as a pre-build step (lot of maintenance, non-standard)

solution:

  • allow AST rewrites via decorators

    @rewrite(addContructorFunction)
    interface MyConfig {
       name: string;
       values: number[];
    }
    function addContructorFunction(node: ts.Node): ts.Node[] {
       return [node, toConstructorFunction(node as ts.InterfaceDeclaration)];
    }
    function toConstructorFunction(node: ts.IntefaceDeclaration): ts.FunctionDeclaration {
       // fun stuff goes here
    }
@sandersn
Copy link
Member

This is a duplicate of #4892, although with a decorator mechanism instead. Can you add your proposal to that discussion instead?

@sandersn sandersn added the Duplicate An existing issue was already created label Oct 11, 2016
@zpdDG4gta8XKpMCd
Copy link
Author

ok

@sandersn
Copy link
Member

Also a little like #2900, except with that idea you'd still have to write your own code generator.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants