Skip to content

Commit

Permalink
DRY up _expressions Add
Browse files Browse the repository at this point in the history
  • Loading branch information
bfarmer67 committed Sep 10, 2024
1 parent 0b1c73b commit cfe4862
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions src/Hyperbee.AsyncExpressions/AwaitSplitVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

namespace Hyperbee.AsyncExpressions;

Expand All @@ -10,40 +11,30 @@ public class AwaitSplitVisitor : ExpressionVisitor

public IReadOnlyList<Expression> Expressions => _expressions;

protected override Expression VisitConstant( ConstantExpression node )
[MethodImpl( MethodImplOptions.AggressiveInlining )]
private Expression ProcessNode( Expression node )
{
if ( _currentExpressions.Count == 0 )
var rewriting = _currentExpressions.Count > 0;

if ( !rewriting )
_expressions.Add( node );

return node;
}

protected override Expression VisitExtension( Expression node )
{
if ( _currentExpressions.Count == 0 )
_expressions.Add( node );
protected override Expression VisitConstant( ConstantExpression node ) => ProcessNode( node );

return node;
}
protected override Expression VisitExtension( Expression node ) => ProcessNode( node );

protected override Expression VisitUnary( UnaryExpression node )
{
if ( _currentExpressions.Count == 0 ) _expressions.Add( node );
return node;
}
protected override Expression VisitUnary( UnaryExpression node ) => ProcessNode( node );

protected override Expression VisitBlock( BlockExpression node )
{
if ( _currentExpressions.Count == 0 ) _expressions.Add( node );
return node;
}
protected override Expression VisitBlock( BlockExpression node ) => ProcessNode( node );

protected override Expression VisitBinary( BinaryExpression node )
{
if ( node.Right is not AwaitExpression awaitExpression )
{
if ( _currentExpressions.Count == 0 ) _expressions.Add( node );
return node;
return ProcessNode( node );
}

_expressions.Add( awaitExpression );
Expand Down Expand Up @@ -82,8 +73,7 @@ protected override Expression VisitMethodCall( MethodCallExpression node )

if(!containedAwait )
{
if ( _currentExpressions.Count == 0 ) _expressions.Add( node );
return node;
return ProcessNode( node );
}

// Rewrite the method call
Expand Down

0 comments on commit cfe4862

Please sign in to comment.