Skip to content

Commit b7da9b3

Browse files
committed
Update after CR
1 parent 73f5fb5 commit b7da9b3

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5353
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5454
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
5555

56-
var enumMemberDeclaration = (EnumMemberDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
56+
var enumMemberDeclaration = (EnumMemberDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
5757
var enumDeclaration = (EnumDeclarationSyntax)enumMemberDeclaration.Parent;
5858

5959
var memberIndex = enumDeclaration.Members.IndexOf(enumMemberDeclaration);
@@ -72,9 +72,11 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
7272
.Add(SyntaxFactory.CarriageReturnLineFeed);
7373

7474
// replace the trivia for the tokens
75-
var replacements = new Dictionary<SyntaxToken, SyntaxToken>();
76-
replacements[precedingSeparatorToken] = precedingSeparatorToken.WithTrailingTrivia(newTrailingTrivia);
77-
replacements[enumMemberDeclarationFirstToken] = enumMemberDeclarationFirstToken.WithLeadingTrivia(indentation);
75+
var replacements = new Dictionary<SyntaxToken, SyntaxToken>
76+
{
77+
[precedingSeparatorToken] = precedingSeparatorToken.WithTrailingTrivia(newTrailingTrivia),
78+
[enumMemberDeclarationFirstToken] = enumMemberDeclarationFirstToken.WithLeadingTrivia(indentation),
79+
};
7880

7981
var newSyntaxRoot = syntaxRoot.ReplaceTokens(replacements.Keys, (original, rewritten) => replacements[original]);
8082
var newDocument = document.WithSyntaxRoot(newSyntaxRoot.WithoutFormatting());

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1136UnitTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.ReadabilityRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis;
910
using Microsoft.CodeAnalysis.CodeFixes;
1011
using Microsoft.CodeAnalysis.Diagnostics;
1112
using StyleCop.Analyzers.ReadabilityRules;
@@ -142,6 +143,39 @@ public enum TestEnum
142143
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
143144
}
144145

146+
/// <summary>
147+
/// Verifies that an enum declaration without a block is handled correctly.
148+
/// </summary>
149+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
150+
[Fact]
151+
public async Task TestEnumWithoutBlockAsync()
152+
{
153+
var testCode = @"
154+
public enum TestEnum
155+
";
156+
157+
DiagnosticResult[] expected =
158+
{
159+
new DiagnosticResult
160+
{
161+
Id = "CS1513",
162+
Severity = DiagnosticSeverity.Error,
163+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 2, 21) },
164+
Message = "} expected",
165+
},
166+
167+
new DiagnosticResult
168+
{
169+
Id = "CS1514",
170+
Severity = DiagnosticSeverity.Error,
171+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 2, 21) },
172+
Message = "{ expected",
173+
},
174+
};
175+
176+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
177+
}
178+
145179
/// <inheritdoc/>
146180
protected override CodeFixProvider GetCSharpCodeFixProvider()
147181
{

0 commit comments

Comments
 (0)