Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to AsNode extension #73645

Merged
merged 2 commits into from
May 22, 2024
Merged
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 @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.CSharp.NewLines.ArrowExpressionClausePlacement;

Expand Down Expand Up @@ -52,8 +53,8 @@ private void Recurse(SyntaxTreeAnalysisContext context, NotificationOption2 noti
if (!context.ShouldAnalyzeSpan(child.Span))
continue;

if (child.IsNode)
Recurse(context, notificationOption, child.AsNode()!);
if (child.AsNode(out var childNode))
Recurse(context, notificationOption, childNode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.CSharp.NewLines.ConstructorInitializerPlacement;

Expand Down Expand Up @@ -58,8 +58,8 @@ private void Recurse(SyntaxTreeAnalysisContext context, NotificationOption2 noti
if (!context.ShouldAnalyzeSpan(child.Span))
continue;

if (child.IsNode)
Recurse(context, notificationOption, child.AsNode()!);
if (child.AsNode(out var childNode))
Recurse(context, notificationOption, childNode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.CSharp.NewLines.EmbeddedStatementPlacement;

Expand Down Expand Up @@ -63,8 +63,8 @@ private void Recurse(SyntaxTreeAnalysisContext context, NotificationOption2 noti
if (!context.ShouldAnalyzeSpan(child.Span))
continue;

if (child.IsNode)
Recurse(context, notificationOption, child.AsNode()!);
if (child.AsNode(out var childNode))
Recurse(context, notificationOption, childNode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ private void ProcessSyntaxTree(
if (!current.ContainsDirectives)
continue;

if (current.IsNode)
if (current.AsNode(out var childNode))
{
// Add the nodes in reverse so we continue walking in a depth-first fashion.
foreach (var child in current.AsNode()!.ChildNodesAndTokens().Reverse())
foreach (var child in childNode.ChildNodesAndTokens().Reverse())
stack.Push(child);
}
else if (current.IsToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ private static bool ContainsVariableDeclaration(

foreach (var child in current.ChildNodesAndTokens())
{
if (child.IsNode)
stack.Push(child.AsNode()!);
if (child.AsNode(out var childNode))
stack.Push(childNode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.NewLines.ConsecutiveStatementPlacement;

Expand Down Expand Up @@ -59,8 +60,8 @@ private void Recurse(SyntaxTreeAnalysisContext context, NotificationOption2 noti
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
Recurse(context, notificationOption, child.AsNode()!, cancellationToken);
if (child.AsNode(out var childNode))
Recurse(context, notificationOption, childNode, cancellationToken);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.NewLines.MultipleBlankLines;
Expand Down Expand Up @@ -59,8 +59,8 @@ private void Recurse(
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
Recurse(context, notificationOption, child.AsNode()!, cancellationToken);
if (child.AsNode(out var childNode))
Recurse(context, notificationOption, childNode, cancellationToken);
else if (child.IsToken)
Copy link
Contributor

Choose a reason for hiding this comment

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

else if (child.IsToken)

else if (child.AsToken(out var childToken))

:)

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe later :) i wanted to stop the AsNode()! craziness.

CheckToken(context, notificationOption, child.AsToken());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ private bool ImplicitMemberAccessWouldBeAffected(SyntaxNode node)
{
foreach (var child in node.ChildNodesAndTokens())
{
if (child.IsNode &&
ImplicitMemberAccessWouldBeAffected(child.AsNode()!))
if (child.AsNode(out var childNode) &&
ImplicitMemberAccessWouldBeAffected(childNode))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ private static void Recurse(

foreach (var child in node.ChildNodesAndTokens().Reverse())
{
if (child.IsNode)
if (child.AsNode(out var childNode))
{
nodeStack.Add(child.AsNode()!);
nodeStack.Add(childNode);
}
else if (child.IsToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,8 @@ private static void AddMatches(

foreach (var child in node.ChildNodesAndTokens())
{
if (child.IsNode)
{
AddMatches(child.AsNode()!, expr, type, matches);
}
if (child.AsNode(out var childNode))
AddMatches(childNode, expr, type, matches);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ bool IterationVariableIsUsedForMoreThanCollectionIndex(SyntaxNode current)

foreach (var child in current.ChildNodesAndTokens())
{
if (child.IsNode)
if (child.AsNode(out var childNode) &&
IterationVariableIsUsedForMoreThanCollectionIndex(childNode))
{
if (IterationVariableIsUsedForMoreThanCollectionIndex(child.AsNode()!))
return true;
return true;
}
}

Expand Down Expand Up @@ -458,8 +458,8 @@ void FindAndReplaceMatches(SyntaxNode current)

foreach (var child in current.ChildNodesAndTokens())
{
if (child.IsNode)
FindAndReplaceMatches(child.AsNode()!);
if (child.AsNode(out var childNode))
FindAndReplaceMatches(childNode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.EmbeddedLanguages;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;

namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageServices;
Expand Down Expand Up @@ -69,9 +70,9 @@ private void Analyze(
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
if (child.AsNode(out var childNode))
{
Analyze(context, detector, child.AsNode()!, cancellationToken);
Analyze(context, detector, childNode, cancellationToken);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.EmbeddedLanguages;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;

namespace Microsoft.CodeAnalysis.Features.EmbeddedLanguages.Json.LanguageServices;
Expand Down Expand Up @@ -63,9 +64,9 @@ private void Analyze(
if (!context.ShouldAnalyzeSpan(child.FullSpan))
continue;

if (child.IsNode)
if (child.AsNode(out var childNode))
{
Analyze(context, detector, child.AsNode()!, cancellationToken);
Analyze(context, detector, childNode, cancellationToken);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Extensions;

namespace Microsoft.CodeAnalysis.Structure;

Expand Down Expand Up @@ -47,9 +48,9 @@ private void Collect(SyntaxNode root, ref TemporaryArray<BlockSpan> spans)
SyntaxToken previousToken = default;
foreach (var nodeOrToken in root.DescendantNodesAndTokensAndSelf(descendIntoTrivia: true))
{
if (nodeOrToken.IsNode)
if (nodeOrToken.AsNode(out var childNode))
{
GetBlockSpans(previousToken, nodeOrToken.AsNode()!, ref spans);
GetBlockSpans(previousToken, childNode, ref spans);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ private void ProcessNode(
cancellationToken.ThrowIfCancellationRequested();
foreach (var child in node.ChildNodesAndTokens())
{
if (child.IsNode)
if (child.AsNode(out var childNode))
{
ProcessNode(syntaxFacts, child.AsNode()!, matches, cancellationToken);
ProcessNode(syntaxFacts, childNode, matches, cancellationToken);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ private ImmutableArray<SyntaxToken> FindMatchingIdentifierTokensFromTree(
while (stack.TryPop(out var current))
{
cancellationToken.ThrowIfCancellationRequested();
if (current.IsNode)
if (current.AsNode(out var currentNode))
{
foreach (var child in current.AsNode()!.ChildNodesAndTokens().Reverse())
foreach (var child in currentNode.ChildNodesAndTokens().Reverse())
stack.Push(child);
}
else if (current.IsToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Storage;
using Roslyn.Utilities;

Expand Down Expand Up @@ -141,9 +142,9 @@ private static bool ContainsIfDirective(SyntaxNode node, int ifDirectiveKind)
if (!child.ContainsDirectives)
continue;

if (child.IsNode)
if (child.AsNode(out var childNode))
{
if (ContainsIfDirective(child.AsNode()!, ifDirectiveKind))
if (ContainsIfDirective(childNode, ifDirectiveKind))
return true;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ void Recurse(TextSpan span, SemanticModel semanticModel)

foreach (var child in current.ChildNodesAndTokens())
{
if (child.IsNode)
{
stack.Add(child.AsNode()!);
}
if (child.AsNode(out var childNode))
stack.Add(childNode);

var token = child.AsToken();
if (token != tokenFromNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ void Recurse(TextSpan span, SemanticModel semanticModel)

foreach (var child in current.ChildNodesAndTokens())
{
if (child.IsNode)
stack.Add(child.AsNode()!);
if (child.AsNode(out var childNode))
stack.Add(childNode);
}
}
}
Expand Down Expand Up @@ -305,8 +305,8 @@ bool AnalyzePotentialMatches(

foreach (var child in current.ChildNodesAndTokens())
{
if (child.IsNode)
stack.Add(child.AsNode()!);
if (child.AsNode(out var childNode))
stack.Add(childNode);
}

// Ignore any nodes before the decl.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public void Visit(SyntaxNode node)
{
foreach (var child in node.DescendantNodesAndTokensAndSelf())
{
if (child.IsNode)
if (child.AsNode(out var childNode))
{
var childNode = child.AsNode()!;
var info = semanticModel.GetSymbolInfo(childNode);
if (!IsNone(info))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,10 @@ private static bool RemovalMayIntroduceInterpolationAmbiguity(ParenthesizedExpre
foreach (var nodeOrToken in expression.ChildNodesAndTokens())
{
// Note: There's no need drill into other parenthesized expressions, since any colons in them would be unambiguous.
if (nodeOrToken.IsNode && !nodeOrToken.IsKind(SyntaxKind.ParenthesizedExpression))
if (nodeOrToken.AsNode(out var childNode))
{
stack.Push(nodeOrToken.AsNode()!);
if (!childNode.IsKind(SyntaxKind.ParenthesizedExpression))
stack.Push(childNode);
}
else if (nodeOrToken.IsToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,10 @@ private static void GetAllDeclaredSymbols(

foreach (var child in node.ChildNodesAndTokens())
{
if (child.IsNode)
if (child.AsNode(out var childNode) &&
ShouldDescendInto(childNode, descendInto))
{
var childNode = child.AsNode()!;
if (ShouldDescendInto(childNode, descendInto))
{
GetAllDeclaredSymbols(semanticModel, childNode, symbols, cancellationToken, descendInto);
}
GetAllDeclaredSymbols(semanticModel, childNode, symbols, cancellationToken, descendInto);
}
}

Expand Down
Loading
Loading