Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AvoidSinglePipeOperator false negative in all inner expressions #697

Merged
merged 7 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/FSharpLint.Core/Rules/Conventions/AvoidSinglePipeOperator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ let runner (args: AstNodeRuleParams) =
TypeChecks = List.Empty
} |> Array.singleton

let checkExpr (expr: SynExpr) =
let rec checkExpr (expr: SynExpr) (parentList: AstNode list): WarningDetails array =
let checkParentPiped (expr: AstNode) =
match expr with
| AstNode.Expression(SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, _argExpr, _range)) ->
checkExpr funcExpr [] |> Seq.isEmpty
| _ -> false

match expr with
| SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, argExpr, _range) ->
match funcExpr with
Expand All @@ -29,7 +35,14 @@ let runner (args: AstNodeRuleParams) =
| SynExpr.IfThenElse _ ->
Array.empty
| _ ->
errors ident.idRange
let isParentPiped =
match parentList with
| head::_ -> checkParentPiped head
| [] -> false
if isParentPiped then
Array.empty
else
errors ident.idRange
else
Array.empty
| _ ->
Expand All @@ -39,14 +52,8 @@ let runner (args: AstNodeRuleParams) =

let error =
match args.AstNode with
| AstNode.Binding (SynBinding(_synAcc, _synBinding, _mustInline, _isMut, _synAttribs, _preXmlDoc, _synValData, _headPat, _synBindingRet, synExpr, _range, _debugPointAtBinding, _)) ->
match synExpr with
| SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, _argExpr, _range) ->
checkExpr funcExpr
| SynExpr.IfThenElse(_, SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, _argExpr, _range), _, _, _, _, _) ->
checkExpr funcExpr
| _ ->
Array.empty
| AstNode.Expression(SynExpr.App(_exprAtomicFlag, _isInfix, funcExpr, _argExpr, _range)) ->
checkExpr funcExpr (args.GetParents args.NodeIndex)
| _ ->
Array.empty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ let foo param =
Assert.True this.NoErrorsExist

[<Test>]
member this.``Use pipe operator once on record``() =
member this.``Use pipe operator once on record within a if statement``() =
this.Parse """
type Person =
{
Expand All @@ -123,7 +123,7 @@ let someFunc someParam =
Assert.IsTrue this.ErrorsExist

[<Test>]
member this.``Use pipe operator twice on record``() =
member this.``Use pipe operator twice on record within a if statement``() =
this.Parse """
type Person =
{
Expand All @@ -140,3 +140,62 @@ let someFunc someParam =
"""

Assert.IsTrue this.NoErrorsExist

[<Test>]
member this.``Use pipe operator once inside of an array``() =
this.Parse """
let someFunc () =
[| "Foo" |> String.replicate 2 |]
"""

Assert.IsTrue this.ErrorsExist

[<Test>]
member this.``Use pipe operator once on record within a nested if statement``() =
this.Parse """
type Person =
{
FirstName: string
}

let someFunc someParam barParam =
if someParam then
if barParam then
{ FirstName = "Bar" } |> someOtherFunc
else
Array.empty
else
Array.empty
"""

Assert.IsTrue this.ErrorsExist

[<Test>]
member this.``Use pipe operator once without binding``() =
this.Parse """
module Foo

-1.0 |> printf "%d"
"""

Assert.IsTrue this.ErrorsExist

[<Test>]
member this.``Use pipe operator twice without binding``() =
this.Parse """
module Foo

-1.0 |> printf "%d" |> ignore
"""

Assert.IsTrue this.NoErrorsExist

[<Test>]
member this.``Use pipe operator thrice without binding``() =
this.Parse """
module Foo

-1.0 |> printf "%d" |> ignore |> someOtherFunc
"""

Assert.IsTrue this.NoErrorsExist
Loading