Skip to content

Commit 6994273

Browse files
update SA1518 to ignore empty (0 bytes) files
1 parent f5843ae commit 6994273

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ internal async Task TestWithBlankLinesAtEndOfFileAsync(OptionSetting? newlineAtE
4949
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
5050
}
5151

52+
/// <summary>
53+
/// Verifies that empty files will not produce a warning.
54+
/// </summary>
55+
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
56+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
57+
[Theory]
58+
[InlineData(null)]
59+
[InlineData(OptionSetting.Allow)]
60+
[InlineData(OptionSetting.Require)]
61+
[InlineData(OptionSetting.Omit)]
62+
internal async Task TestWithEmptyFileAsync(OptionSetting? newlineAtEndOfFile)
63+
{
64+
var testCode = string.Empty;
65+
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
66+
}
67+
5268
/// <summary>
5369
/// Verifies that linefeed only blank lines at the end of the file will produce a warning.
5470
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SyntaxTreeHelpers.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public static bool IsWhitespaceOnly(this SyntaxTree tree, CancellationToken canc
7272
&& TriviaHelper.IndexOfFirstNonWhitespaceTrivia(firstToken.LeadingTrivia) == -1;
7373
}
7474

75+
public static bool IsEmpty(this SyntaxTree tree, CancellationToken cancellationToken)
76+
{
77+
var root = tree.GetRoot(cancellationToken);
78+
var firstToken = root.GetFirstToken(includeZeroWidth: true);
79+
80+
return firstToken.IsKind(SyntaxKind.EndOfFileToken) && firstToken.FullSpan.Length == 0;
81+
}
82+
7583
internal static bool ContainsUsingAlias(this SyntaxTree tree, ConcurrentDictionary<SyntaxTree, bool> cache)
7684
{
7785
if (tree == null)

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1518UseLineEndingsCorrectlyAtEndOfFile.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public override void Initialize(AnalysisContext context)
7272

7373
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings)
7474
{
75+
if (context.Tree.IsEmpty(context.CancellationToken))
76+
{
77+
// Empty file never contain line endings.
78+
return;
79+
}
80+
7581
var endOfFileToken = context.Tree.GetRoot().GetLastToken(includeZeroWidth: true);
7682
TextSpan reportedSpan = new TextSpan(endOfFileToken.SpanStart, 0);
7783

0 commit comments

Comments
 (0)