Skip to content

macros (AST rewriters) #11536

Closed
Closed

Description

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
    }

Activity

  1. sandersn commented on Oct 11, 2016

    @sandersn
    Member

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

  2. zpdDG4gta8XKpMCd commented on Oct 11, 2016

    @zpdDG4gta8XKpMCd
    Author

    ok

  3. sandersn commented on Oct 11, 2016

    @sandersn
    Member

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

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

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions