Skip to content

Commit

Permalink
Change all precedence printing to braces for consistency inside JSX.
Browse files Browse the repository at this point in the history
Before:
<div bar=(foo(x))> (apply(arg)) </div>

After:
<div bar={foo(x)}> {apply(arg)} </div>
  • Loading branch information
Iwan committed Aug 1, 2018
1 parent 8c6c151 commit 77492f8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
14 changes: 7 additions & 7 deletions formatTest/typeCheckedTests/expected_output/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ let selfClosing3 =
a="really long values that should"
b="cause the entire thing to wrap"
/>;
let a = <Foo> <Bar c=(a => a + 2) /> </Foo>;
let a = <Foo> <Bar c={a => a + 2} /> </Foo>;
let a3 = <So> <Much> <Nesting /> </Much> </So>;
let a4 =
<Sibling>
Expand All @@ -212,7 +212,7 @@ let a6 =
<Test yo=1 />
<Text> "another string" </Text>
<Bar />
<Exp> (2 + 4) </Exp>
<Exp> {2 + 4} </Exp>
</Foo2>;
let intended = true;
let punning = <Pun intended />;
Expand Down Expand Up @@ -273,7 +273,7 @@ let fragment8 = <> <Foo /> <Foo /> </>;
let fragment9 = <> 2 2 2 2 </>;
let fragment10 = <> 2.2 3.2 4.6 1.2 </>;
let fragment11 = <> "str" </>;
let fragment12 = <> (6 + 2) (6 + 2) (6 + 2) </>;
let fragment12 = <> {6 + 2} {6 + 2} {6 + 2} </>;
let fragment13 = <> fragment11 fragment11 </>;
let listOfItems1 = <List1> 1 2 3 4 5 </List1>;
let listOfItems2 =
Expand Down Expand Up @@ -475,14 +475,14 @@ fakeRender(defaultArg);
/* children can be at any position */
<span test=true foo=2 />;

<Optional1 required=(Some("hi")) />;
<Optional1 required={Some("hi")} />;

/* preserve some other attributes too! */
([@bla] <span test=true foo=2 />);
([@bla] <span test=true foo=2 />);

([@bla] <Optional1 required=(Some("hi")) />);
([@bla] <Optional1 required=(Some("hi")) />);
([@bla] <Optional1 required={Some("hi")} />);
([@bla] <Optional1 required={Some("hi")} />);

/* Overeager JSX punning #1099 */
module Metal = {
Expand Down Expand Up @@ -531,7 +531,7 @@ let g = <Two ?foo />;
/* https://github.com/facebook/reason/issues/1428 */
<Foo> ...element </Foo>;

<Foo> ...(a => 1) </Foo>;
<Foo> ...{a => 1} </Foo>;

<Foo> ...<Foo2 /> </Foo>;

Expand Down
52 changes: 26 additions & 26 deletions formatTest/unit_tests/expected_output/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ let x =
"justifyContent": CssJustifySpaceBetween,
"flexDirection": CssFlexDirectionRow,
}
onKey=(updater(handleInput))
onKey={updater(handleInput)}
/>;

let y =
<Routes
path=(Routes.stateToPath(state))
path={Routes.stateToPath(state)}
isHistorical=true
onHashChange=(
onHashChange={
(_oldPath, _oldUrl, newUrl) =>
updater(
(latestComponentBag, _) => {
Expand All @@ -39,12 +39,12 @@ let y =
},
(),
)
)
}
/>;

let z =
<div
style=(
style={
ReactDOMRe.Style.make(
~width,
~height,
Expand All @@ -57,8 +57,8 @@ let z =
~someOtherAttribute,
(),
)
)
key=(string_of_int(1))
}
key={string_of_int(1)}
/>;

let omega =
Expand All @@ -74,7 +74,7 @@ let omega =
borderColor,
someOtherAttribute,
]
key=(string_of_int(1))
key={string_of_int(1)}
/>;

let someArray =
Expand All @@ -90,7 +90,7 @@ let someArray =
borderColor,
someOtherAttribute,
|]
key=(string_of_int(1))
key={string_of_int(1)}
/>;

let tuples =
Expand All @@ -107,29 +107,29 @@ let tuples =
someOtherAttribute,
definitelyBreakere,
)
key=(string_of_int(1))
key={string_of_int(1)}
/>;

