dotnet/roslyn

Enum Types and Enum Values should have different .editorconfig kinds

Open

#24,209 opened on Jan 12, 2018

View on GitHub
 (14 comments) (20 reactions) (0 assignees)C# (20,414 stars) (4,257 forks)batch import
Area-IDEhelp wanted

Description

Version Used: Not sure of Roslyn version, but using VS 15.5.3.

Description: In the .editorconfig naming conventions described in the docs, symbols can be defined with applicable_kinds equal to class, struct, enum, etc. From my own testing, it appears that enumerated types use the enum kind, while enumerated values use the const kind. This can be frustrating, as shown in the code below. I asked about this on StackOverflow a while back, and nobody had a legitimate solution, so I figured I'd raise an Issue. If this is not the correct repo for .NET .editorconfig settings, please direct me to the correct one.

Consider the following .editorconfig (the more complete file that I use is available here):

    # Require const fields to be all upper-case
    dotnet_naming_symbols.const_fields.applicable_accessibilities = *
    dotnet_naming_symbols.const_fields.required_modifiers = const
    dotnet_naming_rule.const_fields.symbols = const_fields
    dotnet_naming_rule.const_fields.style = all_upper
    dotnet_naming_rule.const_fields.severity = warning

    # Require enums to be Pascal case
    dotnet_naming_symbols.enums.applicable_kinds = enum
    dotnet_naming_rule.enums.symbols = enums
    dotnet_naming_rule.enums.style = pascal_case
    dotnet_naming_rule.enums.severity = warning

    #Naming styles
    dotnet_naming_style.all_upper.capitalization = all_upper
    dotnet_naming_style.pascal_case.capitalization = pascal_case

and the following C# code. The in-line comments represent the warnings shown after "Naming rule violation:" in Visual Studio:

    public class DerpClass {
        public const int DERP = 5;      // OKAY
        public const int Derp = 5;      // These words cannot contain lower case characters: Derp
    }
    public enum EnumLower {
        val                             // These words cannot contain lower case characters: val
    }
    public enum EnumPascal {
        Val                             // These words cannot contain lower case characters: Val
    }
    public enum EnumUpper {
        VAL                             // OKAY
    }
    public enum derpenum {              // These words must begin with upper case characters: derpenum
        VAL
    }

As you can see, the enums naming rule applies only to the name of enum types, not to their values. Instead, enum values share the const_fields naming rule with const fields. This is frustrating, as I either have to make my enumerated values all-upper-case or make my const fields Pascal-case, neither of which follows typical naming conventions for a C-based language. But it's either that or deal with a million Naming rule violation warnings throughout my codebase (or Messages, if I change severity).

Since the enum applicable_kind value is clearly meant for enum type names, similar to the class and struct kinds, it would be great if there were another applicable_kind for enumerated values, say enum_value. That way, enum types, enum values, and const fields could all have their own naming styles. If the Roslyn compiler really treats enum values as consts then I could see this being difficult to implement, but I know nothing of compiler design so hopefully it can be done!

Contributor guide