Skip to content

Commit

Permalink
Add parenthesis around lamba in tuple, fixes #450
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jul 16, 2019
1 parent e76f0e3 commit 26db836
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\netfx.props" />
<PropertyGroup>
<Version>3.0.0-beta-001</Version>
Expand Down Expand Up @@ -51,6 +51,7 @@
<Compile Include="SynExprSetTests.fs" />
<Compile Include="SynConstTests.fs" />
<Compile Include="FSharpScriptTests.fs" />
<Compile Include="TupleTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas\Fantomas.fsproj" />
Expand Down
17 changes: 17 additions & 0 deletions src/Fantomas.Tests/TupleTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Fantomas.Tests.TupleTests

open NUnit.Framework
open FsUnit

open Fantomas.Tests.TestHelper

[<Test>]
let ``tuple with lamba should add parenthesis`` () =
formatSourceString false """
let private carouselSample =
FunctionComponent.Of<obj>(fun _ ->
fragment [] []
,"CarouselSample")
""" config
|> should equal """let private carouselSample = FunctionComponent.Of<obj>((fun _ -> fragment [] []), "CarouselSample")
"""
6 changes: 5 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,11 @@ and genAnonRecordFieldName astContext (AnonRecordFieldName(s, e)) =
and genTuple astContext es =
atCurrentColumn (coli sepComma es (fun i ->
if i = 0 then genExpr astContext else noIndentBreakNln astContext
|> addParenWhen (function |ElIf _ -> true |_ -> false) // "if .. then .. else" have precedence over ","
|> addParenWhen (fun e ->
match e with
|ElIf _
| SynExpr.Lambda _ -> true
|_ -> false) // "if .. then .. else" have precedence over ","
))

and genExpr astContext synExpr =
Expand Down

0 comments on commit 26db836

Please sign in to comment.