Skip to content

Commit

Permalink
use list patterns in analyzers thanks to PolySharp.
Browse files Browse the repository at this point in the history
* Update code style
  • Loading branch information
FreeApophis committed Dec 1, 2023
1 parent fb34859 commit c907848
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>
<ItemGroup Label="Build Dependencies">
<PackageVersion Include="PolySharp" Version="1.14.0" />
<PackageVersion Include="Messerli.CodeStyle" Version="2.2.0" />
<PackageVersion Include="Messerli.CodeStyle" Version="2.3.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.3" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,17 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
private static bool IsWhereInvocation(SyntaxNode syntax, SemanticModel semanticModel, Symbols symbols, [NotNullWhen(true)] out IInvocationOperation? whereInvocation)
{
whereInvocation = null;
return semanticModel.GetOperation(syntax) is IInvocationOperation operation
&& operation.TargetMethod.Name == WhereMethodName
&& SymbolEqualityComparer.Default.Equals(symbols.GenericOptionType, operation.TargetMethod.ContainingType.ConstructedFrom)
&& (whereInvocation = operation) is var _;
return semanticModel.GetOperation(syntax) is IInvocationOperation { TargetMethod.Name: WhereMethodName } operation
&& SymbolEqualityComparer.Default.Equals(symbols.GenericOptionType, operation.TargetMethod.ContainingType.ConstructedFrom)
&& (whereInvocation = operation) is var _;
}

private static bool IsOptionReturnInvocation(IOperation? candidate, Symbols symbols, [NotNullWhen(true)] out IInvocationOperation? returnInvocationOperation)
{
returnInvocationOperation = null;
return candidate is IInvocationOperation operation
&& operation.TargetMethod.Name is MonadReturnMethodName or OptionSomeMethodName
&& SymbolEqualityComparer.Default.Equals(symbols.OptionType, operation.TargetMethod.ContainingType)
&& (returnInvocationOperation = operation) is var _;
return candidate is IInvocationOperation { TargetMethod.Name: MonadReturnMethodName or OptionSomeMethodName } operation
&& SymbolEqualityComparer.Default.Equals(symbols.OptionType, operation.TargetMethod.ContainingType)
&& (returnInvocationOperation = operation) is var _;
}

private static ApplyPredicate? TryGetPredicateApplier(InvocationExpressionSyntax whereInvocation, SemanticModel semanticModel)
Expand All @@ -82,19 +80,19 @@ LambdaExpressionSyntax lambda when
=> lambda switch
{
SimpleLambdaExpressionSyntax { Parameter: var parameter } => parameter,
ParenthesizedLambdaExpressionSyntax { ParameterList.Parameters: { Count: 1 } parameters } => parameters.Single(),
ParenthesizedLambdaExpressionSyntax { ParameterList.Parameters: [var parameter] } => parameter,
_ => null,
};

private static ExpressionSyntax? TryGetLambdaExpression(LambdaExpressionSyntax lambda)
=> lambda switch
{
{ ExpressionBody: { } expressionBody } => expressionBody,
{ Block.Statements: { Count: 1 } statements } when statements.Single() is ReturnStatementSyntax { Expression: var returnExpression } => returnExpression,
{ Block.Statements: [ReturnStatementSyntax { Expression: var returnExpression }] } => returnExpression,
_ => null,
};

