Skip to content

Commit c900a60

Browse files
committed
Core(Tests): introduce DisallowShadowing rule
Added DisallowShadowing rule (no implementation yet). Added tests for it.
1 parent 73e6196 commit c900a60

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

src/FSharpLint.Core/FSharpLint.Core.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Rules\Conventions\FailwithBadUsage.fs" />
6262
<Compile Include="Rules\Conventions\FavourSingleton.fs" />
6363
<Compile Include="Rules\Conventions\FavourNestedFunctions.fs" />
64+
<Compile Include="Rules\Conventions\DisallowShadowing.fs" />
6465
<Compile Include="Rules\Conventions\SourceLength\SourceLengthHelper.fs" />
6566
<Compile Include="Rules\Conventions\SourceLength\MaxLinesInLambdaFunction.fs" />
6667
<Compile Include="Rules\Conventions\SourceLength\MaxLinesInMatchLambdaFunction.fs" />
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module FSharpLint.Rules.DisallowShadowing
2+
3+
open System
4+
5+
open FSharpLint.Framework
6+
open FSharpLint.Framework.Suggestion
7+
open FSharp.Compiler.Syntax
8+
open FSharp.Compiler.Text
9+
open FSharp.Compiler.CodeAnalysis
10+
open FSharpLint.Framework.Ast
11+
open FSharpLint.Framework.Rules
12+
13+
let runner (args: AstNodeRuleParams) =
14+
15+
failwith "Not yet implemented"
16+
17+
let rule =
18+
{ Name = "DisallowShadowing"
19+
Identifier = Identifiers.DisallowShadowing
20+
RuleConfig = { AstNodeRuleConfig.Runner = runner; Cleanup = ignore } }
21+
|> AstNodeRule

src/FSharpLint.Core/Rules/Identifiers.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ let IndexerAccessorStyleConsistency = identifier 88
9696
let FavourSingleton = identifier 89
9797
let NoAsyncRunSynchronouslyInLibrary = identifier 90
9898
let FavourNestedFunctions = identifier 91
99+
let DisallowShadowing = identifier 92

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Compile Include="Rules\Conventions\IndexerAccessorStyleConsistency.fs" />
5252
<Compile Include="Rules\Conventions\FavourSingleton.fs" />
5353
<Compile Include="Rules\Conventions\FavourNestedFunctions.fs" />
54+
<Compile Include="Rules\Conventions\DisallowShadowing.fs" />
5455
<Compile Include="Rules\Conventions\Naming\NamingHelpers.fs" />
5556
<Compile Include="Rules\Conventions\Naming\InterfaceNames.fs" />
5657
<Compile Include="Rules\Conventions\Naming\ExceptionNames.fs" />
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
module FSharpLint.Core.Tests.Rules.Conventions.DisallowShadowing
2+
3+
open NUnit.Framework
4+
5+
open FSharpLint.Rules
6+
open FSharpLint.Core.Tests
7+
8+
[<TestFixture>]
9+
type TestConventionsDisallowShadowing() =
10+
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(DisallowShadowing.rule)
11+
12+
[<Test>]
13+
member this.``Should produce error for shadowed variable``() =
14+
this.Parse """
15+
let foo = 0
16+
17+
module Foo =
18+
let foo = 1"""
19+
20+
Assert.IsTrue this.ErrorsExist
21+
22+
[<Test>]
23+
member this.``Should produce error for shadowed variable, inside function``() =
24+
this.Parse """
25+
let bar () =
26+
let foo = 0
27+
let foo = 1
28+
foo"""
29+
30+
Assert.IsTrue this.ErrorsExist
31+
32+
[<Test>]
33+
member this.``Should produce error for shadowed variable (function argument)``() =
34+
this.Parse """
35+
let foo = 0
36+
let bar foo = foo + 1"""
37+
38+
Assert.IsTrue this.ErrorsExist
39+
40+
[<Test>]
41+
member this.``Should produce error for shadowed variable (function argument, inside function)``() =
42+
this.Parse """
43+
let baz () =
44+
let foo = 0
45+
let bar foo = foo + 1
46+
()"""
47+
48+
Assert.IsTrue this.ErrorsExist
49+
50+
[<Test>]
51+
member this.``Should produce error for shadowed variable (function argument, nested)``() =
52+
this.Parse """
53+
let baz foo =
54+
let bar foo = foo + 1
55+
()"""
56+
57+
Assert.IsTrue this.ErrorsExist
58+
59+
[<Test>]
60+
member this.``Should produce error for shadowed variable (function argument, tuple)``() =
61+
this.Parse """
62+
let foo = 0
63+
let bar (foo, baz) = foo + baz"""
64+
65+
Assert.IsTrue this.ErrorsExist
66+
67+
[<Test>]
68+
member this.``Should produce error for shadowed variable (lambda function argument)``() =
69+
this.Parse """
70+
let foo = 0
71+
(fun foo -> foo + 1) 0 |> ignore"""
72+
73+
Assert.IsTrue this.ErrorsExist
74+
75+
[<Test>]
76+
member this.``Should produce error for shadowed variable inside lambda function``() =
77+
this.Parse """
78+
(fun foo ->
79+
let foo = foo + 1
80+
foo) 0 |> ignore"""
81+
82+
Assert.IsTrue this.ErrorsExist
83+
84+
[<Test>]
85+
member this.``Should produce error for shadowed variable (match pattern)``() =
86+
this.Parse """
87+
let foo = 0
88+
match 1 with
89+
| foo -> foo"""
90+
91+
Assert.IsTrue this.ErrorsExist
92+
93+
[<Test>]
94+
member this.``Should produce error for shadowed variable (as match pattern)``() =
95+
this.Parse """
96+
let foo = 0
97+
match (1, 2) with
98+
| (x, y) as foo -> foo"""
99+
100+
Assert.IsTrue this.ErrorsExist
101+
102+
[<Test>]
103+
member this.``Should produce error for shadowed variable inside match pattern``() =
104+
this.Parse """
105+
match 1 with
106+
| foo ->
107+
let foo = foo + 1
108+
foo"""
109+
110+
Assert.IsTrue this.ErrorsExist
111+
112+
[<Test>]
113+
member this.``Should produce error for shadowed variable inside type definition``() =
114+
this.Parse """
115+
type Foo(foo) =
116+
let foo = foo + 1
117+
118+
member this.Bar = foo"""
119+
120+
Assert.IsTrue this.ErrorsExist
121+
122+
[<Test>]
123+
member this.``Should produce error for shadowed variable inside member definition``() =
124+
this.Parse """
125+
type Foo() =
126+
member this.Bar(foo) =
127+
let foo = foo + 1
128+
foo"""
129+
130+
Assert.IsTrue this.ErrorsExist
131+
132+
[<Test>]
133+
member this.``Should not produce error when variable with same name exists in another module``() =
134+
this.Parse """
135+
module Foo =
136+
let foo = 0
137+
let foo = 1"""
138+
139+
Assert.IsTrue this.NoErrorsExist

0 commit comments

Comments
 (0)