Skip to content

Commit 5d9fdb9

Browse files
committed
Updated after feedback.
Added additional test case to improve coverage Removed unreachable code Fixed copy/paste typo
1 parent 13923ad commit 5d9fdb9

2 files changed

Lines changed: 55 additions & 11 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1500/SA1500UnitTests.Properties.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,5 +415,51 @@ public bool Property11
415415
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
416416
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
417417
}
418+
419+
/// <summary>
420+
/// Verifies that a single line accessor with an embedded block will be handled correctly.
421+
/// </summary>
422+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
423+
[Fact]
424+
public async Task TestSingleLineAccessorWithEmbeddedBlockAsync()
425+
{
426+
var testCode = @"
427+
public class TestClass
428+
{
429+
public int[] TestProperty
430+
{
431+
get {
432+
{
433+
return new[] { 1, 2, 3 }; } }
434+
}
435+
}
436+
";
437+
438+
var fixedTestCode = @"
439+
public class TestClass
440+
{
441+
public int[] TestProperty
442+
{
443+
get
444+
{
445+
{
446+
return new[] { 1, 2, 3 };
447+
}
448+
}
449+
}
450+
}
451+
";
452+
453+
DiagnosticResult[] expected =
454+
{
455+
this.CSharpDiagnostic().WithLocation(6, 13),
456+
this.CSharpDiagnostic().WithLocation(8, 43),
457+
this.CSharpDiagnostic().WithLocation(8, 45)
458+
};
459+
460+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
461+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
462+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
463+
}
418464
}
419465
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1500CodeFixProvider.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace StyleCop.Analyzers.LayoutRules
2626
internal class SA1500CodeFixProvider : CodeFixProvider
2727
{
2828
/// <inheritdoc/>
29-
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
29+
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
3030
ImmutableArray.Create(SA1500CurlyBracketsForMultiLineStatementsMustNotShareLine.DiagnosticId);
3131

3232
/// <inheritdoc/>
@@ -74,14 +74,11 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Document
7474

7575
if (IsAccessorWithSingleLineBlock(previousToken, braceToken))
7676
{
77-
if (!braceTokens.Contains(previousToken))
78-
{
79-
var newTrailingTrivia = previousToken.TrailingTrivia
80-
.WithoutTrailingWhitespace()
81-
.Add(SyntaxFactory.Space);
77+
var newTrailingTrivia = previousToken.TrailingTrivia
78+
.WithoutTrailingWhitespace()
79+
.Add(SyntaxFactory.Space);
8280

83-
AddReplacement(tokenReplacements, previousToken, previousToken.WithTrailingTrivia(newTrailingTrivia));
84-
}
81+
AddReplacement(tokenReplacements, previousToken, previousToken.WithTrailingTrivia(newTrailingTrivia));
8582

8683
braceReplacementToken = braceReplacementToken.WithLeadingTrivia(braceToken.LeadingTrivia.WithoutLeadingWhitespace());
8784
}
@@ -254,7 +251,7 @@ private class FixAll : DocumentBasedFixAllProvider
254251
new FixAll();
255252

256253
protected override string CodeActionTitle =>
257-
LayoutResources.SA1503CodeFix;
254+
LayoutResources.SA1500CodeFix;
258255

259256
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document)
260257
{
@@ -268,9 +265,10 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
268265

269266
var tokens = diagnostics
270267
.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start))
271-
.OrderBy(token => token.SpanStart);
268+
.OrderBy(token => token.SpanStart)
269+
.ToImmutableArray();
272270

273-
var tokenReplacements = GenerateBraceFixes(document, tokens.ToImmutableArray());
271+
var tokenReplacements = GenerateBraceFixes(document, tokens);
274272

275273
return syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
276274
}

0 commit comments

Comments
 (0)