Skip to content

Commit bb6471e

Browse files
committed
Core(Tests): add FavourNestedFunctions rule
Added FavourNestedFunctions rule and tests for it.
1 parent 2b5b1ef commit bb6471e

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

src/FSharpLint.Core/FSharpLint.Core.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<Compile Include="Rules\Conventions\UsedUnderscorePrefixedElements.fs" />
5959
<Compile Include="Rules\Conventions\FavourNonMutablePropertyInitialization.fs" />
6060
<Compile Include="Rules\Conventions\EnsureTailCallDiagnosticsInRecursiveFunctions.fs" />
61+
<Compile Include="Rules\Conventions\FavourNestedFunctions.fs" />
6162
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\RaiseWithTooManyArgumentsHelper.fs" />
6263
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\FailwithWithSingleArgument.fs" />
6364
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\RaiseWithSingleArgument.fs" />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module FSharpLint.Rules.FavourNestedFunctions
2+
3+
open System
4+
open FSharp.Compiler.Syntax
5+
open FSharpLint.Framework.Ast
6+
open FSharpLint.Framework.Rules
7+
open FSharpLint.Framework
8+
open FSharpLint.Framework.Suggestion
9+
10+
let runner (args: AstNodeRuleParams) =
11+
failwith "Not yet implemeted"
12+
13+
let rule =
14+
{ Name = "FavourNestedFunctions"
15+
Identifier = Identifiers.FavourNestedFunctions
16+
RuleConfig =
17+
{ AstNodeRuleConfig.Runner = runner
18+
Cleanup = ignore } }
19+
|> AstNodeRule

src/FSharpLint.Core/Rules/Identifiers.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,4 @@ let UnneededRecKeyword = identifier 83
9292
let FavourNonMutablePropertyInitialization = identifier 84
9393
let EnsureTailCallDiagnosticsInRecursiveFunctions = identifier 85
9494
let FavourAsKeyword = identifier 86
95+
let FavourNestedFunctions = identifier 87

tests/FSharpLint.Core.Tests/FSharpLint.Core.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Compile Include="Rules\Conventions\UsedUnderscorePrefixedElements.fs" />
4747
<Compile Include="Rules\Conventions\FavourNonMutablePropertyInitialization.fs" />
4848
<Compile Include="Rules\Conventions\EnsureTailCallDiagnosticsInRecursiveFunctions.fs" />
49+
<Compile Include="Rules\Conventions\FavourNestedFunctions.fs" />
4950
<Compile Include="Rules\Conventions\Naming\NamingHelpers.fs" />
5051
<Compile Include="Rules\Conventions\Naming\InterfaceNames.fs" />
5152
<Compile Include="Rules\Conventions\Naming\ExceptionNames.fs" />
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
module FSharpLint.Core.Tests.Rules.Conventions.FavourNestedFunctions
2+
3+
open NUnit.Framework
4+
open FSharpLint.Rules
5+
open FSharpLint.Core.Tests
6+
7+
[<TestFixture>]
8+
type TestFavourNestedFunctions() =
9+
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(FavourNestedFunctions.rule)
10+
11+
[<Test>]
12+
member this.``Top level functions that are not used in another function should not give an error`` () =
13+
this.Parse """
14+
let Foo () =
15+
()
16+
17+
let Bar () =
18+
()
19+
"""
20+
21+
this.AssertNoWarnings()
22+
23+
[<Test>]
24+
member this.``Top level private functions that are not used in another function should not give an error`` () =
25+
this.Parse """
26+
let private Foo () =
27+
()
28+
29+
let Bar () =
30+
()
31+
"""
32+
33+
this.AssertNoWarnings()
34+
35+
[<Test>]
36+
member this.``Top level private function that is used in single function should give an error`` () =
37+
this.Parse """
38+
let private Foo () =
39+
()
40+
41+
let Bar () =
42+
Foo()
43+
()
44+
"""
45+
46+
Assert.IsTrue this.ErrorsExist
47+
48+
[<Test>]
49+
member this.``Nested functions should not give an error`` () =
50+
this.Parse """
51+
let Bar () =
52+
let Foo() =
53+
()
54+
55+
Foo()
56+
()
57+
"""
58+
59+
this.AssertNoWarnings()
60+
61+
[<Test>]
62+
member this.``Private function that is used in more than one function should not give an error`` () =
63+
this.Parse """
64+
let private Foo () =
65+
()
66+
67+
let Bar () =
68+
Foo()
69+
()
70+
71+
let Baz () =
72+
Foo ()
73+
()
74+
"""
75+
76+
this.AssertNoWarnings()

0 commit comments

Comments
 (0)