Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -29,6 +29,8 @@ public void Bar(int i)
}
}";

private const string WhiteSpace = "\t ";

/// <summary>
/// Verifies that blank lines at the end of the file will produce a warning.
/// </summary>
Expand All @@ -49,6 +51,76 @@ internal async Task TestWithBlankLinesAtEndOfFileAsync(OptionSetting? newlineAtE
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
/// Verifies file with white space only and no cr/lf at end of file will produce a warning when setting requires.
/// </summary>
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
/// <param name="expectedText">The expected text to appear at the end of the file.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData(null, null)]
[InlineData(OptionSetting.Allow, null)]
[InlineData(OptionSetting.Require, "\r\n")]
[InlineData(OptionSetting.Omit, null)]
internal async Task TestWithWhiteSpaceOnlyAsync(OptionSetting? newlineAtEndOfFile, string expectedText)
{
var testCode = WhiteSpace;
var fixedCode = expectedText;

if (expectedText == null)
{
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
else
{
var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(1, 1);
await VerifyCSharpFixAsync(newlineAtEndOfFile, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}

/// <summary>
/// Verifies file with white space only and cr/lf at end of file will produce a warning when setting requires.
/// </summary>
/// <param name="newlineAtEndOfFile">The effective <see cref="OptionSetting"/> setting.</param>
/// <param name="expectedText">The expected text to appear at the end of the file.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Theory]
[InlineData(null, null)]
[InlineData(OptionSetting.Allow, null)]
[InlineData(OptionSetting.Require, null)]
[InlineData(OptionSetting.Omit, "")]
internal async Task TestWithWhiteSpaceAndNewlineOnlyAsync(OptionSetting? newlineAtEndOfFile, string expectedText)
{
var testCode = WhiteSpace + "\r\n";
var fixedCode = expectedText;

if (expectedText == null)
{
await VerifyCSharpDiagnosticAsync(newlineAtEndOfFile, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
else
{
var expected = Diagnostic(this.GetDescriptor(newlineAtEndOfFile)).WithLocation(1, 1);
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.IsEmpty;
}

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 @@ -185,6 +185,12 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
break;
}

if (context.Tree.IsEmpty(context.CancellationToken))
{
// Empty files never contain line endings.
return;
}

context.ReportDiagnostic(Diagnostic.Create(descriptorToReport, Location.Create(context.Tree, reportedSpan)));
}
}
Expand Down
Loading