SomeRanDev / reflaxe.CSharp

A remake of the Haxe/C# target written entirely within Haxe using Reflaxe.
MIT License
35 stars 2 forks source link

Enhancement Proposal - Support PascalCase for Namespaces #7

Closed AndrewDRX closed 8 months ago

AndrewDRX commented 8 months ago

Glad to see that there are efforts to still support C# as a Haxe target post version 5.0. It is already shaping up to be a better replacement than the original target.

One issue that I noticed is that namespace names are generated in the same case as the defined Haxe package name, but Microsoft recommends using PascalCase for namespace naming: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/identifier-names#naming-conventions

I'm planning to submit a pull request for adding an optional flag to support PascalCase for namespace names (leaving current behavior as default).

Something like changing src/cscompiler/components/CSType.hx from this

public function getNameSpace(baseType: BaseType):String {
    final pack = getPackWithoutModule(baseType);
    return pack.length > 0 ? pack.join(".") : CSCompiler.DEFAULT_ROOT_NAMESPACE;
}

To this:

public function getNameSpace(baseType: BaseType):String {
    final pack = getPackWithoutModule(baseType);
    if (pack.length == 0) {
        return CSCompiler.DEFAULT_ROOT_NAMESPACE;
    }
    var returnValue = pack.join(".");
    if (!Context.defined("ns-style")) {
        return returnValue;
    }
    switch (Context.definedValue("ns-style")) {
        case "pascal":
            returnValue = ~/[\._]\w/g.map(
                returnValue,
                (each) -> {
                    final part = each.matched(0);
                    final separator = part.charAt(0);
                    final character = part.charAt(1).toUpperCase();
                    return '${separator == "." ? separator : ""}${character}';
                }
            );
            returnValue = '${returnValue.charAt(0).toUpperCase()}${returnValue.substr(1)}';
    }
    return returnValue;
}

So, the optional flag would be like:

-D ns-style=pascal

Thoughts?

jeremyfa commented 8 months ago

I’m all for it and completely agree with the rationale, especially if that’s an option

SomeRanDev commented 8 months ago

It shall be done!

jeremyfa commented 8 months ago

You can make a PR with the proposed changes and we can continue from there

AndrewDRX commented 8 months ago

Excellent! This would account for the below scenarios.

Lower to Pascal via capitalizing the first letter of each dot part, keeping the dot: From myname.subname1.subname2.ClassName To Myname.Subname1.Subname2.ClassName

Camel to Pascal via same as above: From myName.subName1.subName2.ClassName To MyName.SubName1.SubName2.ClassName

Snake to Pascal via same as above plus capitalizing the first letter after each underscore, removing the underscore: From my_name.sub_name1.sub_name2.ClassName To MyName.SubName1.SubName2.ClassName

I'll do the pull request for review in a couple minutes.