Skip to content

Commit

Permalink
Merge pull request #1578 from vweijsters/fix-1534
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Sep 29, 2015
2 parents e6be9df + 5be315c commit 39b5c3f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using StyleCop.Analyzers.LayoutRules;
Expand Down Expand Up @@ -832,6 +833,57 @@ public int TestProperty
await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
}

/// <summary>
/// Verifies that an invalid syntax will not crash the analyzer.
/// This is a regression test for #1534
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task TestInvalidSyntaxAsync()
{
var testCode = @"namespace TestNamespace
{
public class TestClass /
{
}
}
";

DiagnosticResult[] expected =
{
new DiagnosticResult
{
Id = "CS1514",
Severity = DiagnosticSeverity.Error,
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
Message = "{ expected"
},
new DiagnosticResult
{
Id = "CS1513",
Severity = DiagnosticSeverity.Error,
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
Message = "} expected"
},
new DiagnosticResult
{
Id = "CS1022",
Severity = DiagnosticSeverity.Error,
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 3, 28) },
Message = "Type or namespace definition, or end-of-file expected"
},
new DiagnosticResult
{
Id = "CS1022",
Severity = DiagnosticSeverity.Error,
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 1) },
Message = "Type or namespace definition, or end-of-file expected"
}
};

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
}

/// <inheritdoc/>
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ private static void AnalyzeCloseBrace(SyntaxNodeAnalysisContext context, SyntaxT
var separatingTrivia = TriviaHelper.MergeTriviaLists(previousToken.TrailingTrivia, closeBraceToken.LeadingTrivia);

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

0 comments on commit 39b5c3f

Please sign in to comment.