Skip to content

Commit cb545a7

Browse files
committed
Merge pull request #1 from sharwell/pr-1852
Updates to SA1518 messaging
2 parents 07f546f + 203e698 commit cb545a7

7 files changed

Lines changed: 133 additions & 44 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: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace StyleCop.Analyzers.Test.LayoutRules
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
109
using Microsoft.CodeAnalysis;
1110
using Microsoft.CodeAnalysis.CodeFixes;
1211
using Microsoft.CodeAnalysis.Diagnostics;
@@ -16,7 +15,7 @@ namespace StyleCop.Analyzers.Test.LayoutRules
1615
using Xunit;
1716

1817
/// <summary>
19-
/// Unit tests for <see cref="SA1518CodeMustNotContainBlankLinesAtEndOfFile"/>.
18+
/// Unit tests for <see cref="SA1518UseLineEndingsCorrectlyAtEndOfFile"/>.
2019
/// </summary>
2120
public class SA1518UnitTests : CodeFixVerifier
2221
{
@@ -428,7 +427,7 @@ protected override string GetSettings()
428427
/// <inheritdoc/>
429428
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
430429
{
431-
yield return new SA1518CodeMustNotContainBlankLinesAtEndOfFile();
430+
yield return new SA1518UseLineEndingsCorrectlyAtEndOfFile();
432431
}
433432

434433
/// <inheritdoc/>
@@ -441,14 +440,16 @@ private DiagnosticDescriptor GetDescriptor(EndOfFileHandling? endOfFileHandling)
441440
{
442441
switch (endOfFileHandling)
443442
{
444-
case EndOfFileHandling.Require:
445-
return SA1518CodeMustNotContainBlankLinesAtEndOfFile.DescriptorForRequireSetting;
446-
case EndOfFileHandling.Omit:
447-
return SA1518CodeMustNotContainBlankLinesAtEndOfFile.DescriptorForOmitSetting;
448-
case EndOfFileHandling.Allow:
449-
case null:
450-
default:
451-
return SA1518CodeMustNotContainBlankLinesAtEndOfFile.DescriptorForAllowSetting;
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;
452453
}
453454
}
454455
}

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>

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1518CodeMustNotContainBlankLinesAtEndOfFile.cs renamed to StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1518UseLineEndingsCorrectlyAtEndOfFile.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,38 @@ namespace StyleCop.Analyzers.LayoutRules
2222
/// <para>A violation of this rule occurs when one or more blank lines are at the end of the file.</para>
2323
/// </remarks>
2424
[DiagnosticAnalyzer(LanguageNames.CSharp)]
25-
internal class SA1518CodeMustNotContainBlankLinesAtEndOfFile : DiagnosticAnalyzer
25+
internal class SA1518UseLineEndingsCorrectlyAtEndOfFile : DiagnosticAnalyzer
2626
{
2727
/// <summary>
28-
/// The ID for diagnostics produced by the <see cref="SA1518CodeMustNotContainBlankLinesAtEndOfFile"/> analyzer.
28+
/// The ID for diagnostics produced by the <see cref="SA1518UseLineEndingsCorrectlyAtEndOfFile"/> analyzer.
2929
/// </summary>
3030
public const string DiagnosticId = "SA1518";
31+
private static readonly LocalizableString Title = new LocalizableResourceString(nameof(LayoutResources.SA1518Title), LayoutResources.ResourceManager, typeof(LayoutResources));
3132

32-
internal static readonly DiagnosticDescriptor DescriptorForAllowSetting =
33-
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForAllowSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForAllowSetting, HelpLink);
33+
private static readonly LocalizableString MessageFormatAllow = new LocalizableResourceString(nameof(LayoutResources.SA1518MessageFormatAllow), LayoutResources.ResourceManager, typeof(LayoutResources));
34+
private static readonly LocalizableString DescriptionAllow = new LocalizableResourceString(nameof(LayoutResources.SA1518DescriptionAllow), LayoutResources.ResourceManager, typeof(LayoutResources));
35+
private static readonly LocalizableString MessageFormatRequire = new LocalizableResourceString(nameof(LayoutResources.SA1518MessageFormatRequire), LayoutResources.ResourceManager, typeof(LayoutResources));
36+
private static readonly LocalizableString DescriptionRequire = new LocalizableResourceString(nameof(LayoutResources.SA1518DescriptionRequire), LayoutResources.ResourceManager, typeof(LayoutResources));
37+
private static readonly LocalizableString MessageFormatOmit = new LocalizableResourceString(nameof(LayoutResources.SA1518MessageFormatOmit), LayoutResources.ResourceManager, typeof(LayoutResources));
38+
private static readonly LocalizableString DescriptionOmit = new LocalizableResourceString(nameof(LayoutResources.SA1518DescriptionOmit), LayoutResources.ResourceManager, typeof(LayoutResources));
3439

35-
internal static readonly DiagnosticDescriptor DescriptorForRequireSetting =
36-
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForRequireSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForRequireSetting, HelpLink);
40+
private static readonly string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1518.md";
3741

