Implement subset-based ordering for naming rules#36366
Implement subset-based ordering for naming rules#36366sharwell merged 7 commits intodotnet:masterfrom
Conversation
…e of truth" Prior to this change, user settings could interact with .editorconfig in unknown ways to produce deviations in code style across a team which could not be controlled by simply updating .editorconfig. This change ensures that .editorconfig, when used, becomes the code style "source of truth" for affected documents.
...oreTest/EditorConfigStorageLocation/NamingStylePreferenceEditorConfigStorageLocationTests.cs
Outdated
Show resolved
Hide resolved
7a1a52a to
86eb4fa
Compare
src/Workspaces/Core/Portable/NamingStyles/EditorConfig/EditorConfigNamingStyleParser.cs
Outdated
Show resolved
Hide resolved
|
@sharwell Can you include in a commit message (or just this PR) the email you sent internally describing this and the cases you were seeing? It's really helpful to understand the change and I'd hate for other reviewers or people not on the email to not have that context. |
|
@jasonmalinowski added it |
|
@sharwell Thanks. While looking at the code (doing so right now and but haven't posted yet) I see it might be awesome to stick the comment too in the logic where all the ordering is in case if this ever gets detached from the PR. But otherwise awesome for putting it here because the "why" was otherwise missing. |
|
@jasonmalinowski Once the algorithm stabilizes I'll do that. I'm going to work with @madskristensen to implement an analyzer for the EditorConfig Language Service that detects cases where the ordering above is not the same as the order in which rules appear in the file, and offer to take action to correct it:
|
jasonmalinowski
left a comment
There was a problem hiding this comment.
Some general comments but it feels like there's a few tests missing:
- Tests asserting that we're case sensitive for rule names if rule names are differing only by case.
- A test that confirms that our choice of which rules go first is also correct. The OrderBy/ThenBy makes it clear enough in the code what order is but it seems that I could change that ordering in the code and no test would break. I'm not sure if it's worth having a case added where two rules are applied both with differing accessibility and modifiers, and the right one is still taken. Consider the common cases that got us here in the first place?
src/EditorFeatures/Test/EditorConfig/LegacyEditorConfigDocumentOptionsProviderTests.cs
Outdated
Show resolved
Hide resolved
src/Workspaces/Core/Portable/NamingStyles/EditorConfig/EditorConfigNamingStyleParser.cs
Show resolved
Hide resolved
src/Workspaces/Core/Portable/NamingStyles/EditorConfig/EditorConfigNamingStyleParser.cs
Show resolved
Hide resolved
src/Workspaces/Core/Portable/NamingStyles/EditorConfig/EditorConfigNamingStyleParser.cs
Outdated
Show resolved
Hide resolved
src/Workspaces/Core/Portable/NamingStyles/EditorConfig/EditorConfigNamingStyleParser.cs
Show resolved
Hide resolved
...oreTest/EditorConfigStorageLocation/NamingStylePreferenceEditorConfigStorageLocationTests.cs
Show resolved
Hide resolved
| [Theory] | ||
| [InlineData("a", "b", "a", "public", "public, private")] | ||
| [InlineData("b", "a", "a", "public, private", "public")] | ||
| [InlineData("b", "a", "b", "public", "public, private")] |
There was a problem hiding this comment.
Consider a comment that this and the next are verifying we aren't accidentally sensitive to textual names?
...oreTest/EditorConfigStorageLocation/NamingStylePreferenceEditorConfigStorageLocationTests.cs
Show resolved
Hide resolved
...oreTest/EditorConfigStorageLocation/NamingStylePreferenceEditorConfigStorageLocationTests.cs
Outdated
Show resolved
Hide resolved
...oreTest/EditorConfigStorageLocation/NamingStylePreferenceEditorConfigStorageLocationTests.cs
Outdated
Show resolved
Hide resolved
|
And otherwise this looks great modulo the nitpicks, but it seems those missing tests make me a bit worried to outright hit signoff. |
During modifier ordering, 'const' is explicitly treated as a further restriction on 'static' and 'readonly'.
|
@jasonmalinowski I'm going to file a follow-up issue to get more tests added |
fa5fb78 to
f0b12ff
Compare
|
🔗 See madskristensen/EditorConfigLanguage#71 for the new analyzer to help users avoid problems in this space |
|
@sharwell Do you need a follow-up review or I see you're still tweaking some things? |
|
@jasonmalinowski I'm going to try and find a way to add the name for ordering without changing the serialization everywhere. |
This reverts commit a2be1a4.
| return (result: combinedNamingStylePreferences, succeeded: true); | ||
| } | ||
|
|
||
| // no existing naming styles were passed so just return the set of styles that were parsed from editorconfig |
| internal static ModifierKind FromXElement(XElement modifierElement) | ||
| => new ModifierKind((ModifierKindEnum)Enum.Parse(typeof(ModifierKindEnum), modifierElement.Value)); | ||
|
|
||
| public override bool Equals(object obj) |
There was a problem hiding this comment.
This is just for the perf benefit of not relying on the slow struct equals?
| namingStyles.Add(namingStyle); | ||
| namingRules.Add(serializableNamingRule); | ||
|
|
||
| var ruleKey = (serializableNamingRule.SymbolSpecificationID, serializableNamingRule.NamingStyleID, serializableNamingRule.EnforcementLevel); |
There was a problem hiding this comment.
This feels like it needs a lot of comments.
| if (modifier.ModifierKindWrapper == SymbolSpecification.ModifierKindEnum.IsStatic | ||
| || modifier.ModifierKindWrapper == SymbolSpecification.ModifierKindEnum.IsReadOnly) | ||
| { | ||
| if (x.SymbolSpecification.RequiredModifierList.Any(x => x.ModifierKindWrapper == SymbolSpecification.ModifierKindEnum.IsConst)) |
There was a problem hiding this comment.
Is it easier just writing RequiredModifierList.Contains(new ModifierKind(ModifierKindEnum.Const)) or something? I'll admit it tripped me up that here we're doing an Any and below we're doing a Contains, but that's largely because this whole wrapping thing is...icky. (And not your fault.)
Fixes #36355
From my previous investigation:
This change implements the above ordering proposal, with a final fallback to rule name order to ensure the order is deterministic for any given configuration.
Update 13 June:
Based on an evaluation of .editorconfig files on GitHub with naming rules, the following changes were made to the ordering: