Skip to content

Commit 3525c16

Browse files
committed
Different messages for different values of NewlineAtEndOfFile added in SA1518CodeMustNotContainBlankLinesAtEndOfFile
1 parent b694579 commit 3525c16

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1518CodeMustNotContainBlankLinesAtEndOfFile.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,30 @@ internal class SA1518CodeMustNotContainBlankLinesAtEndOfFile : DiagnosticAnalyze
2828
/// The ID for diagnostics produced by the <see cref="SA1518CodeMustNotContainBlankLinesAtEndOfFile"/> analyzer.
2929
/// </summary>
3030
public const string DiagnosticId = "SA1518";
31-
private const string Title = "Code must not contain blank lines at end of file";
32-
private const string MessageFormat = "Code must not contain blank lines at end of file";
33-
private const string Description = "The code file has blank lines at the end.";
31+
private const string TitleForAllowSetting = "Code must not contain blank lines at end of file";
32+
private const string MessageFormatForAllowSetting = "Code must not contain blank lines at end of file";
33+
private const string TitleForRequireSetting = "Code is required to end with a single newline character";
34+
private const string MessageFormatForRequireSetting = "Code is required to end with a single newline character";
35+
private const string TitleForOmitSetting = "Code may not end with a newline character";
36+
private const string MessageFormatForOmitSetting = "Code may not end with a newline character";
37+
private const string Description = "Fix blank lines at the end of the file.";
3438
private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1518.md";
3539

36-
private static readonly DiagnosticDescriptor Descriptor =
37-
new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
40+
private static readonly DiagnosticDescriptor DescriptorForAllowSetting =
41+
new DiagnosticDescriptor(DiagnosticId, TitleForAllowSetting, MessageFormatForAllowSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
42+
43+
private static readonly DiagnosticDescriptor DescriptorForRequireSetting =
44+
new DiagnosticDescriptor(DiagnosticId, TitleForRequireSetting, MessageFormatForRequireSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
45+
46+
private static readonly DiagnosticDescriptor DescriptorForOmitSetting =
47+
new DiagnosticDescriptor(DiagnosticId, TitleForOmitSetting, MessageFormatForOmitSetting, AnalyzerCategory.LayoutRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink);
3848

3949
private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart;
4050
private static readonly Action<SyntaxTreeAnalysisContext, StyleCopSettings> SyntaxTreeAction = HandleSyntaxTree;
4151

4252
/// <inheritdoc/>
4353
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
44-
ImmutableArray.Create(Descriptor);
54+
ImmutableArray.Create(DescriptorForAllowSetting);
4555

4656
/// <inheritdoc/>
4757
public override void Initialize(AnalysisContext context)
@@ -131,6 +141,8 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
131141
string trailingWhitespaceText = sourceText.ToString(reportedSpan);
132142
int firstNewline = trailingWhitespaceText.IndexOf('\n');
133143
int secondNewline = firstNewline >= 0 ? trailingWhitespaceText.IndexOf('\n', firstNewline + 1) : -1;
144+
145+
DiagnosticDescriptor descriptorToReport;
134146
switch (settings.LayoutRules.NewlineAtEndOfFile)
135147
{
136148
case EndOfFileHandling.Omit:
@@ -139,6 +151,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
139151
return;
140152
}
141153

154+
descriptorToReport = DescriptorForOmitSetting;
142155
break;
143156

144157
case EndOfFileHandling.Require:
@@ -147,6 +160,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
147160
return;
148161
}
149162

163+
descriptorToReport = DescriptorForRequireSetting;
150164
break;
151165

152166
case EndOfFileHandling.Allow:
@@ -161,10 +175,11 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
161175
}
162176
}
163177

178+
descriptorToReport = DescriptorForAllowSetting;
164179
break;
165180
}
166181

167-
context.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.Create(context.Tree, reportedSpan)));
182+
context.ReportDiagnostic(Diagnostic.Create(descriptorToReport, Location.Create(context.Tree, reportedSpan)));
168183
}
169184
}
170185
}

0 commit comments

Comments
 (0)