38-
internal static readonly DiagnosticDescriptor DescriptorForOmitSetting =
39-
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatForOmitSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionForOmitSetting, HelpLink);
40-
41-
private const string Title = "Use line endings correctly at the end of the file";
42-
43-
private const string MessageFormatForAllowSetting = "Code must not contain blank lines at the end of the file";
44-
private const string DescriptionForAllowSetting = "Code must not contain blank lines at the end of the file";
45-
46-
private const string MessageFormatForRequireSetting = "Code is required to end with a single newline character";
47-
private const string DescriptionForRequireSetting = "Code is required to end with a single newline character";
42+
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
43+
private static readonly Action<SyntaxTreeAnalysisContext, StyleCopSettings> SyntaxTreeAction = HandleSyntaxTree;
4844

49-
private const string MessageFormatForOmitSetting = "Code may not end with a newline character";
50-
private const string DescriptionForOmitSetting = "Code may not end with a newline character";
45+
public static DiagnosticDescriptor DescriptorAllow { get; } =
46+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatAllow, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionAllow, HelpLink);
5147

52-
private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1518.md";
48+
public static DiagnosticDescriptor DescriptorRequire { get; } =
49+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatRequire, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionRequire, HelpLink);
5350

54-
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
55-
private static readonly Action<SyntaxTreeAnalysisContext, StyleCopSettings> SyntaxTreeAction = HandleSyntaxTree;
51+
public static DiagnosticDescriptor DescriptorOmit { get; } =
52+
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormatOmit, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, DescriptionOmit, HelpLink);
5653

5754
/// <inheritdoc/>
5855
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
59-
ImmutableArray.Create(DescriptorForAllowSetting);
56+
ImmutableArray.Create(DescriptorAllow);
6057

6158
/// <inheritdoc/>
6259
public override void Initialize(AnalysisContext context)
@@ -156,7 +153,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
156153
return;
157154
}
158155

159-
descriptorToReport = DescriptorForOmitSetting;
156+
descriptorToReport = DescriptorOmit;
160157
break;
161158

162159
case EndOfFileHandling.Require:
@@ -165,7 +162,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
165162
return;
166163
}
167164

168-
descriptorToReport = DescriptorForRequireSetting;
165+
descriptorToReport = DescriptorRequire;
169166
break;
170167

171168
case EndOfFileHandling.Allow:
@@ -180,7 +177,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
180177
}
181178
}
182179

183-
descriptorToReport = DescriptorForAllowSetting;
180+
descriptorToReport = DescriptorAllow;
184181
break;
185182
}
186183

StyleCop.Analyzers/StyleCop.Analyzers/StyleCop.Analyzers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
<Compile Include="LayoutRules\SA1515SingleLineCommentMustBePrecededByBlankLine.cs" />
161161
<Compile Include="LayoutRules\SA1516ElementsMustBeSeparatedByBlankLine.cs" />
162162
<Compile Include="LayoutRules\SA1517CodeMustNotContainBlankLinesAtStartOfFile.cs" />
163-
<Compile Include="LayoutRules\SA1518CodeMustNotContainBlankLinesAtEndOfFile.cs" />
163+
<Compile Include="LayoutRules\SA1518UseLineEndingsCorrectlyAtEndOfFile.cs" />
164164
<Compile Include="LayoutRules\SA1519BracesMustNotBeOmittedFromMultiLineChildStatement.cs" />
165165
<Compile Include="LayoutRules\SA1520UseBracesConsistently.cs" />
166166
<Compile Include="LinqHelpers\SyntaxTriviaListEnumerable.cs" />

documentation/SA1518.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<table>
44
<tr>
55
<td>TypeName</td>
6-
<td>SA1518CodeMustNotContainBlankLinesAtEndOfFile</td>
6+
<td>SA1518UseLineEndingsCorrectlyAtEndOfFile</td>
77
</tr>
88
<tr>
99
<td>CheckId</td>
@@ -17,17 +17,24 @@
1717

1818
## Cause
1919

20-
The code file has blank lines at the end.
20+
The line endings at the end of a file do not match the settings for the project.
2121

2222
## Rule description
2323

24-
To improve the layout of the code, StyleCop requires no blank lines at the end of files.
24+
To improve the layout of the code, StyleCop requires line endings to be consistent at the end of files. The specific
25+
requirements for a project may be configured using **stylecop.json**. See [Configuration.md](Configuration.md) for more
26+
information.
2527

26-
A violation of this rule occurs when one or more blank lines are at the end of the file.
28+
The specific settings is one of the following:
29+
30+
* Allow (default): Files are allowed to end with a single newline character, but it is not required
31+
* Require: Files are required to end with a single newline character
32+
* Omit: Files may not end with a newline character
2733

2834
## How to fix violations
2935

30-
To fix a violation of this rule, remove the blank lines from the end of the file.
36+
To fix a violation of this rule, update the line endings at the end of the file to match the settings for the current
37+
project.
3138

3239
## How to suppress violations
3340

0 commit comments

Comments
 (0)