Skip to content

Commit f273830

Browse files
committed
Make SA1008 fix do not remove new line
1 parent 2df902e commit f273830

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/TokenSpacingCodeFixProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,10 @@ private static void UpdateReplaceMap(Dictionary<SyntaxToken, SyntaxToken> replac
239239

240240
case TokenSpacingProperties.ActionRemove:
241241
triviaList = token.TrailingTrivia.AddRange(nextToken.LeadingTrivia);
242+
bool preserveLayout = layout == TokenSpacingProperties.LayoutPreserve;
242243

243244
UpdateReplaceMap(replaceMap, token, t => t.WithTrailingTrivia());
244-
UpdateReplaceMap(replaceMap, nextToken, t => t.WithLeadingTrivia(triviaList.WithoutLeadingWhitespace(true)));
245+
UpdateReplaceMap(replaceMap, nextToken, t => t.WithLeadingTrivia(triviaList.WithoutLeadingWhitespace(endOfLineIsWhitespace: !preserveLayout)));
245246
break;
246247
}
247248

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1008UnitTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ public void TestMethod2 (
144144
int z)
145145
{
146146
}
147+
148+
// Opening parenthesis followed by space
149+
public void TestMethod3(
150+
int x,
151+
int y,
152+
int z)
153+
{
154+
}
147155
}
148156
}
149157
";
@@ -165,13 +173,22 @@ public void TestMethod2(
165173
int z)
166174
{
167175
}
176+
177+
// Opening parenthesis followed by space
178+
public void TestMethod3(
179+
int x,
180+
int y,
181+
int z)
182+
{
183+
}
168184
}
169185
}
170186
";
171187

172188
DiagnosticResult[] expectedDiagnostics =
173189
{
174190
Diagnostic(DescriptorNotPreceded).WithLocation(12, 33),
191+
Diagnostic(DescriptorNotFollowed).WithLocation(20, 32),
175192
};
176193

177194
await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedTestCode, CancellationToken.None).ConfigureAwait(false);
@@ -863,7 +880,8 @@ public TestClass(int x, int y)
863880
y);
864881
865882
// Opening parenthesis followed by space
866-
var s3 = new String('a',
883+
var s3 = new String(
884+
'a',
867885
x);
868886
}
869887
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1008OpeningParenthesisMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, Synt
262262

263263
if (token.IsFollowedByWhitespace())
264264
{
265-
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotFollowed, token.GetLocation(), TokenSpacingProperties.RemoveFollowing));
265+
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotFollowed, token.GetLocation(), TokenSpacingProperties.RemoveFollowingPreserveLayout));
266266
}
267267
}
268268
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/TokenSpacingProperties.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,11 @@ internal static class TokenSpacingProperties
6161
ImmutableDictionary<string, string>.Empty
6262
.SetItem(LocationKey, LocationFollowing)
6363
.SetItem(ActionKey, ActionRemove);
64+
65+
internal static ImmutableDictionary<string, string> RemoveFollowingPreserveLayout { get; } =
66+
ImmutableDictionary<string, string>.Empty
67+
.SetItem(LocationKey, LocationFollowing)
68+
.SetItem(ActionKey, ActionRemove)
69+
.SetItem(LayoutKey, LayoutPreserve);
6470
}
6571
}

0 commit comments

Comments
 (0)