-
Notifications
You must be signed in to change notification settings - Fork 510
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,6 +425,98 @@ void Foo() | |
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that the last value of an empty enum does not produce a diagnostic. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task VerifyEmptyEnumAsync() | ||
{ | ||
var testCode = @"enum EmptyEnum | ||
{ | ||
} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that the last value of an enum with a trailing comma does not produce a diagnostic. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task VerifyEnumWithTrailingCommaAsync() | ||
{ | ||
var testCode = @"enum TestEnum | ||
{ | ||
One, | ||
Two, | ||
} | ||
"; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that the last value of an enum without a trailing comma produces a diagnostic. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task VerifyEnumWithoutTrailingCommaAsync() | ||
{ | ||
var testCode = @"enum TestEnum | ||
{ | ||
One, | ||
Two | ||
} | ||
"; | ||
|
||
var fixedTestCode = @"enum TestEnum | ||
{ | ||
One, | ||
Two, | ||
} | ||
"; | ||
|
||
DiagnosticResult[] expected = | ||
{ | ||
this.CSharpDiagnostic().WithLocation(4, 5), | ||
}; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that the last value of an enum without a trailing comma produces a diagnostic. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task VerifyEnumWithValueWithoutTrailingCommaAsync() | ||
{ | ||
var testCode = @"enum TestEnum | ||
{ | ||
One = 2 /* test comment */ | ||
} | ||
"; | ||
|
||
var fixedTestCode = @"enum TestEnum | ||
{ | ||
One = 2, /* test comment */ | ||
} | ||
"; | ||
|
||
DiagnosticResult[] expected = | ||
{ | ||
this.CSharpDiagnostic().WithLocation(3, 5), | ||
}; | ||
|
||
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false); | ||
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */
}
"; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed my unit test and it still passes |
||
/// <inheritdoc/> | ||
protected override CodeFixProvider GetCSharpCodeFixProvider() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.MaintainabilityRules | |
{ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Helpers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
@@ -73,6 +74,22 @@ public override void Initialize(AnalysisContext context) | |
|
||
context.RegisterSyntaxNodeAction(HandleObjectInitializerAction, ObjectInitializerKinds); | ||
context.RegisterSyntaxNodeAction(HandleAnonymousObjectInitializerAction, SyntaxKind.AnonymousObjectCreationExpression); | ||
context.RegisterSyntaxNodeAction(HandleEnumMemberDeclarationAction, SyntaxKind.EnumDeclaration); | ||
} | ||
|
||
private static void HandleEnumMemberDeclarationAction(SyntaxNodeAnalysisContext context) | ||
{ | ||
var initializer = (EnumDeclarationSyntax)context.Node; | ||
var lastMember = initializer.Members.LastOrDefault(); | ||
if (lastMember == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (initializer.Members.Count() != initializer.Members.SeparatorCount) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create(Descriptor, lastMember.GetLocation())); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing that out. I was looking for something like this 👍 |
||
} | ||
|
||
private static void HandleObjectInitializer(SyntaxNodeAnalysisContext context) | ||
|
There was a problem hiding this comment.
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
andRewriteAnonymousObjectInitializer
.If this code performs better than the existing code, then please also change the other methods to use the same pattern, for consistency.
There was a problem hiding this comment.
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.