AdaskoTheBeAsT / Typewriter

Automatic TypeScript template generation from C# source files
http://frhagn.github.io/Typewriter
Apache License 2.0
62 stars 12 forks source link

Support for public static readonly fields #38

Closed JBBianchi closed 7 months ago

JBBianchi commented 7 months ago

From https://github.com/frhagn/Typewriter/issues/205

I cannot get public static readonly fields to get picked up by TypeWriter.

models.tst

$Classes(c => c.Name == "SignalRNotifications" || c.Name == "SignalRChannels")[export class $Name {$Fields[
    static $name: string = '$Value';]}
]

C# class

public static class SignalRNotifications
    {
        public static readonly string ImportProgress = "ImportProgressUpdated";
        public static readonly string ImportCompleted = "ImportCompleted";
        ...
    }

Indeed, for some reason, the static fields are excluded in RoslynFieldMetadata:

public static IEnumerable<IFieldMetadata> FromFieldSymbols(IEnumerable<IFieldSymbol> symbols, Settings settings)
{
    return symbols
        .Where(
            s => s.DeclaredAccessibility == Accessibility.Public && !s.IsConst && !s.IsStatic)
        .Select(s => new RoslynFieldMetadata(s, settings));
}

It would be great to be able to opt-in for static members in the settings (and maybe also other more "in-depth" reflection settings like private/protected members ...)

JBBianchi commented 7 months ago

I wanted to try to contribute to the project but I can't get it to build in its default state (I had to remove StyleCop and SecurityCodeScan) and all the tests are failing (except one) because var document = _workspace.CurrentSolution.GetDocumentIdsWithFilePath(path).FirstOrDefault(); returns null.

AdaskoTheBeAsT commented 7 months ago

to build you need to have latest VS and also execute submodules-sync.bat

AdaskoTheBeAsT commented 7 months ago

and tests works on my machine ;) image

AdaskoTheBeAsT commented 7 months ago

https://github.com/AdaskoTheBeAsT/Typewriter/releases/tag/v2.20.0

AdaskoTheBeAsT commented 7 months ago

classes and records have StaticReadOnlyFields property which contains collection of such fields (with values)