NeVeSpl / NTypewriter

File/code generator using Scriban text templates populated with C# code metadata from Roslyn API.
https://nevespl.github.io/NTypewriter/
MIT License
126 stars 24 forks source link

Remove last comma when writing function parameters #125

Closed bzuidgeest closed 1 month ago

bzuidgeest commented 1 month ago

I'm trying to render my signalR hub to a clientside typescript class.

I have for example this template code

{{~ for method in class.Methods }}
    {{ method.Name | String.ToCamelCase }}(
    {{- for parameter in method.Parameters }}
            {{- parameter.Name }}: {{ parameter.Type -}}, 
    {{- end -}}
    ) : {{ method.ReturnType | Type.ToTypeScriptType }} {
        .....
    }
{{ end }}

But with how I do the parameters I end up with one comma to many in the function parameters. for example

createAgent(agent: Agent,) : Promise {

        }

Maybe I can fix this with Array Each to create the parameters and then do Array Join..... But that feels convoluted.

Considering the goal of this language, I wonder if there might not be a better way to do this.

NeVeSpl commented 1 month ago

Have you seen examples? Array.Join is used to address this problem:

parameters = method | Action.Parameters | Parameters.ToTypeScript | Array.Join ", "; 

https://nevespl.github.io/NTypewriter/?exampleId=type01

bzuidgeest commented 1 month ago

I see what you mean in the extensions example. I don't use typescript that often, let alone this library, which is completely new to me. It's easy to miss something like that in a example without any comments. The intellisense clearly says "Action.Parameters" works on webapi action methods.... But apparently it isn't really that picky. Who knew?

So maybe not the greatest question, but your answer was very helpful to me.