Skip to content

Commit c032b21

Browse files
committed
Merge pull request #1852 from NikolayIT/master
Different messages for different values of NewlineAtEndOfFile added in SA1518CodeMustNotContainBlankLinesAtEndOfFile
2 parents 3e23b10 + cb545a7 commit c032b21

7 files changed

Lines changed: 160 additions & 34 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ namespace StyleCop.Analyzers.LayoutRules
1515
using Settings.ObjectModel;
1616

1717
/// <summary>
18-
/// Implements a code fix for <see cref="SA1518CodeMustNotContainBlankLinesAtEndOfFile"/>.
18+
/// Implements a code fix for <see cref="SA1518UseLineEndingsCorrectlyAtEndOfFile"/>.
1919
/// </summary>
2020
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1518CodeFixProvider))]
2121
[Shared]
2222
internal class SA1518CodeFixProvider : CodeFixProvider
2323
{
2424
/// <inheritdoc/>
2525
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
26-
ImmutableArray.Create(SA1518CodeMustNotContainBlankLinesAtEndOfFile.DiagnosticId);
26+
ImmutableArray.Create(SA1518UseLineEndingsCorrectlyAtEndOfFile.DiagnosticId);
2727

2828
/// <inheritdoc/>
2929
public override FixAllProvider GetFixAllProvider()

StyleCop.Analyzers/StyleCop.Analyzers.Test/LayoutRules/SA1518UnitTests.cs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis;
910
using Microsoft.CodeAnalysis.CodeFixes;
1011
using Microsoft.CodeAnalysis.Diagnostics;
1112
using StyleCop.Analyzers.LayoutRules;
@@ -14,7 +15,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
1415
using Xunit;
1516

1617
/// <summary>
17-
/// Unit tests for <see cref="SA1518CodeMustNotContainBlankLinesAtEndOfFile"/>.
18+
/// Unit tests for <see cref="SA1518UseLineEndingsCorrectlyAtEndOfFile"/>.
1819
/// </summary>
1920
public class SA1518UnitTests : CodeFixVerifier
2021
{
@@ -47,7 +48,7 @@ internal async Task TestWithBlankLinesAtEndOfFileAsync(EndOfFileHandling? newlin
4748
var testCode = BaseCode + "\r\n\r\n";
4849
var fixedCode = BaseCode + result;
4950

50-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
51+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
5152
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
5253
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
5354
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -71,7 +72,7 @@ internal async Task TestWithLineFeedOnlyBlankLinesAtEndOfFileAsync(EndOfFileHand
7172
var testCode = BaseCode + "\n\n";
7273
var fixedCode = BaseCode + result;
7374

74-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
75+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
7576
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
7677
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
7778
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -102,7 +103,7 @@ internal async Task TestWithSingleCarriageReturnLineFeedAtEndOfFileAsync(EndOfFi
102103
}
103104
else
104105
{
105-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
106+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
106107
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
107108
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
108109
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -134,7 +135,7 @@ internal async Task TestWithSingleLineFeedAtEndOfFileAsync(EndOfFileHandling? ne
134135
}
135136
else
136137
{
137-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
138+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
138139
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
139140
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
140141
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -166,7 +167,7 @@ internal async Task TestWithoutCarriageReturnLineFeedAtEndOfFileAsync(EndOfFileH
166167
}
167168
else
168169
{
169-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
170+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
170171
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
171172
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
172173
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -191,7 +192,7 @@ internal async Task TestFileEndsWithSpacesAsync(EndOfFileHandling? newlineAtEndO
191192
var testCode = BaseCode + "\r\n ";
192193
var fixedCode = BaseCode + expectedText;
193194

194-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
195+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
195196
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
196197
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
197198
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -222,7 +223,7 @@ internal async Task TestFileEndingWithCommentAsync(EndOfFileHandling? newlineAtE
222223
}
223224
else
224225
{
225-
var expected = this.CSharpDiagnostic().WithLocation(9, 16);
226+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(9, 16);
226227
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
227228
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
228229
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -247,7 +248,7 @@ internal async Task TestFileEndingWithCommentAndSpuriousWhitespaceAsync(EndOfFil
247248
var testCode = BaseCode + "\r\n// Test comment\r\n \r\n";
248249
var fixedCode = BaseCode + "\r\n// Test comment" + expectedText;
249250

250-
var expected = this.CSharpDiagnostic().WithLocation(9, 16);
251+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(9, 16);
251252
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
252253
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
253254
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -278,7 +279,7 @@ internal async Task TestFileEndingWithEndIfAsync(EndOfFileHandling? newlineAtEnd
278279
}
279280
else
280281
{
281-
var expected = this.CSharpDiagnostic().WithLocation(10, 7);
282+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);
282283
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
283284
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
284285
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -303,7 +304,7 @@ internal async Task TestFileEndingWithEndIfWithSpuriousWhitespaceAsync(EndOfFile
303304
var testCode = "#if true\r\n" + BaseCode + "\r\n#endif\r\n \r\n";
304305
var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;
305306

306-
var expected = this.CSharpDiagnostic().WithLocation(10, 7);
307+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);
307308
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
308309
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
309310
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -327,7 +328,7 @@ internal async Task TestCodeFixProviderStripsTrailingBlankLinesAsync(EndOfFileHa
327328
var testCode = BaseCode + "\r\n\r\n";
328329
var fixedCode = BaseCode + expectedText;
329330

330-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
331+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
331332
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
332333
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
333334
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -351,7 +352,7 @@ internal async Task TestCodeFixProviderStripsTrailingBlankLinesIncludingWhitespa
351352
var testCode = BaseCode + "\r\n \r\n \r\n";
352353
var fixedCode = BaseCode + expectedText;
353354

354-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
355+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
355356
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
356357
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
357358
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -375,7 +376,7 @@ internal async Task TestCodeFixProviderStripsTrailingLinefeedOnlyBlankLinesInclu
375376
var testCode = BaseCode + "\n \n \n";
376377
var fixedCode = BaseCode + expectedText;
377378

378-
var expected = this.CSharpDiagnostic().WithLocation(8, 2);
379+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(8, 2);
379380
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
380381
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
381382
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -399,7 +400,7 @@ internal async Task TestCodeFixProviderOnlyStripsTrailingBlankLinesAsync(EndOfFi
399400
var testCode = "#if true\r\n" + BaseCode + "\r\n#endif\r\n \r\n";
400401
var fixedCode = "#if true\r\n" + BaseCode + "\r\n#endif" + expectedText;
401402

402-
var expected = this.CSharpDiagnostic().WithLocation(10, 7);
403+
var expected = this.CSharpDiagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(10, 7);
403404
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
404405
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
405406
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
@@ -426,13 +427,30 @@ protected override string GetSettings()
426427
/// <inheritdoc/>
427428
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
428429
{
429-
yield return new SA1518CodeMustNotContainBlankLinesAtEndOfFile();
430+
yield return new SA1518UseLineEndingsCorrectlyAtEndOfFile();
430431
}
431432

432433
/// <inheritdoc/>
433434
protected override CodeFixProvider GetCSharpCodeFixProvider()
434435
{
435436
return new SA1518CodeFixProvider();
436437
}
438+
439+
private DiagnosticDescriptor GetDescriptor(EndOfFileHandling? endOfFileHandling)
440+
{
441+
switch (endOfFileHandling)
442+
{
443+
case EndOfFileHandling.Require:
444+
return SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorRequire;
445+
446+
case EndOfFileHandling.Omit:
447+
return SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorOmit;
448+
449+
case EndOfFileHandling.Allow:
450+
case null:
451+
default:
452+
return SA1518UseLineEndingsCorrectlyAtEndOfFile.DescriptorAllow;
453+
}
454+
}
437455
}
438456
}

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/LayoutResources.Designer.cs

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/LayoutResources.resx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,25 @@
186186
<data name="SA1518CodeFix" xml:space="preserve">
187187
<value>Fix whitespace at the end of the file</value>
188188
</data>
189+
<data name="SA1518DescriptionAllow" xml:space="preserve">
190+
<value>Code must not contain blank lines at the end of the file</value>
191+
</data>
192+
<data name="SA1518DescriptionOmit" xml:space="preserve">
193+
<value>File may not end with a newline character</value>
194+
</data>
195+
<data name="SA1518DescriptionRequire" xml:space="preserve">
196+
<value>File is required to end with a single newline character</value>
197+
</data>
198+
<data name="SA1518MessageFormatAllow" xml:space="preserve">
199+
<value>Code must not contain blank lines at the end of the file</value>
200+
</data>
201+
<data name="SA1518MessageFormatOmit" xml:space="preserve">
202+
<value>File may not end with a newline character</value>
203+
</data>
204+
<data name="SA1518MessageFormatRequire" xml:space="preserve">
205+
<value>File is required to end with a single newline character</value>
206+
</data>
207+
<data name="SA1518Title" xml:space="preserve">
208+
<value>Use line endings correctly at end of file</value>
209+
</data>
189210
</root>

0 commit comments

Comments
 (0)