Skip to content

Commit 8a764ca

Browse files
committed
Made things work properly for the normal situation as well
1 parent dc007f4 commit 8a764ca

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1119CodeFixProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,17 @@ private static SyntaxNode GetReplacement(ParenthesizedExpressionSyntax oldNode)
6868
var trailingTrivia = oldNode.Expression.GetTrailingTrivia().AddRange(oldNode.CloseParenToken.GetAllTrivia());
6969

7070
// Workaround for Roslyn not handling elastic markers for directive trivia correctly.
71-
var leadingSeparator = (oldNode.Parent is DirectiveTriviaSyntax) ? SyntaxFactory.Space : SyntaxFactory.ElasticMarker;
71+
if (!leadingTrivia.Any())
72+
{
73+
var previousToken = oldNode.OpenParenToken.GetPreviousToken();
74+
if (TriviaHelper.IndexOfTrailingWhitespace(previousToken.TrailingTrivia) == -1)
75+
{
76+
leadingTrivia = SyntaxFactory.TriviaList(SyntaxFactory.Space);
77+
}
78+
}
7279

7380
return oldNode.Expression
74-
.WithLeadingTrivia(leadingTrivia.Any() ? leadingTrivia : SyntaxFactory.TriviaList(leadingSeparator))
81+
.WithLeadingTrivia(leadingTrivia)
7582
.WithTrailingTrivia(trailingTrivia.Any() ? trailingTrivia : SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker));
7683
}
7784

StyleCop.Analyzers/StyleCop.Analyzers.Test/MaintainabilityRules/SA1119UnitTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,9 @@ public static void Main(string[] args)
14181418
14191419
#endif
14201420
1421+
#if (DEBUG)
1422+
1423+
#endif
14211424
}
14221425
}
14231426
";
@@ -1431,6 +1434,9 @@ public static void Main(string[] args)
14311434
14321435
#endif
14331436
1437+
#if DEBUG
1438+
1439+
#endif
14341440
}
14351441
}
14361442
";
@@ -1439,6 +1445,9 @@ public static void Main(string[] args)
14391445
this.CSharpDiagnostic(DiagnosticId).WithLocation(6, 4),
14401446
this.CSharpDiagnostic(ParenthesesDiagnosticId).WithLocation(6, 4),
14411447
this.CSharpDiagnostic(ParenthesesDiagnosticId).WithLocation(6, 10),
1448+
this.CSharpDiagnostic(DiagnosticId).WithLocation(10, 5),
1449+
this.CSharpDiagnostic(ParenthesesDiagnosticId).WithLocation(10, 5),
1450+
this.CSharpDiagnostic(ParenthesesDiagnosticId).WithLocation(10, 11),
14421451
};
14431452

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

0 commit comments

Comments
 (0)