Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SA1008 to not crash if there is no previous token #3741

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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