|
| 1 | +module FSharpLint.Rules.FavourNonMutablePropertyInitialization |
| 2 | + |
| 3 | +open FSharpLint.Framework |
| 4 | +open FSharpLint.Framework.Suggestion |
| 5 | +open FSharp.Compiler.Syntax |
| 6 | +open FSharpLint.Framework.Ast |
| 7 | +open FSharpLint.Framework.Rules |
| 8 | +open System |
| 9 | + |
| 10 | +let private getWarningDetails (ident: Ident) = |
| 11 | + let formatError errorName = |
| 12 | + String.Format(Resources.GetString errorName, ident.idText) |
| 13 | + |
| 14 | + "RulesFavourNonMutablePropertyInitializationError" |
| 15 | + |> formatError |
| 16 | + |> Array.singleton |
| 17 | + |> Array.map (fun message -> |
| 18 | + { Range = ident.idRange |
| 19 | + Message = message |
| 20 | + SuggestedFix = None |
| 21 | + TypeChecks = List.Empty }) |
| 22 | + |
| 23 | +let private extraInstanceMethod (app:SynExpr) (instanceMethodCalls: List<string>) = |
| 24 | + match app with |
| 25 | + | SynExpr.App(_, _, expression, _, _) -> |
| 26 | + match expression with |
| 27 | + | SynExpr.LongIdent(_, SynLongIdent(identifiers, _, _), _, _) -> |
| 28 | + match List.tryLast identifiers with |
| 29 | + | Some _ -> |
| 30 | + identifiers.[0].idText::instanceMethodCalls |
| 31 | + | _ -> instanceMethodCalls |
| 32 | + | _ -> instanceMethodCalls |
| 33 | + | _ -> instanceMethodCalls |
| 34 | + |
| 35 | +let rec private extraFromBindings (bindings: List<SynBinding>) (classInstances: List<string>) = |
| 36 | + match bindings with |
| 37 | + | SynBinding(_, _, _, _, _, _, _, SynPat.Named(SynIdent(ident, _), _, _, _), _, _expression, _, _, _)::rest -> |
| 38 | + extraFromBindings rest (ident.idText::classInstances) |
| 39 | + | _ -> classInstances |
| 40 | + |
| 41 | +let rec private processLetBinding (instanceNames: Set<string>) (body: SynExpr) : array<WarningDetails> = |
| 42 | + match body with |
| 43 | + | SynExpr.LongIdentSet(SynLongIdent(identifiers, _, _), _, _) -> |
| 44 | + match identifiers with |
| 45 | + | [instanceIdent; propertyIdent] when Set.contains instanceIdent.idText instanceNames -> |
| 46 | + getWarningDetails propertyIdent |
| 47 | + | _ -> Array.empty |
| 48 | + | SynExpr.Sequential(_, _, expr1, expr2, _) -> |
| 49 | + let instanceNames = |
| 50 | + Set.difference |
| 51 | + instanceNames |
| 52 | + (extraInstanceMethod expr1 List.empty |> Set.ofList) |
| 53 | + Array.append |
| 54 | + (processLetBinding instanceNames expr1) |
| 55 | + (processLetBinding instanceNames expr2) |
| 56 | + | _ -> [||] |
| 57 | + |
| 58 | +and processExpression (expression: SynExpr) : array<WarningDetails> = |
| 59 | + match expression with |
| 60 | + | SynExpr.LetOrUse(_, _, bindings, body, _, _) -> |
| 61 | + let instanceNames = extraFromBindings bindings [] |> Set.ofList |
| 62 | + processLetBinding instanceNames body |
| 63 | + | SynExpr.Sequential(_, _, expr1, expr2, _) -> |
| 64 | + Array.append |
| 65 | + (processExpression expr1) |
| 66 | + (processExpression expr2) |
| 67 | + | _ -> Array.empty |
| 68 | + |
| 69 | +let runner args = |
| 70 | + match args.AstNode with |
| 71 | + | Binding(SynBinding(_, _, _, _, _, _, _, _, _, SynExpr.LetOrUse(_, _, bindings, body, _, _), _, _, _)) -> |
| 72 | + let instanceNames = extraFromBindings bindings [] |> Set.ofList |
| 73 | + processLetBinding instanceNames body |
| 74 | + | Match(SynMatchClause(_, _, expr, _, _, _)) -> |
| 75 | + processExpression expr |
| 76 | + | Lambda(lambda, _) -> |
| 77 | + processExpression lambda.Body |
| 78 | + | Expression(SynExpr.TryWith(tryExpr, _, _, _, _, _)) -> |
| 79 | + processExpression tryExpr |
| 80 | + | Expression(SynExpr.TryFinally(tryExpr, finallyExpr, _, _, _, _)) -> |
| 81 | + Array.append |
| 82 | + (processExpression tryExpr) |
| 83 | + (processExpression finallyExpr) |
| 84 | + | Expression(SynExpr.ComputationExpr(_, expr, _)) -> |
| 85 | + processExpression expr |
| 86 | + | _ -> Array.empty |
| 87 | + |
| 88 | +let rule = |
| 89 | + { Name = "FavourNonMutablePropertyInitialization" |
| 90 | + Identifier = Identifiers.FavourNonMutablePropertyInitialization |
| 91 | + RuleConfig = { AstNodeRuleConfig.Runner = runner; Cleanup = ignore } } |
| 92 | + |> AstNodeRule |
0 commit comments