Skip to content

Commit

Permalink
Update SA1008 to not crash if there is no token before the analyzed o…
Browse files Browse the repository at this point in the history
…pening parenthesis

#2354
  • Loading branch information
bjornhellander committed Dec 19, 2023
1 parent be49652 commit 68c49ff
Show file tree
Hide file tree
Showing 3 changed files with 54 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,35 @@ 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}( var a, var b) = (1, 2);";
var fixedCode = $@"{prefix}(var a, var b) = (1, 2);";

var lineOffset = prefix.Count(x => x == '\n');
var columnOffset = prefix.Length - (prefix.LastIndexOf('\n') + 1);

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(1 + lineOffset, 1 + columnOffset),
},
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 68c49ff

Please sign in to comment.