Skip to content

Commit 5be315c

Browse files
committed
Made SA1508 implementation more robust
1 parent 4e5fe43 commit 5be315c

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1508UnitTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
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.LayoutRules;
@@ -832,6 +833,60 @@ public int TestProperty
832833
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
833834
}
834835

836+
/// <summary>
837+
/// Verifies that an invalid syntax will not crash the analyzer.
838+
/// This is a regression test for #1534
839+
/// </summary>
840+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
841+
[Fact]
842+
public async Task TestInvalidSyntaxAsync()
843+
{
844+
var testCode = @"namespace TestNamespace
845+
{
846+
public class TestClass /
847+
{
848+
}
849+
}
850+
";
851+
852+
DiagnosticResult[] expected =
853+
{
854+
new DiagnosticResult
855+
{
856+
Id = "CS1514",
857+
Severity = DiagnosticSeverity.Error,
858+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
859+
Message = "{ expected"
860+
},
861+
862+
new DiagnosticResult
863+
{
864+
Id = "CS1513",
865+
Severity = DiagnosticSeverity.Error,
866+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
867+
Message = "} expected"
868+
},
869+
870+
new DiagnosticResult
871+
{
872+
Id = "CS1022",
873+
Severity = DiagnosticSeverity.Error,
874+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
875+
Message = "Type or namespace definition, or end-of-file expected"
876+
},
877+
878+
new DiagnosticResult
879+
{
880+
Id = "CS1022",
881+
Severity = DiagnosticSeverity.Error,
882+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 1) },
883+
Message = "Type or namespace definition, or end-of-file expected"
884+
}
885+
};
886+
887+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
888+
}
889+
835890
/// <inheritdoc/>
836891
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
837892
{

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1508ClosingCurlyBracketsMustNotBePrecededByBlankLine.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ private static void AnalyzeCloseBrace(SyntaxNodeAnalysisContext context, SyntaxT
129129
var separatingTrivia = TriviaHelper.MergeTriviaLists(previousToken.TrailingTrivia, closeBraceToken.LeadingTrivia);
130130

131131
// skip all leading whitespace for the close brace
132+
// the index must be checked because two tokens can be more than two lines apart and
133+
// still only be separated by whitespace trivia due to compilation errors
132134
var index = separatingTrivia.Count - 1;
133-
while (separatingTrivia[index].IsKind(SyntaxKind.WhitespaceTrivia))
135+
while (index >= 0 && separatingTrivia[index].IsKind(SyntaxKind.WhitespaceTrivia))
134136
{
135137
index--;
136138
}

0 commit comments

Comments
 (0)