Skip to content

Commit 3dd6e70

Browse files
committed
NoAsyncRunSynchronouslyInLibrary: ignore obsolete
Methods and functions when applying the rule. Added 2 more tests. Marked non-async parsing methods re-introduced in previous commit with `[<Obsolete>]` attribute.
1 parent b6693ee commit 3dd6e70

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/FSharpLint.Core/Application/Lint.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ module Lint =
510510
return FailedToLoadFile projectFilePath |> LintResult.Failure
511511
}
512512

513+
[<Obsolete "Use asyncLintProject instead, otherwise this synchronous version might cause thread blocking issues; this API will be removed in the future.">]
513514
let lintProject optionalParams projectFilePath toolsPath =
514515
asyncLintProject optionalParams projectFilePath toolsPath |> Async.RunSynchronously
515516

@@ -569,6 +570,7 @@ module Lint =
569570
return FailedToLoadFile solutionFilePath |> LintResult.Failure
570571
}
571572

573+
[<Obsolete "Use asyncLintSolution instead, otherwise this synchronous version might cause thread blocking issues; this API will be removed in the future.">]
572574
let lintSolution optionalParams solutionFilePath toolsPath =
573575
asyncLintSolution optionalParams solutionFilePath toolsPath |> Async.RunSynchronously
574576

@@ -618,6 +620,7 @@ module Lint =
618620
| ParseFile.Failed failure -> return LintResult.Failure(FailedToParseFile failure)
619621
}
620622

623+
[<Obsolete "Use asyncLintSource instead, otherwise this synchronous version might cause thread blocking issues; this API will be removed in the future.">]
621624
let lintSource optionalParams source =
622625
asyncLintSource optionalParams source |> Async.RunSynchronously
623626

@@ -669,6 +672,7 @@ module Lint =
669672
return FailedToLoadFile filePath |> LintResult.Failure
670673
}
671674

675+
[<Obsolete "Use asyncLintFile instead, otherwise this synchronous version might cause thread blocking issues; this API will be removed in the future.">]
672676
let lintFile optionalParams filePath =
673677
asyncLintFile optionalParams filePath |> Async.RunSynchronously
674678

@@ -714,5 +718,6 @@ module Lint =
714718
return LintResult.Failure (RunTimeConfigError err)
715719
}
716720

721+
[<Obsolete "Use asyncLintFiles instead, otherwise this synchronous version might cause thread blocking issues; this API will be removed in the future.">]
717722
let lintFiles optionalParams filePaths =
718723
asyncLintFiles optionalParams filePaths |> Async.RunSynchronously

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,32 @@ let isInTheSameModuleAsTest (nodes: array<AbstractSyntaxArray.Node>) (maybeProje
7777
| None ->
7878
nodes |> Array.exists (fun node -> isTestMethodOrClass node.Actual)
7979

80+
let isInObsoleteMethodOrFunction parents =
81+
let isObsolete node =
82+
match node with
83+
| AstNode.MemberDefinition(SynMemberDefn.Member(SynBinding(_, _, _, _, attributes, _, _, _, _, _, _, _, _), _)) ->
84+
attributes
85+
|> extractAttributeNames
86+
|> Seq.contains "Obsolete"
87+
| AstNode.Binding(SynBinding(_, _, _, _, attributes, _, _, _, _, _, _, _, _)) ->
88+
attributes
89+
|> extractAttributeNames
90+
|> Seq.contains "Obsolete"
91+
| _ -> false
92+
93+
parents |> List.exists isObsolete
94+
8095
let checkIfInLibrary (args: AstNodeRuleParams) (range: range) : array<WarningDetails> =
8196
let ruleNotApplicable =
8297
match args.CheckInfo with
8398
| Some checkFileResults ->
8499
hasEntryPoint checkFileResults args.ProjectCheckInfo
85100
|| isInTestProject checkFileResults
101+
|| isInObsoleteMethodOrFunction (args.GetParents args.NodeIndex)
86102
|| isInTheSameModuleAsTest args.SyntaxArray args.ProjectCheckInfo
87103
| None ->
88-
isInTheSameModuleAsTest args.SyntaxArray args.ProjectCheckInfo
104+
isInObsoleteMethodOrFunction (args.GetParents args.NodeIndex)
105+
|| isInTheSameModuleAsTest args.SyntaxArray args.ProjectCheckInfo
89106

90107
if ruleNotApplicable then
91108
Array.empty

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,32 @@ type FooTest () =
101101
()""")
102102

103103
this.AssertNoWarnings()
104+
105+
[<Test>]
106+
member this.``Async.RunSynchronously may be used in methods with Obsolete attribute``() =
107+
this.Parse("""
108+
module Program
109+
110+
type FooTest () =
111+
[<Obsolete>]
112+
member this.Foo() =
113+
async {
114+
return ()
115+
}
116+
|> Async.RunSynchronously""")
117+
118+
this.AssertNoWarnings()
119+
120+
[<Test>]
121+
member this.``Async.RunSynchronously may be used in functions with Obsolete attribute``() =
122+
this.Parse("""
123+
module Program
124+
125+
[<Obsolete>]
126+
let Foo() =
127+
async {
128+
return ()
129+
}
130+
|> Async.RunSynchronously""")
131+
132+
this.AssertNoWarnings()

0 commit comments

Comments
 (0)