Skip to content

Commit

Permalink
Correct prefix of synbinding in collected computation expression. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 10, 2021
1 parent b333fba commit 4a69a1c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
39 changes: 39 additions & 0 deletions src/Fantomas.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1831,3 +1831,42 @@ type Viewport =
longitude: float
zoom: int }
"""

[<Test>]
let ``recursive let bindings in sequential expression, 1628`` () =
formatSourceString
false
"""
let foobar () =
Console.WriteLine("Hello")
let rec foo () = bar "Hello"
and bar str = printf "%s" str |> ignore
foo ()
let foobar () =
let rec foo () = bar "Hello"
and bar str = printf "%s" str |> ignore
foo ()
"""
config
|> prepend newline
|> should
equal
"""
let foobar () =
Console.WriteLine("Hello")
let rec foo () = bar "Hello"
and bar str = printf "%s" str |> ignore
foo ()
let foobar () =
let rec foo () = bar "Hello"
and bar str = printf "%s" str |> ignore
foo ()
"""
9 changes: 3 additions & 6 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,10 +1426,7 @@ and genExpr astContext synExpr ctx =
| CompExprBody statements ->
let genCompExprStatement astContext ces =
match ces with
| LetOrUseStatement (isRecursive, isUse, binding) ->
let prefix =
sprintf "%s%s" (if isUse then "use " else "let ") (if isRecursive then "rec " else "")

| LetOrUseStatement (prefix, binding) ->
enterNodeFor (synBindingToFsAstType binding) binding.RangeOfBindingAndRhs
+> genLetBinding astContext prefix binding
| LetOrUseBangStatement (isUse, pat, expr, r) ->
Expand All @@ -1448,14 +1445,14 @@ and genExpr astContext synExpr ctx =

let getRangeOfCompExprStatement ces =
match ces with
| LetOrUseStatement (_, _, binding) -> binding.RangeOfBindingAndRhs
| LetOrUseStatement (_, binding) -> binding.RangeOfBindingAndRhs
| LetOrUseBangStatement (_, _, _, r) -> r
| AndBangStatement (_, _, r) -> r
| OtherStatement expr -> expr.Range

let getSepNln ces r =
match ces with
| LetOrUseStatement (_, _, b) ->
| LetOrUseStatement (_, b) ->
sepNlnConsideringTriviaContentBeforeForMainNode (synBindingToFsAstType b) r
| LetOrUseBangStatement _ -> sepNlnConsideringTriviaContentBeforeForMainNode SynExpr_LetOrUseBang r
| AndBangStatement _ -> sepNlnConsideringTriviaContentBeforeForToken AND_BANG r
Expand Down
10 changes: 4 additions & 6 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -906,22 +906,20 @@ let rec (|LetOrUses|_|) =
| _ -> None

type ComputationExpressionStatement =
| LetOrUseStatement of recursive: bool * isUse: bool * SynBinding
| LetOrUseStatement of prefix: string * binding: SynBinding
| LetOrUseBangStatement of isUse: bool * SynPat * SynExpr * range
| AndBangStatement of SynPat * SynExpr * range
| OtherStatement of SynExpr

let rec collectComputationExpressionStatements e : ComputationExpressionStatement list =
match e with
| SynExpr.LetOrUse (isRecursive, isUse, bindings, body, _) ->
let bindings =
bindings
|> List.map (fun b -> LetOrUseStatement(isRecursive, isUse, b))
| LetOrUses (bindings, body) ->
let letBindings = bindings |> List.map LetOrUseStatement

let returnExpr =
collectComputationExpressionStatements body

[ yield! bindings; yield! returnExpr ]
letBindings @ returnExpr
| SynExpr.LetOrUseBang (_, isUse, _, pat, expr, andBangs, body, r) ->
let letOrUseBang =
LetOrUseBangStatement(isUse, pat, expr, r)
Expand Down

0 comments on commit 4a69a1c

Please sign in to comment.