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

Add another CompositeGeneratorNode#indent overload #1295

Open
tomsontom opened this issue Nov 18, 2023 · 1 comment
Open

Add another CompositeGeneratorNode#indent overload #1295

tomsontom opened this issue Nov 18, 2023 · 1 comment

Comments

@tomsontom
Copy link
Contributor

tomsontom commented Nov 18, 2023

Proposal

I'd like to propse to add another overload method

indent<T>(children: ((indented: IndentNode, value: T) => void), value: T | T[]): this;

allowing me to directly pass a function instead of writing a closure.

Reason

While writing my code generator I found that I could have written more consise code if could directly pass a function to indent just to pass on a value/list of values to operate on.

Original:

function generateType(node: IndentNode, type: Type) {
    node.append(`class ${type.name} {`, NL)
    node.indent(content => {
        type.methods.forEach( m => generateMethod(content, m) )
    } )
    node.append('}')
}

function generateMethod(node: IndentNode, method: Method) {
    node.append(`public ${method.name}(): ${method.returnType} {`, NL)
    node.indent(content => generateMethodBody(content, method))
    node.append('}')
}

function generateMethodBody(node: IndentNode, method: Method) {
    // ...
}

With API addition:

function generateType(node: IndentNode, type: Type) {
    node.append(`class ${type.name} {`, NL)
    node.indent(generateMethod, type.methods)
    node.append('}')
}

function generateMethod(node: IndentNode, method: Method) {
    node.append(`public ${method.name}(): ${method.returnType} {`, NL)
    node.indent(generateMethodBody, method)
    node.append('}')
}

function generateMethodBody(node: IndentNode, method: Method) {
    // ...
}

If you think this API makes sense then I would implement and provide a PR.

@sailingKieler
Copy link
Contributor

Hi Tom,
thanks for this idea.

Instead of sticking to the classic append-based API:
Did you give the expandToNode-based approach a try? That way you can write

function generateMethod(node: IndentNode, method: Method) {
    expandToNode`
        public ${method.name}(): ${method.returnType} {
            ${generateMethodBody(method)}
        }
    `;
}

provided

function generateMethodBody(method: Method): Generated {
    return new ComponentGeneratorNode()....
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants