Skip to content

Commit 60911ef

Browse files
committed
Merge pull request #1840 from sharwell/fix-1834
Fix SA1009 behavior inside trivia
2 parents 0cf6026 + 44fd5d6 commit 60911ef

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@ public void Method()
6060
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
6161
}
6262

63+
[Fact]
64+
public async Task TestDocumentationMethodReferenceWithWhitespaceBeforeClosingParenthesisAsync()
65+
{
66+
const string testCode = @"
67+
public class Foo
68+
{
69+
/// <see cref=""Method( )""/>
70+
public void Method()
71+
{
72+
}
73+
}";
74+
const string fixedCode = @"
75+
public class Foo
76+
{
77+
/// <see cref=""Method()""/>
78+
public void Method()
79+
{
80+
}
81+
}";
82+
83+
DiagnosticResult expected = this.CSharpDiagnostic().WithArguments(" not", "preceded").WithLocation(4, 28);
84+
85+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
86+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
87+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
88+
}
89+
6390
[Fact]
6491
public async Task TestMethodWith2CorrectlySpacedParametersAsync()
6592
{
@@ -667,6 +694,13 @@ public void TestMethod2(
667694
}
668695
669696
public int TestMethod3(int a) { return a; }
697+
698+
public int TestMethod4(string[] args)
699+
{
700+
#if !(X || NOT )
701+
return 1;
702+
#endif
703+
}
670704
}
671705
";
672706

@@ -698,6 +732,13 @@ public void TestMethod2(
698732
}
699733
700734
public int TestMethod3(int a) { return a; }
735+
736+
public int TestMethod4(string[] args)
737+
{
738+
#if !(X || NOT)
739+
return 1;
740+
#endif
741+
}
701742
}
702743
";
703744

@@ -706,7 +747,8 @@ public void TestMethod2(
706747
this.CSharpDiagnostic().WithLocation(10, 9).WithArguments(" not", "preceded"),
707748
this.CSharpDiagnostic().WithLocation(16, 7).WithArguments(" not", "preceded"),
708749
this.CSharpDiagnostic().WithLocation(21, 19).WithArguments(" not", "preceded"),
709-
this.CSharpDiagnostic().WithLocation(25, 17).WithArguments(" not", "preceded")
750+
this.CSharpDiagnostic().WithLocation(25, 17).WithArguments(" not", "preceded"),
751+
this.CSharpDiagnostic().WithLocation(32, 16).WithArguments(" not", "preceded"),
710752
};
711753

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
6363
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
6464
{
6565
SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
66-
foreach (var token in root.DescendantTokens())
66+
foreach (var token in root.DescendantTokens(descendIntoTrivia: true))
6767
{
6868
if (token.IsKind(SyntaxKind.CloseParenToken))
6969
{
@@ -96,6 +96,7 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
9696
case SyntaxKind.CloseBracketToken:
9797
case SyntaxKind.SemicolonToken:
9898
case SyntaxKind.CommaToken:
99+
case SyntaxKind.DoubleQuoteToken:
99100
precedesStickyCharacter = true;
100101
break;
101102

0 commit comments

Comments
 (0)