-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalizes the existing analyzer to cover ValueOption and `.Value` member access
- Loading branch information
1 parent
ab29042
commit 360d688
Showing
7 changed files
with
137 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Ionide.Analyzers/Suggestion/HandleOptionGracefullyAnalyzer.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Ionide.Analyzers.Suggestion.HandleOptionGracefullyAnalyzer | ||
|
||
open System | ||
open FSharp.Analyzers.SDK | ||
open FSharp.Analyzers.SDK.TASTCollecting | ||
open FSharp.Compiler.Symbols | ||
open FSharp.Compiler.Text | ||
|
||
[<CliAnalyzer("HandleOptionGracefullyAnalyzer", | ||
"Replace unsafe option unwrapping with graceful handling of each case.", | ||
"https://ionide.io/ionide-analyzers/suggestion/006.html")>] | ||
let optionGetAnalyzer (ctx: CliContext) = | ||
async { | ||
let messages = ResizeArray<Message>() | ||
|
||
let walker = | ||
{ new TypedTreeCollectorBase() with | ||
override x.WalkCall _ (mfv: FSharpMemberOrFunctionOrValue) _ _ (args: FSharpExpr list) (m: range) = | ||
let fullyQualifiedCall = | ||
String.Join(".", mfv.DeclaringEntity.Value.FullName, mfv.DisplayName) | ||
|
||
if | ||
(mfv.FullName = "Microsoft.FSharp.Core.Option.get" | ||
|| mfv.FullName = "Microsoft.FSharp.Core.ValueOption.get" | ||
|| fullyQualifiedCall = "Microsoft.FSharp.Core.FSharpOption`1.Value" | ||
|| fullyQualifiedCall = "Microsoft.FSharp.Core.FSharpValueOption`1.Value") | ||
&& args.Length = 1 | ||
then | ||
messages.Add | ||
{ | ||
Type = "HandleOptionGracefully" | ||
Message = "Replace unsafe option unwrapping with graceful handling of each case." | ||
Code = "IONIDE-006" | ||
Severity = Severity.Hint | ||
Range = m | ||
Fixes = [] | ||
} | ||
} | ||
|
||
match ctx.TypedTree with | ||
| None -> return [] | ||
| Some typedTree -> | ||
walkTast walker typedTree | ||
return Seq.toList messages | ||
} |
35 changes: 0 additions & 35 deletions
35
src/Ionide.Analyzers/Suggestion/ReplaceOptionGetWithGracefulHandlingAnalyzer.fs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tests/Ionide.Analyzers.Tests/Suggestion/HandleOptionGracefullyAnalyzerTests.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module Ionide.Analyzers.Tests.Suggestion.HandleOptionGracefullyAnalyzerTests | ||
|
||
open NUnit.Framework | ||
open FSharp.Compiler.CodeAnalysis | ||
open FSharp.Analyzers.SDK.Testing | ||
open Ionide.Analyzers.Suggestion.HandleOptionGracefullyAnalyzer | ||
|
||
let mutable projectOptions: FSharpProjectOptions = FSharpProjectOptions.zero | ||
|
||
let messageString = | ||
"Replace unsafe option unwrapping with graceful handling of each case." | ||
|
||
[<SetUp>] | ||
let Setup () = | ||
task { | ||
let! opts = mkOptionsFromProject "net7.0" [] | ||
projectOptions <- opts | ||
} | ||
|
||
[<Test>] | ||
let ``Option.get is detected`` () = | ||
async { | ||
let source = | ||
""" | ||
module M | ||
let option = Some 10 | ||
let value = Option.get option | ||
""" | ||
|
||
let ctx = getContext projectOptions source | ||
let! msgs = optionGetAnalyzer ctx | ||
Assert.IsNotEmpty msgs | ||
Assert.IsTrue(Assert.messageContains messageString msgs[0]) | ||
} | ||
|
||
[<Test>] | ||
let ``ValueOption.get is detected`` () = | ||
async { | ||
let source = | ||
""" | ||
module M | ||
let voption = ValueSome 10 | ||
let value = ValueOption.get voption | ||
""" | ||
|
||
let ctx = getContext projectOptions source | ||
let! msgs = optionGetAnalyzer ctx | ||
Assert.IsNotEmpty msgs | ||
Assert.IsTrue(Assert.messageContains messageString msgs[0]) | ||
} | ||
|
||
[<Test>] | ||
let ``Option.Value member is detected`` () = | ||
async { | ||
let source = | ||
""" | ||
module M | ||
let option = Some 10 | ||
let value = option.Value | ||
""" | ||
|
||
let ctx = getContext projectOptions source | ||
let! msgs = optionGetAnalyzer ctx | ||
Assert.IsNotEmpty msgs | ||
Assert.IsTrue(Assert.messageContains messageString msgs[0]) | ||
} | ||
|
||
[<Test>] | ||
let ``ValueOption.Value member is detected`` () = | ||
async { | ||
let source = | ||
""" | ||
module M | ||
let voption = ValueSome 10 | ||
let value = voption.Value | ||
""" | ||
|
||
let ctx = getContext projectOptions source | ||
let! msgs = optionGetAnalyzer ctx | ||
Assert.IsNotEmpty msgs | ||
Assert.IsTrue(Assert.messageContains messageString msgs[0]) | ||
} |
32 changes: 0 additions & 32 deletions
32
tests/Ionide.Analyzers.Tests/Suggestion/ReplaceOptionGetWithGracefulHandlingAnalyzerTests.fs
This file was deleted.
Oops, something went wrong.