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

Print (foo^)##bar with the right precedence #2044

Merged
merged 2 commits into from
Jul 4, 2018
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
3 changes: 3 additions & 0 deletions formatTest/unit_tests/expected_output/basicStructures.re
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,6 @@ it("should remove parens", a => {
print_string("did it work?");
print_string("did it work?");
});

/* https://github.com/facebook/reason/issues/1554 */
(curNode^)##childNodes;
3 changes: 3 additions & 0 deletions formatTest/unit_tests/input/basicStructures.re
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,6 @@ it("should remove parens", (a) => {
print_string("did it work?");
print_string("did it work?");
});

/* https://github.com/facebook/reason/issues/1554 */
(curNode^)##childNodes;
31 changes: 20 additions & 11 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ let getPrintableUnaryIdent s =
may have resulted from Pexp -> Texp -> Pexp translation, then checking
if all the characters in the beginning of the string are valid infix
characters. *)
let printedStringAndFixity = function
let printedStringAndFixity = function
| s when List.mem s special_infix_strings -> Infix s
| "^" -> UnaryPostfix "^"
| s when List.mem s.[0] infix_symbols -> Infix s
Expand Down Expand Up @@ -3378,6 +3378,11 @@ let printer = object(self:'self)

Which seems to indicate that the pretty printer doesn't think `#=` is of
high enough precedence for the parens to be worth adding back. *)
let leftItm =
self#simple_enough_to_be_lhs_dot_send
~infix_context:infixStr
leftExpr
in
let rightItm = (
match rightExpr.pexp_desc with
| Pexp_apply (eFun, ls) -> (
Expand All @@ -3387,7 +3392,7 @@ let printer = object(self:'self)
)
| _ -> self#simplifyUnparseExpr rightExpr
) in
Some (makeList [self#simple_enough_to_be_lhs_dot_send leftExpr; atom infixStr; rightItm])
Some (makeList [leftItm; atom infixStr; rightItm])
| (_, _) -> (
match (eFun, ls) with
| ({pexp_desc = Pexp_ident {txt = Ldot (Lident ("Array"),"get")}}, [(_,e1);(_,e2)]) ->
Expand Down Expand Up @@ -5276,18 +5281,22 @@ let printer = object(self:'self)
* };
*
*)
method simple_enough_to_be_lhs_dot_send x = match x.pexp_desc with
method simple_enough_to_be_lhs_dot_send ?(infix_context) x =
match x.pexp_desc with
| (Pexp_apply (eFun, _)) -> (
match printedStringAndFixityExpr eFun with
| AlmostSimplePrefix _ ->
match printedStringAndFixityExpr eFun, infix_context with
| AlmostSimplePrefix _, _ ->
source_map ~loc:x.pexp_loc
(formatPrecedence (self#simplifyUnparseExpr x))
| UnaryPostfix _, Some infix_str when infix_str.[0] = '#' ->
source_map ~loc:x.pexp_loc
(formatPrecedence (self#simplifyUnparseExpr x))
| UnaryPlusPrefix _
| UnaryMinusPrefix _
| UnaryNotPrefix _
| UnaryPostfix _
| Infix _ -> self#simplifyUnparseExpr x
| Normal ->
| UnaryPlusPrefix _, _
| UnaryMinusPrefix _, _
| UnaryNotPrefix _, _
| UnaryPostfix _, _
| Infix _, _ -> self#simplifyUnparseExpr x
| Normal, _ ->
if x.pexp_attributes = [] then
(* `let a = foo().bar` instead of `let a = (foo()).bar *)
(* same for foo()##bar, foo()#=bar, etc. *)
Expand Down