@@ -7,8 +7,63 @@ open FSharpLint.Framework.Rules
77open FSharpLint.Framework
88open FSharpLint.Framework .Suggestion
99
10+ let private (| FunctionDeclaration | _ |) ( declaration : SynModuleDecl ) =
11+ match declaration with
12+ | SynModuleDecl.Let(_, [ SynBinding(_, _, _, _, _, _, _, headPat, _, expr, _, _) ], _) ->
13+ match headPat with
14+ | SynPat.LongIdent( LongIdentWithDots([ ident], _), _, _, _, accessibility, _) ->
15+ Some( ident, expr, accessibility)
16+ | _ -> None
17+ | _ -> None
18+
1019let runner ( args : AstNodeRuleParams ) =
11- failwith " Not yet implemeted"
20+ match args.AstNode with
21+ | AstNode.ModuleOrNamespace( SynModuleOrNamespace(_, _, _ kind, declarations, _, _, _, _)) ->
22+ let privateFunctionIdentifiers =
23+ declarations
24+ |> Seq.choose
25+ ( fun declaration ->
26+ match declaration with
27+ | FunctionDeclaration( ident, _ body, Some( SynAccess.Private)) ->
28+ Some ident
29+ | _ -> None)
30+ |> Seq.toArray
31+
32+ match args.CheckInfo with
33+ | Some checkInfo when privateFunctionIdentifiers.Length > 0 ->
34+ let otherFunctionBodies =
35+ declarations
36+ |> List.choose
37+ ( fun declaration ->
38+ match declaration with
39+ | FunctionDeclaration( ident, body, _)
40+ when not ( Array.exists ( fun ( each : Ident ) -> each.idText = ident.idText) privateFunctionIdentifiers) ->
41+ Some body
42+ | _ -> None)
43+
44+ privateFunctionIdentifiers
45+ |> Array.choose
46+ ( fun currFunctionIdentifier ->
47+ match ExpressionUtilities.getSymbolFromIdent args.CheckInfo ( SynExpr.Ident currFunctionIdentifier) with
48+ | Some symbolUse ->
49+ let numberOfOtherFunctionsCurrFunctionIsUsedIn =
50+ otherFunctionBodies
51+ |> Seq.filter ( fun funcBody ->
52+ checkInfo.GetUsesOfSymbolInFile symbolUse.Symbol
53+ |> Array.exists ( fun usage -> ExpressionUtilities.rangeContainsOtherRange funcBody.Range usage.Range))
54+ |> Seq.length
55+ if numberOfOtherFunctionsCurrFunctionIsUsedIn = 1 then
56+ Some {
57+ Range = currFunctionIdentifier.idRange
58+ WarningDetails.Message = Resources.GetString " RulesFavourNestedFunctions"
59+ SuggestedFix = None
60+ TypeChecks = List.Empty
61+ }
62+ else
63+ None
64+ | None -> None)
65+ | _ -> Array.empty
66+ | _ -> Array.empty
1267
1368let rule =
1469 { Name = " FavourNestedFunctions"
0 commit comments