Skip to content

Commit 40f4cca

Browse files
authored
Merge pull request #3927 from DWIAltonaAnalytics/#3926
update SA1518 to ignore empty (0 bytes) files
2 parents 60b1b1c + f951587 commit 40f4cca

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public void Bar(int i)
2929
}
3030
}";
3131

32+
private const string WhiteSpace = "\t ";
33+
3234
/// <summary>
3335
/// Verifies that blank lines at the end of the file will produce a warning.
3436
/// </summary>
@@ -49,6 +51,83 @@ internal async Task TestWithBlankLinesAtEndOfFileAsync(OptionSetting? newlineAtE
4951
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
5052
}
5153

54+
/// <summary>
55+
/// Verifies file with white space only and no cr/lf at end of file will produce a warning when setting requires.
56+
/// </summary>
57+
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
58+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
59+
[Theory]
60+
[InlineData(null)]
61+
[InlineData(OptionSetting.Allow)]
62+
[InlineData(OptionSetting.Require)]
63+
[InlineData(OptionSetting.Omit)]
64+
internal async Task TestWithWhiteSpaceOnlyAsync(OptionSetting? newlineAtEndOfFile)
65+
{
66+
var testCode = WhiteSpace;
67+
68+
var fixedCode = newlineAtEndOfFile switch
69+
{
70+
OptionSetting.Require => "\r\n",
71+
_ => null,
72+
};
73+
74+
if (fixedCode == null)
75+
{
76+
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
77+
}
78+
else
79+
{
80+
var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(1, 1);
81+
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
82+
}
83+
}
84+
85+
/// <summary>
86+
/// Verifies file with white space only and cr/lf at end of file will produce a warning when setting requires.
87+
/// </summary>
88+
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
89+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
90+
[Theory]
91+
[InlineData(null)]
92+
[InlineData(OptionSetting.Allow)]
93+
[InlineData(OptionSetting.Require)]
94+
[InlineData(OptionSetting.Omit)]
95+
internal async Task TestWithWhiteSpaceAndNewlineOnlyAsync(OptionSetting? newlineAtEndOfFile)
96+
{
97+
var testCode = WhiteSpace + "\r\n";
98+
var fixedCode = newlineAtEndOfFile switch
99+
{
100+
OptionSetting.Omit => string.Empty,
101+
_ => null,
102+
};
103+
104+
if (fixedCode == null)
105+
{
106+
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
107+
}
108+
else
109+
{
110+
var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(1, 1);
111+
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
112+
}
113+
}
114+
115+
/// <summary>
116+
/// Verifies that empty files will not produce a warning.
117+
/// </summary>
118+
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
119+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
120+
[Theory]
121+
[InlineData(null)]
122+
[InlineData(OptionSetting.Allow)]
123+
[InlineData(OptionSetting.Require)]
124+
[InlineData(OptionSetting.Omit)]
125+
internal async Task TestWithEmptyFileAsync(OptionSetting? newlineAtEndOfFile)
126+
{
127+
var testCode = string.Empty;
128+
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
129+
}
130+
52131
/// <summary>
53132
/// Verifies that linefeed only blank lines at the end of the file will produce a warning.
54133
/// </summary>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,10 @@ public static bool IsWhitespaceOnly(this SyntaxTree tree, CancellationToken canc
6767
return firstToken.IsKind(SyntaxKind.EndOfFileToken)
6868
&& TriviaHelper.IndexOfFirstNonWhitespaceTrivia(firstToken.LeadingTrivia) == -1;
6969
}
70+
71+
public static bool IsEmpty(this SyntaxTree tree)
72+
{
73+
return tree.Length == 0;
74+
}
7075
}
7176
}

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())
76+
{
77+
// Empty files 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)