private Func<CancellationToken, Task<Document>> ReplaceWithOptionFromBoolean(Document document, Symbols symbols, ApplyPredicate applyPredicate, InvocationExpressionSyntax whereInvocation, InvocationExpressionSyntax returnInvocation)
private static Func<CancellationToken, Task<Document>> ReplaceWithOptionFromBoolean(Document document, Symbols symbols, ApplyPredicate applyPredicate, InvocationExpressionSyntax whereInvocation, InvocationExpressionSyntax returnInvocation)
=> async cancellationToken =>
{
var editor = await DocumentEditor.CreateAsync(document, cancellationToken);
Expand All @@ -112,7 +110,7 @@ private Func<CancellationToken, Task<Document>> ReplaceWithOptionFromBoolean(Doc
return editor.GetChangedDocument();
};

private InvocationExpressionSyntax CreateFromBooleanInvocation(
private static InvocationExpressionSyntax CreateFromBooleanInvocation(
InvocationExpressionSyntax returnInvocation,
Symbols symbols,
SyntaxGenerator generator,
Expand All @@ -127,12 +125,12 @@ private InvocationExpressionSyntax CreateFromBooleanInvocation(
ArgumentList(SeparatedList(new[] { Argument(condition), Argument(returnValue) })))
.WithLeadingTrivia(returnInvocation.GetLeadingTrivia());

private SimpleNameSyntax GetFromBooleanName(InvocationExpressionSyntax returnInvocation)
private static SimpleNameSyntax GetFromBooleanName(InvocationExpressionSyntax returnInvocation)
=> GetMethodName(returnInvocation) is GenericNameSyntax genericNameSyntax
? genericNameSyntax.WithIdentifier(Identifier(OptionFromBooleanMethodName))
: IdentifierName(OptionFromBooleanMethodName);

private SimpleNameSyntax GetMethodName(InvocationExpressionSyntax invocationExpressionSyntax)
private static SimpleNameSyntax GetMethodName(InvocationExpressionSyntax invocationExpressionSyntax)
=> invocationExpressionSyntax.Expression switch
{
SimpleNameSyntax simpleNameSyntax => simpleNameSyntax,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static bool IsGetOrElseEquivalent(INamedTypeSymbol receiverType, IArgume
private static ITypeSymbol? GetTypeOrDelegateReturnType(IOperation operation)
=> operation switch
{
IDelegateCreationOperation { Target: IAnonymousFunctionOperation { Body.Operations: { Length: 1 } operations } } when operations[0] is IReturnOperation returnOperation => returnOperation.ReturnedValue?.Type,
IDelegateCreationOperation { Target: IAnonymousFunctionOperation { Body.Operations: [IReturnOperation returnOperation] } } => returnOperation.ReturnedValue?.Type,
IDelegateCreationOperation { Target: IAnonymousFunctionOperation { Symbol.ReturnType: var returnType } } => returnType,
IDelegateCreationOperation { Target: IMethodReferenceOperation { Method.ReturnType: var returnType } } => returnType,
_ => operation.Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public static bool MatchAnonymousFunctionWithSingleReturn(
[NotNullWhen(true)] out IReturnOperation? functionReturnOperation)
{
functionReturnOperation = null;
return anonymousFunction.Body.Operations.Length == 1
&& anonymousFunction.Body.Operations[0] is IReturnOperation returnOperation
&& (functionReturnOperation = returnOperation) is var _;
return anonymousFunction.Body.Operations is [IReturnOperation returnOperation]
&& (functionReturnOperation = returnOperation) is var _;
}
}
9 changes: 4 additions & 5 deletions Funcky.Analyzers/Funcky.Analyzers/MonadReturnMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ private static bool IsReturn(AlternativeMonadType alternativeMonadType, IMethodS

private static bool IsReturnFunction(AlternativeMonadType alternativeMonadType, IAnonymousFunctionOperation anonymousFunction)
=> MatchAnonymousUnaryFunctionWithSingleReturn(anonymousFunction, out var returnOperation)
&& returnOperation is { ReturnedValue: IInvocationOperation returnedValue }
&& IsReturn(alternativeMonadType, returnedValue.TargetMethod)
&& returnedValue.Arguments.Length == 1
&& returnedValue.Arguments[0].Value is IParameterReferenceOperation { Parameter.ContainingSymbol: var parameterContainingSymbol }
&& SymbolEqualityComparer.Default.Equals(parameterContainingSymbol, anonymousFunction.Symbol);
&& returnOperation is { ReturnedValue: IInvocationOperation returnedValue }
&& IsReturn(alternativeMonadType, returnedValue.TargetMethod)
&& returnedValue.Arguments is [{ Value: IParameterReferenceOperation { Parameter.ContainingSymbol: var parameterContainingSymbol } }]
&& SymbolEqualityComparer.Default.Equals(parameterContainingSymbol, anonymousFunction.Symbol);

private static bool IsImplicitReturn(AlternativeMonadType alternativeMonadType, IAnonymousFunctionOperation anonymousFunction)
=> MatchAnonymousUnaryFunctionWithSingleReturn(anonymousFunction, out var returnOperation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ private static bool TakesExpressionTree(SymbolInfo info, INamedTypeSymbol expres
=> 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);
=> symbol is IMethodSymbol { Parameters: [var firstParameter, ..] }
&& SymbolEqualityComparer.Default.Equals(expressionType, firstParameter.Type.OriginalDefinition);

private sealed record IsExpressionTreeContext(
SyntaxNode Syntax,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FsCheck.Xunit;

namespace Funcky.Test.Extensions;

public sealed class HttpHeadersNonValidatedExtensionsTest
{
[Property]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Numerics;

namespace Funcky.Extensions;

public static partial class ParseExtensions
{
public static Option<TNumber> ParseNumberOrNone<TNumber>(this string value, NumberStyles style, IFormatProvider? provider)
Expand Down
5 changes: 2 additions & 3 deletions Funcky/Extensions/ParseExtensions/ParseExtensions.TimeOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
using Funcky.Internal;

namespace Funcky.Extensions;

[OrNoneFromTryPattern(typeof(TimeOnly), nameof(TimeOnly.TryParse))]
[OrNoneFromTryPattern(typeof(TimeOnly), nameof(TimeOnly.TryParseExact))]
public static partial class ParseExtensions
{
}
public static partial class ParseExtensions;
#endif

0 comments on commit c907848

Please sign in to comment.