Skip to content

Commit

Permalink
Core(Tests): new rule and tests for it
Browse files Browse the repository at this point in the history
Add a rule that suggest usage of auto-property for property
with getter that only returns some (immutable) value. Add
tests for it.

Co-authored-by: Mehrshad <code.rezaei@gmail.com>
  • Loading branch information
webwarrior-ws and Mersho committed Dec 7, 2023
1 parent c7ceca1 commit 4c93a90
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/FSharpLint.Core/FSharpLint.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Compile Include="Rules\Conventions\FavourReRaise.fs" />
<Compile Include="Rules\Conventions\FavourConsistentThis.fs" />
<Compile Include="Rules\Conventions\AvoidSinglePipeOperator.fs" />
<Compile Include="Rules\Conventions\SuggestUseAutoProperty.fs" />
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\RaiseWithTooManyArgumentsHelper.fs" />
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\FailwithWithSingleArgument.fs" />
<Compile Include="Rules\Conventions\RaiseWithTooManyArguments\RaiseWithSingleArgument.fs" />
Expand Down
19 changes: 19 additions & 0 deletions src/FSharpLint.Core/Rules/Conventions/SuggestUseAutoProperty.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module FSharpLint.Rules.SuggestUseAutoProperty

open System
open FSharpLint.Framework
open FSharpLint.Framework.Suggestion
open FSharp.Compiler.Syntax
open FSharpLint.Framework.Ast
open FSharpLint.Framework.Rules

let private runner (args: AstNodeRuleParams) =
failwith "Not yet implemented"

let rule =
{ Name = "SuggestUseAutoProperty"
Identifier = Identifiers.FavourConsistentThis
RuleConfig =
{ AstNodeRuleConfig.Runner = runner
Cleanup = ignore } }
|> AstNodeRule
1 change: 1 addition & 0 deletions src/FSharpLint.Core/Rules/Identifiers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ let AvoidTooShortNames = identifier 75
let FavourStaticEmptyFields = identifier 76
let AvoidSinglePipeOperator = identifier 77
let AsyncExceptionWithoutReturn = identifier 78
let SuggestUseAutoProperty = identifier 79
5 changes: 4 additions & 1 deletion src/FSharpLint.Core/Text.resx
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,7 @@
<data name="RulesAsyncExceptionWithoutReturn" xml:space="preserve">
<value>You should use the 'return' keyword when raising an exception in an async computation expression.</value>
</data>
</root>
<data name="RulesSuggestUseAutoProperty" xml:space="preserve">
<value>Consider giving auto-properties via the "val" keyword</value>
</data>
</root>
1 change: 1 addition & 0 deletions tests/FSharpLint.Core.Tests/FSharpLint.Core.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Compile Include="Rules\Conventions\FavourConsistentThis.fs" />
<Compile Include="Rules\Conventions\AvoidTooShortNames.fs" />
<Compile Include="Rules\Conventions\AvoidSinglePipeOperator.fs" />
<Compile Include="Rules\Conventions\SuggestUseAutoProperty.fs" />
<Compile Include="Rules\Conventions\Naming\NamingHelpers.fs" />
<Compile Include="Rules\Conventions\Naming\InterfaceNames.fs" />
<Compile Include="Rules\Conventions\Naming\ExceptionNames.fs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module FSharpLint.Core.Tests.Rules.Conventions.SuggestUseAutoProperty

open NUnit.Framework
open FSharpLint.Framework.Rules
open FSharpLint.Rules

[<TestFixture>]
type TestSuggestUseAutoProperty() =
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(SuggestUseAutoProperty.rule)

[<Test>]
member this.``Should suggest usage of auto-property for property that only returns immutable value`` () =
this.Parse """
type Foo(content: int) =
member self.Content = content
"""

Assert.IsTrue(this.ErrorsExist)

[<Test>]
member this.``Shouldn't suggest usage of auto-property for non-property member``() =
this.Parse """
type Foo(content: int) =
member self.Content() = content
"""

Assert.IsTrue(this.NoErrorsExist)

0 comments on commit 4c93a90

Please sign in to comment.