Skip to content

Commit

Permalink
Fix printing of fragments inside JSX props (#2463)
Browse files Browse the repository at this point in the history
* Fix printing of fragments inside JSX props

* Also fix pathological case for optional labeled args

* add changes in new lexer file
  • Loading branch information
anmonteiro authored and jordwalke committed Oct 29, 2019
1 parent 782aed1 commit 5441cd4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions formatTest/unit_tests/expected_output/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,7 @@ let v =
}),
}
/>;

<A someProp={<> {React.string("Hello")} </>} />;

<A someProp=?{<> {React.string("Hi")} </>} />;
4 changes: 4 additions & 0 deletions formatTest/unit_tests/input/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,7 @@ let v =
name: x |> Option.map(x => {let y = x; y ++ y})
}}
/>;

<A someProp={ <> {React.string("Hello")} </> } />;

<A someProp=?{ <> {React.string("Hi")} </> } />;
8 changes: 8 additions & 0 deletions src/reason-parser/reason_declarative_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,14 @@ rule token state = parse
set_lexeme_length lexbuf 1;
LBRACE
}
| "{<>" {
set_lexeme_length lexbuf 1;
LBRACE
}
| "{<>}" {
set_lexeme_length lexbuf 2;
LBRACELESS
}
| "</" blank* ((uppercase_or_lowercase (identchar|'.')* ) as tag) blank* ">"
{ LESSSLASHIDENTGREATER tag }
| "]" { RBRACKET }
Expand Down
13 changes: 12 additions & 1 deletion src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4439,17 +4439,24 @@ let printer = object(self:'self)
in
processArguments tail processedAttrs (Some [dotdotdotChild])
| (Optional lbl, expression) :: tail ->
let {jsxAttrs; _} = partitionAttributes expression.pexp_attributes in
let value_has_jsx = jsxAttrs != [] in
let nextAttr =
match expression.pexp_desc with
| Pexp_ident ident when isPunnedJsxArg lbl ident ->
makeList ~break:Layout.Never [atom "?"; atom lbl]
| Pexp_construct _ when value_has_jsx ->
label
(makeList ~break:Layout.Never [atom lbl; atom "=?"])
(self#inline_braces#simplifyUnparseExpr ~wrap:("{","}") expression)
| _ ->
label
(makeList ~break:Layout.Never [atom lbl; atom "=?"])
(self#dont_preserve_braces#simplifyUnparseExpr ~wrap:("{","}") expression) in
processArguments tail (nextAttr :: processedAttrs) children
| (Labelled lbl, expression) :: tail ->
let {jsxAttrs; _} = partitionAttributes expression.pexp_attributes in
let value_has_jsx = jsxAttrs != [] in
let nextAttr =
match expression.pexp_desc with
| Pexp_ident ident when isPunnedJsxArg lbl ident -> atom lbl
Expand Down Expand Up @@ -4486,6 +4493,10 @@ let printer = object(self:'self)
| Infix str when requireNoSpaceFor str -> self#unparseExpr expression
| _ -> self#dont_preserve_braces#simplifyUnparseExpr ~wrap:("{","}") expression)
in label lhs rhs
| Pexp_construct _ when value_has_jsx ->
label
(makeList [atom lbl; atom "="])
(self#inline_braces#simplifyUnparseExpr ~wrap:("{","}") expression)
| Pexp_record _
| Pexp_construct _
| Pexp_array _
Expand Down

0 comments on commit 5441cd4

Please sign in to comment.