Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ internal async Task TestWithBlankLinesAtEndOfFileAsync(OptionSetting? newlineAtE
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that empty files will not produce a warning.
/// </summary>
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData(null)]
[InlineData(OptionSetting.Allow)]
[InlineData(OptionSetting.Require)]
[InlineData(OptionSetting.Omit)]
internal async Task TestWithEmptyFileAsync(OptionSetting? newlineAtEndOfFile)
{
var testCode = string.Empty;
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies that linefeed only blank lines at the end of the file will produce a warning.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public static bool IsWhitespaceOnly(this SyntaxTree tree, CancellationToken canc
&& TriviaHelper.IndexOfFirstNonWhitespaceTrivia(firstToken.LeadingTrivia) == -1;
}

public static bool IsEmpty(this SyntaxTree tree, CancellationToken cancellationToken)
{
var root = tree.GetRoot(cancellationToken);
var firstToken = root.GetFirstToken(includeZeroWidth: true);

return firstToken.IsKind(SyntaxKind.EndOfFileToken) && firstToken.FullSpan.Length == 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests for SA1518 can be improved by checking a file with a single space or something like that. firstToken.FullSpan.Length == 0 can currently be removed without any tests failing,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added two test cases with white space only "files".
Of those (depending on the setting) one would fail if the IsEmpty helper does not check if the token is empty.

}

internal static bool ContainsUsingAlias(this SyntaxTree tree, ConcurrentDictionary<SyntaxTree, bool> cache)
{
if (tree == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public override void Initialize(AnalysisContext context)

private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings)
{
if (context.Tree.IsEmpty(context.CancellationToken))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering how rare this is, I would have added this check as the last thing before reporting the diagnostic, to save a couple of clock cycles when no diagnostics are reported.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed and done.

{
// Empty file never contain line endings.
return;
}

var endOfFileToken = context.Tree.GetRoot().GetLastToken(includeZeroWidth: true);
TextSpan reportedSpan = new TextSpan(endOfFileToken.SpanStart, 0);

Expand Down