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

Don't swallow <> </> fragments when formatting #1963

Merged
merged 3 commits into from
May 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions formatTest/typeCheckedTests/expected_output/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,11 @@ module Foo3 = {
};

<Foo3 bar={<Foo />} />;

let onClickHandler = () => ();

let div = (~onClick, ~children, ()) => ();

<div onClick=onClickHandler>
<> "foobar" </>
</div>;
6 changes: 6 additions & 0 deletions formatTest/typeCheckedTests/input/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,9 @@ module Foo3 = {
};

<Foo3 bar=<Foo /> />;

let onClickHandler = () => ();

let div = (~onClick, ~children, ()) => ();

<div onClick=onClickHandler> <> "foobar" </> </div>;
5 changes: 5 additions & 0 deletions formatTest/unit_tests/expected_output/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,8 @@ Module.[<Component />];
let (/></) = (a, b) => a + b;

let x = foo /></ bar;

/* https://github.com/facebook/reason/issues/870 */
<div onClick=this##handleClick>
<> foo </>
</div>;
5 changes: 5 additions & 0 deletions formatTest/unit_tests/input/jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,8 @@ Module.[<Component />];
let (/></) = (a, b) => a + b;

let x = foo /></ bar;

/* https://github.com/facebook/reason/issues/870 */
<div onClick=this##handleClick>
<>foo</>
</div>
11 changes: 8 additions & 3 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ let is_punned_labelled_expression e lbl = match e.pexp_desc with
| _ -> false

let is_punned_labelled_pattern p lbl = match p.ppat_desc with
| Ppat_constraint ({ ppat_desc = Ppat_var { txt; _ }; ppat_attributes = _::_ }, _)
| Ppat_constraint ({ ppat_desc = Ppat_var { txt; _ }; ppat_attributes = _::_ }, _)
-> false
| Ppat_constraint ({ ppat_desc = Ppat_var { txt; _ }; _ }, _)
| Ppat_var { txt; _ }
Expand Down Expand Up @@ -5629,8 +5629,13 @@ let printer = object(self:'self)
| {pexp_desc = Pexp_constant constant} as x :: remaining ->
let raw_literal, _ = extract_raw_literal x.pexp_attributes in
self#formatChildren remaining (self#constant ?raw_literal constant :: processedRev)
| {pexp_desc = Pexp_construct ({txt = Lident "::"}, Some {pexp_desc = Pexp_tuple children} )} :: remaining ->
self#formatChildren (remaining @ children) processedRev
| {pexp_desc = Pexp_construct ({txt = Lident "::"}, Some {pexp_desc = Pexp_tuple children} )} as x :: remaining ->
let {jsxAttrs} = partitionAttributes x.pexp_attributes in
if jsxAttrs != [] then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is the pattern match on jsxAttrs. Pattern matching is probably cheaper than physical equality checks. (Correct me if I'm wrong).

Copy link
Contributor

@hcarty hcarty May 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern matching should be safer against future changes in compiler optimizations around equality.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I actually had a pattern match at first but decided to go with this approach given it’s used everywhere else that does the same thing.

match self#simplest_expression x with
| Some exp -> self#formatChildren remaining (exp :: processedRev)
| None -> self#formatChildren (remaining @ children) processedRev
else self#formatChildren (remaining @ children) processedRev
| {pexp_desc = Pexp_apply(expr, l); pexp_attributes} :: remaining ->
self#formatChildren remaining (self#simplifyUnparseExpr (List.hd children) :: processedRev)
| {pexp_desc = Pexp_ident li} :: remaining ->
Expand Down