NeVeSpl / NTypewriter

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

Restrict custom functions to be defined only in public static classes. #82

Closed schabannes closed 1 year ago

schabannes commented 1 year ago

This fixes a couple of issues/limitations of the .nt.cs files:

Note: the VS plugin need to be updated to apply the same restriction on code completion.

NeVeSpl commented 1 year ago

But let's start from the beginning, what problems are you trying to solve here?

I see only one, Scriban does not work with custom functions that are generic, am I missing something else?

schabannes commented 1 year ago

Yes, that is correct it's all related to generic class that will break the compilation of the .nt.cs files. The fix is the extra where clause '!x.ContainsGenericParameters', the second condition (public+static) avoid exposing non Scriban custom function (e.g. extension methods). However it will be a breaking change (quite small to fix, but still breaking). We could put that behind a feature switch (adding one extra settings to the GlobalConfig) or remove it.

I don't know the impact on your VS plugin though.

NeVeSpl commented 1 year ago

So basically you want to fix problems that no one has, by introducing breaking changes to every existing *.nt.cs. file. :D

I hope that it is very clear how bad that idea is.

From my perspective adding information to documentation about generics would be enough.

I would consider adding emitting some warnings/errors if someone would use generic class/methods, but this is so a rare use case that I am not sure it is worth doing it.