let icon =
<Icon
name=(
name={
switch (state.volume) {
| v when v < 0.1 => "sound-off"
| v when v < 0.11 => "sound-min"
| v when v < 0.51 => "sound-med"
| _ => "sound-max"
}
)
}
/>;

<MessengerSharedPhotosAlbumViewPhotoReact
ref=?(
ref=?{
foo##bar === baz ?
Some(
foooooooooooooooooooooooo(setRefChild),
) :
None
)
}
key=node##legacy_attachment_id
/>;

Expand Down Expand Up @@ -175,19 +175,19 @@ let y = [
child
</Description>;
<Description
term=(
term={
Text.createElement(
~text="Age",
~children=[],
(),
)
)>
}>
child
</Description>;
<Description
term=(
term={
[@JSX] Text.createElement(~text="Age", ())
)>
}>
child
</Description>;

Expand All @@ -211,22 +211,22 @@ let y = [
}
/>;

<div> <span> (str("hello")) </span> </div>;
<div> <span> {str("hello")} </span> </div>;

<description term={<text text="Age" />}>
child
</description>;

<description
term=(text(~text="Age", ~children=[], ()))>
term={text(~text="Age", ~children=[], ())}>
child
</description>;
<description
term=([@JSX] text(~text="Age", ~children=[]))>
term={[@JSX] text(~text="Age", ~children=[])}>
child
</description>;
<description
term=([@JSX] text(~text="Age", ()))>
term={[@JSX] text(~text="Age", ())}>
child
</description>;

Expand Down Expand Up @@ -258,12 +258,12 @@ let x = foo /></ bar;
</div>;

<div onClick=this##handleClick>
<> (foo(bar)) </>
<> {foo(bar)} </>
</div>;

/* function application */
<div onClick=this##handleClick>
<> (foo(bar)) </>
<> {foo(bar)} </>
</div>;

/* tuple, not function application */
Expand All @@ -279,11 +279,11 @@ let x = foo /></ bar;
<div />;

<Component
accept=(
accept={
fun
| Foo => true
| Bar => false
)
}
/>;

<C
Expand Down
2 changes: 1 addition & 1 deletion formatTest/unit_tests/expected_output/jsx_functor.re
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ module M =
let _ =
Group([
<M(X, Y) />,
<M(X, Y)> (Text("A")) </M>,
<M(X, Y)> {Text("A")} </M>,
<M(X, Y) name="Test" id=10 />,
]);
31 changes: 17 additions & 14 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1144,8 +1144,8 @@ let makeCommaBreakableListSurround opn cls lst =

(* TODO: Allow configuration of spacing around colon symbol *)

let formatPrecedence ?loc formattedTerm =
source_map ?loc (makeList ~wrap:("(", ")") ~break:IfNeed [formattedTerm])
let formatPrecedence ?(wrap=("(", ")")) ?loc formattedTerm =
source_map ?loc (makeList ~wrap ~break:IfNeed [formattedTerm])

let wrap fn term =
ignore (Format.flush_str_formatter ());
Expand Down Expand Up @@ -3424,7 +3424,7 @@ let printer = object(self:'self)
| "Genarray" -> (
match label_exprs with
| [(_,a);(_,{pexp_desc=Pexp_array ls});(_,c)] ->
let formattedList = List.map self#simplifyUnparseExpr ls in
let formattedList = List.map (self#simplifyUnparseExpr ~wrap:("(", ")")) ls in
let lhs = makeList [self#simple_enough_to_be_lhs_dot_send a; atom "."] in
let rhs = makeList ~break:IfNeed ~postSpace:true ~sep:commaSep ~wrap:("{", "}") formattedList
in
Expand Down Expand Up @@ -3589,13 +3589,13 @@ let printer = object(self:'self)
in
source_map ~loc:e.pexp_loc itm
method simplifyUnparseExpr x =
method simplifyUnparseExpr ?(wrap=("(", ")")) x =
match self#unparseExprRecurse x with
| SpecificInfixPrecedence ({reducePrecedence; shiftPrecedence}, itm) ->
formatPrecedence ~loc:x.pexp_loc (self#unparseResolvedRule itm)
formatPrecedence ~wrap ~loc:x.pexp_loc (self#unparseResolvedRule itm)
| FunctionApplication itms ->
formatPrecedence ~loc:x.pexp_loc (formatAttachmentApplication applicationFinalWrapping None (itms, Some x.pexp_loc))
| PotentiallyLowPrecedence itm -> formatPrecedence ~loc:x.pexp_loc itm
formatPrecedence ~wrap ~loc:x.pexp_loc (formatAttachmentApplication applicationFinalWrapping None (itms, Some x.pexp_loc))
| PotentiallyLowPrecedence itm -> formatPrecedence ~wrap ~loc:x.pexp_loc itm
| Simple itm -> itm
Expand Down Expand Up @@ -4273,7 +4273,7 @@ let printer = object(self:'self)
| (Labelled "children", {pexp_desc = Pexp_construct ({txt = Lident"::"}, Some {pexp_desc = Pexp_tuple components} )}) :: tail ->
processArguments tail processedAttrs (self#formatChildren components [])
| (Labelled "children", expr) :: tail ->
let childLayout = self#simplifyUnparseExpr expr in
let childLayout = self#simplifyUnparseExpr ~wrap:("{", "}") expr in
let dotdotdotChild = makeList ~break:Layout.Never [atom "..."; childLayout] in
processArguments tail processedAttrs (Some [dotdotdotChild])
| (Optional lbl, expression) :: tail ->
Expand All @@ -4282,7 +4282,7 @@ let printer = object(self:'self)
| Pexp_ident ident when isPunnedJsxArg lbl ident ->
makeList ~break:Layout.Never [atom "?"; atom lbl]
| _ ->
label (makeList ~break:Layout.Never [atom lbl; atom "=?"]) (self#simplifyUnparseExpr expression) in
label (makeList ~break:Layout.Never [atom lbl; atom "=?"]) (self#simplifyUnparseExpr ~wrap:("{","}") expression) in
processArguments tail (nextAttr :: processedAttrs) children
| (Labelled lbl, expression) :: tail ->
Expand All @@ -4302,7 +4302,7 @@ let printer = object(self:'self)
let lhs = (makeList [atom lbl; atom "="]) in
let rhs = (match printedStringAndFixityExpr eFun with
| Infix str when requireNoSpaceFor str -> self#unparseExpr expression
| _ -> self#simplifyUnparseExpr expression)
| _ -> self#simplifyUnparseExpr ~wrap:("{","}") expression)
in label lhs rhs
| Pexp_record _
| Pexp_construct _
Expand All @@ -4311,8 +4311,11 @@ let printer = object(self:'self)
| Pexp_match _
| Pexp_extension _
| Pexp_fun _
| Pexp_function _ -> label (makeList [atom lbl; atom "="]) (self#simplifyUnparseExpr expression)
| _ -> makeList ([atom lbl; atom "="; self#simplifyUnparseExpr expression])
| Pexp_function _ ->
label
(makeList [atom lbl; atom "="])
(self#simplifyUnparseExpr ~wrap:("{","}") expression)
| _ -> makeList ([atom lbl; atom "="; self#simplifyUnparseExpr ~wrap:("{","}") expression])
in
processArguments tail (nextAttr :: processedAttrs) children
| [] -> (processedAttrs, children)
Expand Down Expand Up @@ -5929,11 +5932,11 @@ let printer = object(self:'self)
| _ -> self#formatChildren (remaining @ children) processedRev
end
| {pexp_desc = Pexp_apply(expr, l); pexp_attributes} :: remaining ->
self#formatChildren remaining (self#simplifyUnparseExpr (List.hd children) :: processedRev)
self#formatChildren remaining (self#simplifyUnparseExpr ~wrap:("{", "}") (List.hd children) :: processedRev)
| {pexp_desc = Pexp_ident li} :: remaining ->
self#formatChildren remaining (self#longident_loc li :: processedRev)
| {pexp_desc = Pexp_construct ({txt = Lident "[]"}, None)} :: remaining -> self#formatChildren remaining processedRev
| head :: remaining -> self#formatChildren remaining (self#simplifyUnparseExpr head :: processedRev)
| head :: remaining -> self#formatChildren remaining (self#simplifyUnparseExpr ~wrap:("{", "}") head :: processedRev)
| [] -> match processedRev with
| [] -> None
| _::_ -> Some (List.rev processedRev)
Expand Down

0 comments on commit 77492f8

Please sign in to comment.