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 <[email protected]>
  • Loading branch information
webwarrior-ws and Mersho committed Dec 6, 2023
1 parent fe1dc30 commit 2122669
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/FSharpLint.Core/FSharpLint.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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 @@ -82,3 +82,4 @@ let FavourConsistentThis = identifier 74
let AvoidTooShortNames = identifier 75
let FavourStaticEmptyFields = identifier 76
let AvoidSinglePipeOperator = identifier 77
let SuggestUseAutoProperty = identifier 78
4 changes: 4 additions & 0 deletions src/FSharpLint.Core/Text.resx
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,7 @@
<value>Can use normal function call (instead of pipe operator) because there is no invocation chain (single invocation only).</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 @@ -40,6 +40,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 2122669

Please sign in to comment.