Skip to content

Commit 91088ff

Browse files
committed
Merge pull request #1472 from sharwell/fix-684
Fix behavior of SA1009 when preceded by a comment
2 parents 310da2f + 427a757 commit 91088ff

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1009UnitTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,40 @@ public void TestMethod2(
701701
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
702702
}
703703

704+
/// <summary>
705+
/// This is a regression test for DotNetAnalyzers/StyleCopAnalyzers#684:
706+
/// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/684
707+
/// </summary>
708+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
709+
[Fact]
710+
public async Task TestEmbeddedCommentAsync()
711+
{
712+
var testCode = @"
713+
public class TestClass
714+
{
715+
public void TestMethod()
716+
{
717+
System.Console.WriteLine(""{0}"", 1 /*text*/ );
718+
}
719+
}
720+
";
721+
var fixedCode = @"
722+
public class TestClass
723+
{
724+
public void TestMethod()
725+
{
726+
System.Console.WriteLine(""{0}"", 1 /*text*/);
727+
}
728+
}
729+
";
730+
731+
DiagnosticResult expected = this.CSharpDiagnostic().WithLocation(6, 52).WithArguments(" not", "preceded");
732+
733+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
734+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
735+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
736+
}
737+
704738
/// <inheritdoc/>
705739
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
706740
{

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1009ClosingParenthesisMustBeSpacedCorrectly.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
188188
if (precededBySpace)
189189
{
190190
// Closing parenthesis must{ not} be {preceded} by a space.
191-
var properties = TokenSpacingCodeFixProvider.RemovePreceding;
191+
var properties = token.IsFirstInLine()
192+
? TokenSpacingCodeFixProvider.RemovePreceding
193+
: TokenSpacingCodeFixProvider.RemoveImmediatePreceding;
192194
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "preceded"));
193195
}
194196

0 commit comments

Comments
 (0)