Skip to content

Commit 7c4d369

Browse files
committed
IndexerAccesorStyleConsistency: less "stringly" typing
1 parent 181e840 commit 7c4d369

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/FSharpLint.Core/Rules/Conventions/IndexerAccessorStyleConsistency.fs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,50 @@ open FSharpLint.Framework.Suggestion
88
open FSharpLint.Framework
99
open System
1010

11+
type IndexerAccessorStyle =
12+
| OCaml
13+
| CSharp
14+
1115
[<RequireQualifiedAccess>]
1216
type Config = {
13-
Style: string
17+
Style: IndexerAccessorStyle
1418
}
1519

16-
let generateOutput (range: FSharp.Compiler.Text.Range) msg =
20+
let generateOutput (range: FSharp.Compiler.Text.Range) (style: IndexerAccessorStyle) =
1721
Array.singleton
1822
{
1923
Range = range
20-
Message = Resources.GetString msg
24+
Message = String.Format(Resources.GetString "RulesIndexerAccessorStyleConsistency", style.ToString())
2125
SuggestedFix = None
2226
TypeChecks = List.Empty
2327
}
2428

2529
let runner (config: Config) (args: AstNodeRuleParams) =
26-
let styleType = config.Style
27-
if String.Equals (styleType, "ocaml", StringComparison.InvariantCultureIgnoreCase) then
30+
match config.Style with
31+
| IndexerAccessorStyle.OCaml ->
2832
match args.AstNode with
2933
| AstNode.Binding binding ->
3034
match binding with
3135
| SynBinding (_, _, _, _, _, _, _, SynPat.Named _, _,
3236
SynExpr.App (ExprAtomicFlag.Atomic, _, SynExpr.Ident _, SynExpr.ArrayOrListComputed (_, expr, range), _),
3337
_, _, _)
3438
->
35-
generateOutput range "RulesIndexerAccessorStyleConsistencyToOCaml"
39+
generateOutput range IndexerAccessorStyle.OCaml
3640
| _ ->
3741
Array.empty
3842
| _ ->
3943
Array.empty
40-
elif String.Equals (styleType, "csharp", StringComparison.InvariantCultureIgnoreCase) then
44+
| IndexerAccessorStyle.CSharp ->
4145
match args.AstNode with
4246
| AstNode.Binding binding ->
4347
match binding with
4448
| SynBinding (_, _, _, _, _, _, _, SynPat.Named _, _
4549
, SynExpr.DotIndexedGet (_, _, _, range), _, _, _) ->
46-
generateOutput range "RulesIndexerAccessorStyleConsistencyToCSharp"
50+
generateOutput range IndexerAccessorStyle.CSharp
4751
| _ ->
4852
Array.empty
4953
| _ ->
5054
Array.empty
51-
else
52-
failwithf "Unknown style type %s" styleType
5355

5456
let rule config =
5557
AstNodeRule

src/FSharpLint.Core/Text.resx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,7 @@
384384
<data name="InterpolatedStringWithNoSubstitution" xml:space="preserve">
385385
<value>Do not use interpolated string syntax (with $ prefix) or formatting functions (sprintf, failwithf) when not really performing any interpolation.</value>
386386
</data>
387-
<data name="RulesIndexerAccessorStyleConsistencyToCSharp" xml:space="preserve">
388-
<value>Consider switching the indexer accessor to CSharp style.</value>
389-
</data>
390-
<data name="RulesIndexerAccessorStyleConsistencyToOCaml" xml:space="preserve">
391-
<value>Consider switching the indexer accessor to OCaml style.</value>
387+
<data name="RulesIndexerAccessorStyleConsistency" xml:space="preserve">
388+
<value>Consider switching the indexer accessor to {0} style.</value>
392389
</data>
393390
</root>

tests/FSharpLint.Core.Tests/Rules/Conventions/IndexerAccessorStyleConsistency.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ module FSharpLint.Core.Tests.Rules.Conventions.IndexerAccessorStyleConsistency
33
open NUnit.Framework
44
open FSharpLint.Rules
55
open FSharpLint.Core.Tests
6+
open FSharpLint.Rules.IndexerAccessorStyleConsistency
67

78
[<TestFixture>]
89
type TestConventionsIndexerAccessorStyleConsistencyCSharp() =
9-
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(IndexerAccessorStyleConsistency.rule { Style = "CSharp" })
10+
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(IndexerAccessorStyleConsistency.rule { Style = IndexerAccessorStyle.CSharp })
1011

1112
[<Test>]
1213
member this.IndexerAccessorStyleConsistencyOCamlStyleWhenUsingCSharp() =
@@ -32,7 +33,7 @@ System.Console.WriteLine bar"""
3233

3334
[<TestFixture>]
3435
type TestConventionsIndexerAccessorStyleConsistencyOCaml() =
35-
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(IndexerAccessorStyleConsistency.rule { Style = "OCaml" })
36+
inherit TestAstNodeRuleBase.TestAstNodeRuleBase(IndexerAccessorStyleConsistency.rule { Style = IndexerAccessorStyle.OCaml })
3637

3738
[<Test>]
3839
member this.IndexerAccessorStyleConsistencyCSharpStyleWhenUsingOCaml() =

0 commit comments

Comments
 (0)