Skip to content

Commit

Permalink
Don't visit operator in SynExpr.Quote, fixes 2535 (#2560)
Browse files Browse the repository at this point in the history
* Don't visit operator in SynExpr.Quote, fixes 2535

* Use visit directly for SynExpr.Quote.

Co-authored-by: nojaf <[email protected]>
  • Loading branch information
dawedawe and nojaf authored Oct 7, 2022
1 parent 14d2019 commit 243dc23
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
* Function lambda with type annotation breaks code. [#2295](https://github.com/fsprojects/fantomas/issues/2295)
* Incorrect scope chosen for trivia, hence comments lost. [#2535](https://github.com/fsprojects/fantomas/issues/2535)

## [5.0.4] - 2022-10-04

Expand Down
41 changes: 41 additions & 0 deletions src/Fantomas.Core.Tests/QuotationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,44 @@ let action =
}
@>
"""

[<Test>]
let ``should preserve comments in quotation, 2535`` () =
formatSourceString
false
"""
test
<@
result
.Replace('\r', '\u00FF')
.Replace('\n', '\u00FF')
.Replace("\u00FF\u00FF", "\u00FF")
.Replace("8.12", "8.13") // CRAP score rounding
.Replace("4.12", "4.13") // CRAP score rounding
.Trim([| '\u00FF' |]) = expected
.Replace('\r', '\u00FF')
.Replace('\n', '\u00FF')
.Replace("\u00FF\u00FF", "\u00FF")
.Trim([| '\u00FF' |])
@>
"""
config
|> prepend newline
|> should
equal
"""
test
<@
result
.Replace('\r', '\u00FF')
.Replace('\n', '\u00FF')
.Replace("\u00FF\u00FF", "\u00FF")
.Replace("8.12", "8.13") // CRAP score rounding
.Replace("4.12", "4.13") // CRAP score rounding
.Trim([| '\u00FF' |]) = expected
.Replace('\r', '\u00FF')
.Replace('\n', '\u00FF')
.Replace("\u00FF\u00FF", "\u00FF")
.Trim([| '\u00FF' |])
@>
"""
11 changes: 5 additions & 6 deletions src/Fantomas.Core/AstTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,11 @@ and visitSynExpr (synExpr: SynExpr) : TriviaNode list =
yield! Option.toList (mkNodeOption SynExpr_Paren_ClosingParenthesis rpr) |]
|> List.singleton
|> finalContinuation)
| SynExpr.Quote (operator, _, quotedSynExpr, _, range) ->
let continuations: ((TriviaNode list -> TriviaNode list) -> TriviaNode list) list =
[ visit operator; visit quotedSynExpr ]

processSequence finalContinuation continuations (fun nodes ->
mkSynExprNode SynExpr_Quote synExpr range (sortChildren [| yield! nodes |]))
| SynExpr.Quote (_, _, quotedSynExpr, _, range) ->
visit quotedSynExpr (fun nodes ->
mkSynExprNode SynExpr_Quote synExpr range (sortChildren [| yield! nodes |])
|> List.singleton
|> finalContinuation)
| SynExpr.Const (constant, range) ->
mkSynExprNode SynExpr_Const synExpr range [| visitSynConst range constant |]
|> List.singleton
Expand Down

0 comments on commit 243dc23

Please sign in to comment.