-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
58 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
Funcky.Analyzers/Funcky.Analyzers/SyntaxNodeExtensions.ExpressionTree.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Funcky.Analyzers; | ||
|
||
internal static partial class SyntaxNodeExtensions | ||
{ | ||
// Adapted from Roslyn's source code as this API is not public: | ||
// https://github.com/dotnet/roslyn/blob/232f7afa4966411958759c880de3a1765bdb28a0/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Extensions/SyntaxNodeExtensions.cs#L925 | ||
public static bool IsInExpressionTree( | ||
[NotNullWhen(returnValue: true)] this SyntaxNode? node, | ||
SemanticModel semanticModel, | ||
[NotNullWhen(returnValue: true)] INamedTypeSymbol? expressionType, | ||
CancellationToken cancellationToken) | ||
=> expressionType is not null | ||
&& node is not null | ||
&& node | ||
.AncestorsAndSelf() | ||
.Any(current => IsExpressionTree(new(current, expressionType, semanticModel, cancellationToken))); | ||
|
||
private static bool IsExpressionTree(IsExpressionTreeContext context) | ||
=> context.Syntax switch | ||
{ | ||
var node when node.IsAnyLambda() => LambdaIsExpressionTree(context), | ||
SelectOrGroupClauseSyntax or OrderingSyntax => QueryExpressionIsExpressionTree(context), | ||
QueryClauseSyntax queryClause => QueryClauseIsExpressionTree(context, queryClause), | ||
_ => false, | ||
}; | ||
|
||
private static bool LambdaIsExpressionTree(IsExpressionTreeContext context) | ||
{ | ||
var typeInfo = context.SemanticModel.GetTypeInfo(context.Syntax, context.CancellationToken); | ||
return SymbolEqualityComparer.Default.Equals(context.ExpressionType, typeInfo.ConvertedType?.OriginalDefinition); | ||
} | ||
|
||
private static bool QueryExpressionIsExpressionTree(IsExpressionTreeContext context) | ||
{ | ||
var info = context.SemanticModel.GetSymbolInfo(context.Syntax, context.CancellationToken); | ||
return TakesExpressionTree(info, context.ExpressionType); | ||
} | ||
|
||
private static bool QueryClauseIsExpressionTree(IsExpressionTreeContext context, QueryClauseSyntax queryClause) | ||
{ | ||
var info = context.SemanticModel.GetQueryClauseInfo(queryClause, context.CancellationToken); | ||
return TakesExpressionTree(info.CastInfo, context.ExpressionType) | ||
|| TakesExpressionTree(info.OperationInfo, context.ExpressionType); | ||
} | ||
|
||
private static bool TakesExpressionTree(SymbolInfo info, INamedTypeSymbol expressionType) | ||
=> GetAllSymbols(info).Any(symbol => TakesExpressionTreeAsFirstArgument(symbol, expressionType)); | ||
|
||
private static bool TakesExpressionTreeAsFirstArgument(ISymbol symbol, INamedTypeSymbol expressionType) | ||
=> symbol is IMethodSymbol method | ||
&& method.Parameters.Length > 0 | ||
&& SymbolEqualityComparer.Default.Equals(expressionType, method.Parameters[0].Type?.OriginalDefinition); | ||
|
||
private sealed record IsExpressionTreeContext( | ||
SyntaxNode Syntax, | ||
INamedTypeSymbol ExpressionType, | ||
SemanticModel SemanticModel, | ||
CancellationToken CancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters