Skip to content

Commit d1bb738

Browse files
authored
Merge pull request #249 from ionide/246-option-analyzer-editor-context
Add EditorContext to OptionAnalyzer
2 parents 4e004b4 + 3a175f8 commit d1bb738

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

samples/OptionAnalyzer/Library.fs

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,47 @@ let notUsed () =
99
let option: Option<int> = None
1010
option.Value
1111

12-
[<CliAnalyzer "OptionAnalyzer">]
13-
let optionValueAnalyzer: Analyzer<CliContext> =
14-
fun ctx ->
15-
async {
16-
let state = ResizeArray<range>()
12+
let handler typeTree =
13+
async {
14+
let state = ResizeArray<range>()
1715

18-
let walker =
19-
{ new TypedTreeCollectorBase() with
20-
override _.WalkCall _ m _ _ _ range =
21-
let name = String.Join(".", m.DeclaringEntity.Value.FullName, m.DisplayName)
16+
let walker =
17+
{ new TypedTreeCollectorBase() with
18+
override _.WalkCall _ m _ _ _ range =
19+
m.DeclaringEntity
20+
|> Option.iter (fun de ->
21+
let name = String.Join(".", de.FullName, m.DisplayName)
2222

2323
if name = "Microsoft.FSharp.Core.FSharpOption`1.Value" then
2424
state.Add range
25-
25+
)
26+
27+
}
28+
29+
match typeTree with
30+
| None -> ()
31+
| Some typedTree -> walkTast walker typedTree
32+
33+
return
34+
state
35+
|> Seq.map (fun r ->
36+
{
37+
Type = "Option.Value analyzer"
38+
Message = "Option.Value shouldn't be used"
39+
Code = "OV001"
40+
Severity = Severity.Warning
41+
Range = r
42+
Fixes = []
2643
}
44+
)
45+
|> Seq.toList
46+
}
47+
2748

28-
match ctx.TypedTree with
29-
| None -> ()
30-
| Some typedTree -> walkTast walker typedTree
31-
32-
return
33-
state
34-
|> Seq.map (fun r ->
35-
{
36-
Type = "Option.Value analyzer"
37-
Message = "Option.Value shouldn't be used"
38-
Code = "OV001"
39-
Severity = Severity.Warning
40-
Range = r
41-
Fixes = []
42-
}
43-
)
44-
|> Seq.toList
45-
}
49+
[<EditorAnalyzerAttribute "OptionAnalyzer">]
50+
let analyzerEditorContext : Analyzer<EditorContext> =
51+
fun ctx -> handler ctx.TypedTree
52+
53+
[<CliAnalyzer "OptionAnalyzer">]
54+
let optionValueAnalyzer: Analyzer<CliContext> =
55+
fun ctx -> handler ctx.TypedTree

0 commit comments

Comments
 (0)