Skip to content

Commit 25f82b1

Browse files
committed
Simplified SyntaxTree.IsEmpty()
1 parent e73109e commit 25f82b1

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@ public static bool IsWhitespaceOnly(this SyntaxTree tree, CancellationToken canc
6868
&& TriviaHelper.IndexOfFirstNonWhitespaceTrivia(firstToken.LeadingTrivia) == -1;
6969
}
7070

71-
public static bool IsEmpty(this SyntaxTree tree, CancellationToken cancellationToken)
71+
public static bool IsEmpty(this SyntaxTree tree)
7272
{
73-
var root = tree.GetRoot(cancellationToken);
74-
var firstToken = root.GetFirstToken(includeZeroWidth: true);
75-
76-
return firstToken.IsKind(SyntaxKind.EndOfFileToken) && firstToken.FullSpan.IsEmpty;
73+
return tree.Length == 0;
7774
}
7875
}
7976
}

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

Lines changed: 6 additions & 6 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

@@ -185,12 +191,6 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCop
185191
break;
186192
}
187193

188-
if (context.Tree.IsEmpty(context.CancellationToken))
189-
{
190-
// Empty files never contain line endings.
191-
return;
192-
}
193-
194194
context.ReportDiagnostic(Diagnostic.Create(descriptorToReport, Location.Create(context.Tree, reportedSpan)));
195195
}
196196
}

0 commit comments

Comments
 (0)