Skip to content

Commit

Permalink
Print trivia of brackets in ElmishReactWithChildren. Fixes fsprojects…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 5, 2021
1 parent 7adf48f commit 0ccda39
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 16 deletions.
116 changes: 116 additions & 0 deletions src/Fantomas.Tests/ElmishTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,3 +1324,119 @@ let Dashboard () =
]
]
"""

[<Test>]
let ``block comment in elmish expression with two lists, 1601`` () =
formatSourceString
false
"""
module CapitalGuardian.App
open Fable.Core.JsInterop
open Fable.React
open Feliz
[<ReactComponent()>]
let private App () =
div [] [
str "meh 2000k"
(*
{small && <Navigation />}
<Container>
{!small && <Header />}
{!small && <Navigation />}
{routeResult || <NotFoundPage />}
</Container>
<ToastContainer />
*)
]
exportDefault App
"""
config
|> prepend newline
|> should
equal
"""
module CapitalGuardian.App
open Fable.Core.JsInterop
open Fable.React
open Feliz
[<ReactComponent>]
let private App () =
div [] [
str "meh 2000k"
(*
{small && <Navigation />}
<Container>
{!small && <Header />}
{!small && <Navigation />}
{routeResult || <NotFoundPage />}
</Container>
<ToastContainer />
*)
]
exportDefault App
"""

[<Test>]
let ``block comment in elmish expression with two lists, two children`` () =
formatSourceString
false
"""
module CapitalGuardian.App
open Fable.Core.JsInterop
open Fable.React
open Feliz
[<ReactComponent()>]
let private App () =
div [] [
str "meh 2000k"
str "other meh"
(*
{small && <Navigation />}
<Container>
{!small && <Header />}
{!small && <Navigation />}
{routeResult || <NotFoundPage />}
</Container>
<ToastContainer />
*)
]
exportDefault App
"""
config
|> prepend newline
|> should
equal
"""
module CapitalGuardian.App
open Fable.Core.JsInterop
open Fable.React
open Feliz
[<ReactComponent>]
let private App () =
div [] [
str "meh 2000k"
str "other meh"
(*
{small && <Navigation />}
<Container>
{!small && <Header />}
{!small && <Navigation />}
{routeResult || <NotFoundPage />}
</Container>
<ToastContainer />
*)
]
exportDefault App
"""
38 changes: 22 additions & 16 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,20 @@ and genExpr astContext synExpr ctx =
(not ctx.Config.DisableElmishSyntax)
->
let genChildren isShort =
let tokenSize = if isArray then 2 else 1

let openingTokenRange, openTokenType =
ctx.MkRangeWith
(childrenRange.Start.Line, childrenRange.Start.Column)
(childrenRange.Start.Line, (childrenRange.Start.Column + tokenSize)),
(if isArray then LBRACK_BAR else LBRACK)

let closingTokenRange, closingTokenType =
ctx.MkRangeWith
(childrenRange.End.Line, (childrenRange.End.Column - tokenSize))
(childrenRange.End.Line, childrenRange.End.Column),
(if isArray then BAR_RBRACK else RBRACK)

match children with
| [] when (not isArray) ->
sepOpenLFixed
Expand All @@ -1088,39 +1102,31 @@ and genExpr astContext synExpr ctx =
| [] when isArray -> sepOpenAFixed +> sepCloseAFixed
| [ singleChild ] ->
if isShort then
ifElse isArray sepOpenA sepOpenL
tokN openingTokenRange openTokenType (ifElse isArray sepOpenA sepOpenL)
+> genExpr astContext singleChild
+> ifElse
isArray
sepCloseA
(sepCloseL
+> leaveNodeTokenByName childrenRange RBRACK)
+> tokN closingTokenRange closingTokenType (ifElse isArray sepCloseA sepCloseL)
else
ifElse isArray sepOpenA sepOpenL
tokN openingTokenRange openTokenType (ifElse isArray sepOpenA sepOpenL)
+> indent
+> sepNln
+> genExpr astContext singleChild
+> unindent
+> sepNln
+> ifElse
isArray
sepCloseAFixed
(sepCloseLFixed
+> leaveNodeTokenByName childrenRange RBRACK)
+> tokN closingTokenRange closingTokenType (ifElse isArray sepCloseAFixed sepCloseLFixed)

| children ->
if isShort then
ifElse isArray sepOpenA sepOpenL
tokN openingTokenRange openTokenType (ifElse isArray sepOpenA sepOpenL)
+> col sepSemi children (genExpr astContext)
+> ifElse isArray sepCloseA sepCloseL
+> tokN closingTokenRange closingTokenType (ifElse isArray sepCloseA sepCloseL)
else
ifElse isArray sepOpenA sepOpenL
tokN openingTokenRange openTokenType (ifElse isArray sepOpenA sepOpenL)
+> indent
+> sepNln
+> col sepNln children (genExpr astContext)
+> unindent
+> sepNln
+> ifElse isArray sepCloseAFixed sepCloseLFixed
+> tokN closingTokenRange closingTokenType (ifElse isArray sepCloseAFixed sepCloseLFixed)

let shortExpression =
!-identifier
Expand Down

0 comments on commit 0ccda39

Please sign in to comment.