|
| 1 | +module FSharpLint.Rules.CSharpFriendlyAsyncOverload |
| 2 | + |
| 3 | +open FSharpLint.Framework |
| 4 | +open FSharpLint.Framework.Suggestion |
| 5 | +open FSharp.Compiler.Syntax |
| 6 | +open FSharp.Compiler.Text |
| 7 | +open FSharpLint.Framework.Ast |
| 8 | +open FSharpLint.Framework.Rules |
| 9 | +open System |
| 10 | + |
| 11 | +type NodeDetails = { Ident: string; Range: range } |
| 12 | + |
| 13 | +let rec private getIdentFromSynPat = |
| 14 | + function |
| 15 | + | SynPat.LongIdent (longDotId = longDotId) -> |
| 16 | + longDotId |
| 17 | + |> ExpressionUtilities.longIdentWithDotsToString |
| 18 | + |> Some |
| 19 | + | SynPat.Typed (pat, _, _) -> getIdentFromSynPat pat |
| 20 | + | _ -> None |
| 21 | + |
| 22 | +let runner (args: AstNodeRuleParams) = |
| 23 | + let hasAsync (syntaxArray: array<AbstractSyntaxArray.Node>) nodeIndex fnIdent = |
| 24 | + let rec hasAsync index = |
| 25 | + if index >= syntaxArray.Length then |
| 26 | + None |
| 27 | + else |
| 28 | + let node = syntaxArray.[index].Actual |
| 29 | + match node with |
| 30 | + | AstNode.Binding (SynBinding (_, _, _, _, _attributes, _, _, pattern, _, _, range, _)) -> |
| 31 | + match getIdentFromSynPat pattern with |
| 32 | + | Some ident when ident = fnIdent + "Async" -> |
| 33 | + { Ident = fnIdent |
| 34 | + Range = range } |> Some |
| 35 | + | _ -> hasAsync (index + 1) |
| 36 | + | _ -> hasAsync (index + 1) |
| 37 | + |
| 38 | + hasAsync nodeIndex |
| 39 | + |
| 40 | + match args.AstNode with |
| 41 | + | AstNode.Binding (SynBinding (_, _, _, _, _, _, _, pattern, synInfo, _, range, _)) -> |
| 42 | + match synInfo with |
| 43 | + | Some (SynBindingReturnInfo (SynType.App(SynType.LongIdent(LongIdentWithDots(ident, _)), _, _, _, _, _, _), _, _)) -> |
| 44 | + match ident with |
| 45 | + | head::_ when head.idText = "Async" -> |
| 46 | + let idents = getIdentFromSynPat pattern |
| 47 | + match idents with |
| 48 | + | Some ident when not (ident.EndsWith "Async") -> |
| 49 | + match hasAsync args.SyntaxArray args.NodeIndex ident with |
| 50 | + | Some _ -> Array.empty |
| 51 | + | None -> |
| 52 | + { Range = range |
| 53 | + Message = String.Format(Resources.GetString "RulesCSharpFriendlyAsyncOverload", ident) |
| 54 | + SuggestedFix = None |
| 55 | + TypeChecks = List.Empty } |
| 56 | + |> Array.singleton |
| 57 | + | _ -> Array.empty |
| 58 | + | _ -> Array.empty |
| 59 | + | _ -> Array.empty |
| 60 | + | _ -> Array.empty |
| 61 | + |
| 62 | + |
| 63 | +let rule = |
| 64 | + { Name = "CSharpFriendlyAsyncOverload" |
| 65 | + Identifier = Identifiers.CSharpFriendlyAsyncOverload |
| 66 | + RuleConfig = |
| 67 | + { AstNodeRuleConfig.Runner = runner |
| 68 | + Cleanup = ignore } } |
| 69 | + |> AstNodeRule |
0 commit comments