Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SA1413 now also checks enum members. #2245

Merged
merged 2 commits into from
Dec 24, 2016
Merged

Conversation

dlemstra
Copy link
Member

@dlemstra dlemstra commented Dec 6, 2016

This patch implements the feature that was requests in #2239.

Fixes #2239

@codecov-io
Copy link

codecov-io commented Dec 6, 2016

Current coverage is 96.84% (diff: 100%)

Merging #2245 into master will increase coverage by <.01%

@@             master      #2245   diff @@
==========================================
  Files           598        598          
  Lines         84026      84059    +33   
  Methods        3581       3580     -1   
  Messages          0          0          
  Branches       3196       3197     +1   
==========================================
+ Hits          81369      81404    +35   
+ Misses         1790       1789     -1   
+ Partials        867        866     -1   

Powered by Codecov. Last update e179221...763906e

if (initializer.Members.Count() != commas.Count())
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, lastMember.GetLocation()));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code above should follow the same pattern as the methods below. Specifically you can check EnumDeclarationSyntax.Members.Count against EnumDeclarationSyntax.Members.SeparatorCount. That will work just as well and it will perform quite a bit better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. I was looking for something like this 👍

await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test cases where the element without comma is directly followed by an end of line comment. Like:

    var testCode = @"enum TestEnum
{
    One = 2 /* test comment */
}
";

and verify that it is properly changed to

    var testCode = @"enum TestEnum
{
    One = 2, /* test comment */
}
";

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed my unit test and it still passes

{
var member = (EnumMemberDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
return new TextChange(diagnostic.Location.SourceSpan, member.ToString() + ",");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 The code above should be the same pattern as the code for RewriteInitializer and RewriteAnonymousObjectInitializer.
If this code performs better than the existing code, then please also change the other methods to use the same pattern, for consistency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the code to only use my method and it still passes all the unit tests. Not sure how to test if it performs better but it does a lot less work: f983b5f#diff-9ac99be9ffc83f0bfb6aa1415e965bccR49.


private static TextChange GetEnumMemberTextChange(Diagnostic diagnostic, SyntaxNode syntaxRoot)
{
var member = (EnumMemberDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syntaxRoot.FindNode call has already been done in the calling method, please reuse the result of that, as this is usually a quite expensive call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment below.

@dlemstra
Copy link
Member Author

dlemstra commented Dec 8, 2016

Thanks for the quick review. Will apply the changes this weekend. (or earlier 😛 )

@sharwell sharwell merged commit 763906e into DotNetAnalyzers:master Dec 24, 2016
sharwell added a commit that referenced this pull request Dec 24, 2016
SA1413 now also checks enum members.
@sharwell sharwell added this to the 1.1.0 Beta 2 milestone Dec 24, 2016
@dlemstra dlemstra deleted the SA1413 branch December 24, 2016 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants