Skip to content

Commit 36609bd

Browse files
committed
Core(Tests): add NoAsyncRunSynchronouslyInLibrary
Rule and tests for it.
1 parent 21f3570 commit 36609bd

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

src/FSharpLint.Core/FSharpLint.Core.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="Rules\Smells\RaiseWithTooManyArguments\InvalidArgWithTwoArguments.fs" />
7070
<Compile Include="Rules\Smells\RaiseWithTooManyArguments\FailwithfWithArgumentsMatchingFormatString.fs" />
7171
<Compile Include="Rules\Conventions\FailwithBadUsage.fs" />
72+
<Compile Include="Rules\Conventions\NoAsyncRunSynchronouslyInLibrary.fs" />
7273
<Compile Include="Rules\Conventions\SourceLength\SourceLengthHelper.fs" />
7374
<Compile Include="Rules\Conventions\SourceLength\MaxLinesInLambdaFunction.fs" />
7475
<Compile Include="Rules\Conventions\SourceLength\MaxLinesInMatchLambdaFunction.fs" />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module FSharpLint.Rules.NoAsyncRunSynchronouslyInLibrary
2+
3+
open FSharp.Compiler.Syntax
4+
open FSharp.Compiler.Text
5+
open FSharpLint.Framework
6+
open FSharpLint.Framework.Suggestion
7+
open FSharpLint.Framework.Ast
8+
open FSharpLint.Framework.Rules
9+
open FSharpLint.Framework.Utilities
10+
11+
let checkIfInLibrary (syntaxArray: array<AbstractSyntaxArray.Node>) (range: range) : array<WarningDetails> =
12+
failwith "Not implemented"
13+
14+
let runner args =
15+
match args.AstNode with
16+
| AstNode.Identifier(["Async"; "RunSynchronously"], range) ->
17+
checkIfInLibrary args.SyntaxArray range
18+
| _ -> Array.empty
19+
20+
let rule =
21+
AstNodeRule
22+
{
23+
Name = "NoAsyncRunSynchronouslyInLibrary"
24+
Identifier = Identifiers.NoAsyncRunSynchronouslyInLibrary
25+
RuleConfig =
26+
{
27+
AstNodeRuleConfig.Runner = runner
28+
Cleanup = ignore
29+
}
30+
}

src/FSharpLint.Core/Rules/Identifiers.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,4 @@ let FavourNonMutablePropertyInitialization = identifier 84
9292
let EnsureTailCallDiagnosticsInRecursiveFunctions = identifier 85
9393
let FavourAsKeyword = identifier 86
9494
let InterpolatedStringWithNoSubstitution = identifier 87
95+
let NoAsyncRunSynchronouslyInLibrary = identifier 88

tests/FSharpLint.Core.Tests/FSharpLint.Core.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Compile Include="Rules\Conventions\UsedUnderscorePrefixedElements.fs" />
4848
<Compile Include="Rules\Conventions\FavourNonMutablePropertyInitialization.fs" />
4949
<Compile Include="Rules\Conventions\EnsureTailCallDiagnosticsInRecursiveFunctions.fs" />
50+
<Compile Include="Rules\Conventions\NoAsyncRunSynchronouslyInLibrary.fs" />
5051
<Compile Include="Rules\Conventions\Naming\NamingHelpers.fs" />
5152
<Compile Include="Rules\Conventions\Naming\InterfaceNames.fs" />
5253
<Compile Include="Rules\Conventions\Naming\ExceptionNames.fs" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module FSharpLint.Core.Tests.Rules.Conventions.NoAsyncRunSynchronouslyInLibrary
2+
3+
open NUnit.Framework
4+
open FSharpLint.Framework.Rules
5+
open FSharpLint.Rules
6+
7+
[<TestFixture>]
8+
type TestNoAsyncRunSynchronouslyInLibrary() =
9+
inherit FSharpLint.Core.Tests.TestAstNodeRuleBase.TestAstNodeRuleBase(NoAsyncRunSynchronouslyInLibrary.rule)
10+
11+
[<Test>]
12+
member this.``Async.RunSynchronously should not be used in library code``() =
13+
this.Parse("""
14+
module Program
15+
16+
async {
17+
return ()
18+
}
19+
|> Async.RunSynchronously""")
20+
21+
Assert.IsTrue this.ErrorsExist
22+
23+
[<Test>]
24+
member this.``Async.RunSynchronously may be used in code that declares entry point``() =
25+
this.Parse("""
26+
module Program
27+
28+
[<EntryPoint>]
29+
let main () =
30+
async {
31+
return ()
32+
}
33+
|> Async.RunSynchronously""")
34+
35+
this.AssertNoWarnings()

0 commit comments

Comments
 (0)