Skip to content

Commit 7619dc1

Browse files
committed
Merge branch 'master' into patch-1
2 parents bae6513 + 42e577f commit 7619dc1

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Document
7575
var indentationSteps = DetermineIndentationSteps(indentationSettings, braceToken);
7676

7777
var previousToken = braceToken.GetPreviousToken();
78-
var nextToken = braceToken.GetNextToken();
7978

8079
if (IsAccessorWithSingleLineBlock(previousToken, braceToken))
8180
{
@@ -106,9 +105,13 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Document
106105
braceReplacementToken = braceReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationSettings, indentationSteps));
107106
}
108107

109-
// Check if we need to apply a fix after the brace
110-
// if a closing brace is followed by a semi-colon or closing paren, no fix is needed.
111-
if ((LocationHelpers.GetLineSpan(nextToken).StartLinePosition.Line == braceLine) &&
108+
// Check if we need to apply a fix after the brace. No fix is needed when:
109+
// - The closing brace is followed by a semi-colon or closing paren
110+
// - The closing brace is the last token in the file
111+
var nextToken = braceToken.GetNextToken();
112+
var nextTokenLine = nextToken.IsKind(SyntaxKind.None) ? -1 : LocationHelpers.GetLineSpan(nextToken).StartLinePosition.Line;
113+
114+
if ((nextTokenLine == braceLine) &&
112115
(!braceToken.IsKind(SyntaxKind.CloseBraceToken) || !IsValidFollowingToken(nextToken)))
113116
{
114117
var sharedTrivia = nextToken.LeadingTrivia.WithoutTrailingWhitespace();

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,33 @@ namespace InvalidNamespace6
129129
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
130130
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
131131
}
132+
133+
/// <summary>
134+
/// Verifies that an invalid namespace at the end of the source file will be handled correctly.
135+
/// </summary>
136+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
137+
[Fact]
138+
public async Task TestNamespaceInvalidAtEndOfFileAsync()
139+
{
140+
var testCode = @"
141+
namespace TestNamespace
142+
{
143+
using System; }";
144+
145+
var fixedTestCode = @"
146+
namespace TestNamespace
147+
{
148+
using System;
149+
}";
150+
151+
DiagnosticResult[] expectedDiagnostics =
152+
{
153+
this.CSharpDiagnostic().WithLocation(4, 17),
154+
};
155+
156+
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostics, CancellationToken.None).ConfigureAwait(false);
157+
await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
158+
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
159+
}
132160
}
133161
}

0 commit comments

Comments
 (0)