Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Feliz expression with a single child #1004

Merged
merged 2 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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