Skip to content

Commit

Permalink
Take amount of lambda parameters into account when finding the lambda…
Browse files Browse the repository at this point in the history
… body. Fixes fsprojects#1782. (fsprojects#1783)
  • Loading branch information
nojaf committed Jun 21, 2021
1 parent b32ef14 commit d9df0c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/Fantomas.Tests/LambdaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,20 @@ module Lifecycle =
reg.Consume User.handleGetSessionUserIdRequest)
}
"""

[<Test>]
let ``return lambda from lambda, 1782`` () =
formatSourceString
false
"""
let x =
fun _ ->
fun _ -> "hello"
"""
config
|> prepend newline
|> should
equal
"""
let x = fun _ -> fun _ -> "hello"
"""
17 changes: 11 additions & 6 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,14 +1246,19 @@ let (|Clause|) (SynMatchClause.Clause (p, eo, e, _, _)) = (p, e, eo)
let (|Lambda|_|) =
function
| SynExpr.Lambda (_, _, _, _, Some (pats, body), range) ->
let maxDepth = List.length pats
// find the body expression from the last lambda
let rec visit (e: SynExpr) : SynExpr =
match e with
| SynExpr.Match (matchSeqPoint = NoDebugPointAtInvisibleBinding; clauses = [ Clause (_, expr, _) ])
| SynExpr.Lambda (_, _, _, SynExpr.Match(clauses = [ Clause (_, expr, _) ]), _, _) -> visit expr
| _ -> e
let rec visit (currentDepth: int) (e: SynExpr) : SynExpr =
if currentDepth < maxDepth then
match e with
| SynExpr.Match (matchSeqPoint = NoDebugPointAtInvisibleBinding; clauses = [ Clause (_, expr, _) ])
| SynExpr.Lambda (_, _, _, SynExpr.Match(clauses = [ Clause (_, expr, _) ]), _, _) ->
visit (currentDepth + 1) expr
| _ -> e
else
e

Some(pats, visit body, range)
Some(pats, visit 0 body, range)
| _ -> None

// Type definitions
Expand Down

0 comments on commit d9df0c8

Please sign in to comment.