Skip to content

Commit

Permalink
Merge pull request #3741 from bjornhellander/sa1008-null-exception
Browse files Browse the repository at this point in the history
Update SA1008 to not crash if there is no previous token
  • Loading branch information
sharwell committed Dec 19, 2023
2 parents fbcb53b + 6b7973e commit 497c50a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
{
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;
using Xunit;
Expand Down Expand Up @@ -81,5 +83,32 @@ void Method((int, int) c)

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("\n")]
[InlineData("\n ")]
[WorkItem(2354, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2354")]
public async Task TestDeconstructionInTopLevelProgramAsync(string prefix)
{
var testCode = $@"{prefix}{{|#0:(|}} var a, var b) = (1, 2);";
var fixedCode = $@"{prefix}(var a, var b) = (1, 2);";

await new CSharpTest()
{
TestState =
{
OutputKind = OutputKind.ConsoleApplication,
Sources = { testCode },
},
ExpectedDiagnostics =
{
// /0/Test0.cs(1,1): warning SA1008: Opening parenthesis should not be followed by a space.
Diagnostic(DescriptorNotFollowed).WithLocation(0),
},
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2158,5 +2158,22 @@ public void TestMethod()

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(2354, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2354")]
public async Task TestNoPreviousTokenAsync()
{
var testCode = "(";

var test = new CSharpTest()
{
TestCode = testCode,

// Compiler diagnostics differ between Roslyn versions. The main thing is that the analyzer doesn't throw an exception.
CompilerDiagnostics = CompilerDiagnostics.None,
};

await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
var leadingTriviaList = TriviaHelper.MergeTriviaLists(prevToken.TrailingTrivia, token.LeadingTrivia);

var isFirstOnLine = false;
if (prevToken.GetLineSpan().EndLinePosition.Line < token.GetLineSpan().StartLinePosition.Line)
if (prevToken.IsKind(SyntaxKind.None))
{
isFirstOnLine = true; // This means that it doesn't matter if there are spaces before or not
}
else if (prevToken.GetLineSpan().EndLinePosition.Line < token.GetLineSpan().StartLinePosition.Line)
{
var done = false;
for (var i = leadingTriviaList.Count - 1; !done && (i >= 0); i--)
Expand Down

0 comments on commit 497c50a

Please sign in to comment.