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

v0.3.4 Introduced a Bug in Template Rendering #58

Closed Tristan10 closed 2 years ago

Tristan10 commented 2 years ago

Hello again,

After version 0.3.4 I started noticing some different functionality in my rendering than normal. An erroneous space is being added to my output.

Snippet of Template:

export interface {{ class.Name }} {{ class.HasBaseClass ? ("extends " + class.BaseClass.Name) : ""}} {
    [key: string]: any;
{{- for property in class.Properties | Symbols.ThatArePublic }}
    {{ property.Name }}: {{ property.Type | Custom.GetTypeScriptType }};
{{- end }}
}

Output: image

Notice the two spaces between "FutureCast" and "extends". Another weird behavior that I have noticed is that any time class.HasBaseClass resolves to true in my template, my generated model is checked out in source control even though there are no changes.

NeVeSpl commented 2 years ago

I am not able to reproduce that, it happens to all models or only for this one?

Tristan10 commented 2 years ago

It happens to all models that inherit another class. Any time this {{ class.HasBaseClass ? ("extends " + class.BaseClass.Name) : ""}} resolves true.

NeVeSpl commented 2 years ago

Are you able to reproduce this on a newly created project?

Tristan10 commented 2 years ago

Sorry, I wasn't able to get to this right away. Found out that removing the line above the snippet I included in my issue

{{ class.Properties | Custom.GetPropertyImport }}

was causing the issue. That custom function contained this code:

StringBuilder sb = new StringBuilder();

foreach (var type in allTypes)
{
    sb.AppendLine($"import {{ {type.Name} }} from './{type.Name}';");
}

return sb.ToString();

Just because of my suspicion updated it to be this instead:

List<string> lines = new List<string>();

foreach (var type in allTypes)
{
    lines.Add($"import {{ {type.Name} }} from './{type.Name}';");
}

return string.Join("\n", lines);

It seems to have solved the issue I was seeing. I really don't know why.