Skip to content

Commit

Permalink
Support Feliz expression with a single child. Fixes #999 (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Aug 18, 2020
1 parent 181f36f commit 3f0e976
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
55 changes: 54 additions & 1 deletion src/Fantomas.Tests/ElmishTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -711,4 +711,57 @@ Html.ul [
Html.li [ Html.strong "Two" ]
Html.li [ Html.em "Three" ]
]
"""
"""

[<Test>]
let ``feliz construct with a single element as child, 999`` () =
formatSourceString false """
let drawer =
Mui.drawer [
// drawer.open' props.IsOpen
drawer.children
[
Html.div [ prop.className classes.toolbar ]
props.Items
|> List.map (fun s ->
Mui.listItem
[
listItem.button true
match state with
| Some t when t = s -> listItem.selected true
| _ -> listItem.selected false
prop.text s
prop.onClick (fun _ ->
s
|> MenuItemClick
|> dispatch)
])
|> Mui.list
]
]
""" { config with SingleArgumentWebMode = true }
|> prepend newline
|> should equal """
let drawer =
Mui.drawer [
// drawer.open' props.IsOpen
drawer.children [
Html.div [
prop.className classes.toolbar
]
props.Items
|> List.map (fun s ->
Mui.listItem [
listItem.button true
match state with
| Some t when t = s -> listItem.selected true
| _ -> listItem.selected false
prop.text s
prop.onClick (fun _ -> s |> MenuItemClick |> dispatch)
])
|> Mui.list
]
]
"""
14 changes: 6 additions & 8 deletions src/Fantomas.Tests/ListTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ let prismCli commando =
""" config
|> should equal """let prismCli commando =
let props =
createObj
[ "component" ==> "pre"
//"className" ==> "language-fsharp"
]
createObj [ "component" ==> "pre"
//"className" ==> "language-fsharp"
]
()
"""
Expand All @@ -230,10 +229,9 @@ let prismCli commando =
""" ({ config with SpaceAroundDelimiter = false })
|> should equal """let prismCli commando =
let props =
createObj
[|"component" ==> "pre"
//"className" ==> "language-fsharp"
|]
createObj [|"component" ==> "pre"
//"className" ==> "language-fsharp"
|]
()
"""
Expand Down
4 changes: 3 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ and genExpr astContext synExpr =
let genChildren =
ifElse isArray sepOpenA sepOpenL
+> col sepSemi children (genExpr astContext)
+> enterNodeTokenByName synExpr.Range (if isArray then "BAR_RBRACK" else "RBRACK")
+> ifElse isArray sepCloseA sepCloseL

!- identifier
Expand All @@ -969,7 +970,7 @@ and genExpr astContext synExpr =
!- identifier
+> sepSpace
+> ifElse isArray sepOpenA sepOpenL
+> atCurrentColumn (col sepNln children (genExpr astContext))
+> atCurrentColumn (col sepNln children (genExpr astContext) +> enterNodeTokenByName synExpr.Range (if isArray then "BAR_RBRACK" else "RBRACK"))
+> ifElse isArray sepCloseA sepCloseL

let felizExpression =
Expand All @@ -981,6 +982,7 @@ and genExpr astContext synExpr =
+> col sepNln children (genExpr astContext)
+> unindent
+> sepNln
+> enterNodeTokenByName synExpr.Range (if isArray then "BAR_RBRACK" else "RBRACK")
+> ifElse isArray sepCloseAFixed sepCloseLFixed)

let multilineExpression = ifElse ctx.Config.SingleArgumentWebMode felizExpression elmishExpression
Expand Down
2 changes: 2 additions & 0 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,8 @@ let (|ElmishReactWithoutChildren|_|) e =
| App(OptVar(ident,_,_), [ArrayOrList(isArray, children, _)])
| App(OptVar(ident,_,_), [ArrayOrListOfSeqExpr(isArray, CompExpr(_, Sequentials children))]) ->
Some(ident, isArray, children)
| App(OptVar(ident,_,_), [ArrayOrListOfSeqExpr(isArray, CompExpr(_, singleChild))]) ->
Some(ident, isArray, [singleChild])
| _ ->
None

Expand Down

0 comments on commit 3f0e976

Please sign in to comment.