Skip to content

Commit 43fa62a

Browse files
committed
NoAsyncRunSynchronouslyInLibrary: check for test
Check if Async.RunSynchronously call is iniside NUnit or MSTest tests. If it is, don't trigger the rule.
1 parent 2082a35 commit 43fa62a

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

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

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,53 @@ open FSharpLint.Framework.Ast
99
open FSharpLint.Framework.Rules
1010
open FSharpLint.Framework.Utilities
1111

12-
let hasEntryPointAttribute (syntaxArray: array<AbstractSyntaxArray.Node>) =
13-
let hasEntryPoint (attrs: SynAttributeList) =
14-
attrs.Attributes
15-
|> List.exists
16-
(fun attr ->
17-
match attr.TypeName with
18-
| SynLongIdent([ident], _, _) -> ident.idText = "EntryPoint"
19-
| _ -> false)
12+
let extractAttributeNames (attributes: SynAttributes) =
13+
seq {
14+
for attr in extractAttributes attributes do
15+
match attr.TypeName with
16+
| SynLongIdent([ident], _, _) -> yield ident.idText
17+
| _ -> ()
18+
}
2019

20+
let hasEntryPointAttribute (syntaxArray: array<AbstractSyntaxArray.Node>) =
2121
syntaxArray
2222
|> Array.exists
2323
(fun node ->
2424
match node.Actual with
2525
| AstNode.Binding(SynBinding(_, _, _, _, attributes, _, _, _, _, _, _, _, _)) ->
26-
attributes |> List.exists hasEntryPoint
26+
attributes
27+
|> extractAttributeNames
28+
|> Seq.contains "EntryPoint"
2729
| _ -> false)
2830

29-
let checkIfInLibrary (syntaxArray: array<AbstractSyntaxArray.Node>) (checkInfo: option<FSharpCheckFileResults>) (range: range) : array<WarningDetails> =
31+
let testMethodAttributes = [ "Test"; "TestMethod" ]
32+
let testClassAttributes = [ "TestFixture"; "TestClass" ]
33+
34+
let isInsideTest (parents: list<AstNode>) =
35+
let isTestMethodOrClass node =
36+
match node with
37+
| AstNode.MemberDefinition(SynMemberDefn.Member(SynBinding(_, _, _, _, attributes, _, _, _, _, _, _, _, _), _)) ->
38+
attributes
39+
|> extractAttributeNames
40+
|> Seq.exists (fun name -> testMethodAttributes |> List.contains name)
41+
| AstNode.TypeDefinition(SynTypeDefn.SynTypeDefn(SynComponentInfo(attributes, _, _, _, _, _, _, _), _, _, _, _, _)) ->
42+
attributes
43+
|> extractAttributeNames
44+
|> Seq.exists (fun name -> testClassAttributes |> List.contains name)
45+
| _ -> false
46+
47+
parents |> List.exists isTestMethodOrClass
48+
49+
let checkIfInLibrary (args: AstNodeRuleParams) (range: range) : array<WarningDetails> =
3050
let isInTestAssembly =
31-
match checkInfo with
51+
match args.CheckInfo with
3252
| Some checkFileResults ->
3353
match Seq.tryHead checkFileResults.PartialAssemblySignature.Entities with
3454
| Some entity -> entity.Assembly.QualifiedName.ToLowerInvariant().Contains "test"
3555
| None -> false
3656
| None -> false
3757

38-
if isInTestAssembly || hasEntryPointAttribute syntaxArray then
58+
if isInTestAssembly || isInsideTest (args.GetParents args.NodeIndex) || hasEntryPointAttribute args.SyntaxArray then
3959
Array.empty
4060
else
4161
Array.singleton
@@ -49,7 +69,7 @@ let checkIfInLibrary (syntaxArray: array<AbstractSyntaxArray.Node>) (checkInfo:
4969
let runner args =
5070
match args.AstNode with
5171
| AstNode.Identifier(["Async"; "RunSynchronously"], range) ->
52-
checkIfInLibrary args.SyntaxArray args.CheckInfo range
72+
checkIfInLibrary args range
5373
| _ -> Array.empty
5474

5575
let rule =

0 commit comments

Comments
 (0)