Skip to content

Commit

Permalink
Add parenthesis around multi line if/then/else before infix. Fixes #1327
Browse files Browse the repository at this point in the history
. (#1328)
  • Loading branch information
nojaf committed Dec 23, 2020
1 parent 01a3162 commit 6695a60
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Fantomas.Tests/DynamicOperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ let ``keep () when dynamic operator inside boolean expr, #476`` () =
equal
"""
let fieldColor (fieldNameX: string) =
if f.errors?(fieldNameY) && f.touched?(fieldNameZ) then
IsDanger
else
NoColor
(if f.errors?(fieldNameY) && f.touched?(fieldNameZ) then
IsDanger
else
NoColor)
|> Input.Color
"""

Expand Down
32 changes: 32 additions & 0 deletions src/Fantomas.Tests/IfThenElseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,3 +1658,35 @@ let tryDecompile (ty: FSharpEntity) =
|> tryGetSource
}
"""

[<Test>]
let ``if then else followed by pipe, 1327`` () =
formatSourceString
false
"""
module X =
let getValSignature displayContext (v: FSharpMemberOrFunctionOrValue) =
let name =
if v.DisplayName.StartsWith "( "
then v.LogicalName
else v.DisplayName
|> PrettyNaming.QuoteIdentifierIfNeeded
()
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
module X =
let getValSignature displayContext (v: FSharpMemberOrFunctionOrValue) =
let name =
(if v.DisplayName.StartsWith "( " then
v.LogicalName
else
v.DisplayName)
|> PrettyNaming.QuoteIdentifierIfNeeded
()
"""
7 changes: 6 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2769,8 +2769,13 @@ and genMultilineInfixExpr astContext e1 operatorText operatorExpr e2 =
if noBreakInfixOps.Contains(operatorText) then
genOnelinerInfixExpr astContext e1 operatorText operatorExpr e2
else
let genE1 =
match e1 with
| SynExpr.IfThenElse _ -> autoParenthesisIfExpressionExceedsPageWidth (genExpr astContext e1)
| _ -> genExpr astContext e1

atCurrentColumn (
genExpr astContext e1
genE1
+> sepNln
+> genInfixOperator operatorText operatorExpr
+> sepSpace
Expand Down

0 comments on commit 6695a60

Please sign in to comment.