From 051ad60675f50c5747131c13815db38987c04d48 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sun, 1 Jul 2018 19:16:41 -0700 Subject: [PATCH 1/2] Print (foo^)##bar with the right precedence fixes #1554 --- .../unit_tests/expected_output/basicStructures.re | 3 +++ formatTest/unit_tests/input/basicStructures.re | 3 +++ src/reason-parser/reason_pprint_ast.ml | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/formatTest/unit_tests/expected_output/basicStructures.re b/formatTest/unit_tests/expected_output/basicStructures.re index 970dcbd9b..e8c97c84c 100644 --- a/formatTest/unit_tests/expected_output/basicStructures.re +++ b/formatTest/unit_tests/expected_output/basicStructures.re @@ -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; diff --git a/formatTest/unit_tests/input/basicStructures.re b/formatTest/unit_tests/input/basicStructures.re index e7f7a0075..0b5a261ff 100644 --- a/formatTest/unit_tests/input/basicStructures.re +++ b/formatTest/unit_tests/input/basicStructures.re @@ -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; diff --git a/src/reason-parser/reason_pprint_ast.ml b/src/reason-parser/reason_pprint_ast.ml index 1b7248128..472d7ffd9 100644 --- a/src/reason-parser/reason_pprint_ast.ml +++ b/src/reason-parser/reason_pprint_ast.ml @@ -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 @@ -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 + ~except_unary_postfix:true + leftExpr + in let rightItm = ( match rightExpr.pexp_desc with | Pexp_apply (eFun, ls) -> ( @@ -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)]) -> @@ -5276,12 +5281,16 @@ 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 ?(except_unary_postfix=false) x = + match x.pexp_desc with | (Pexp_apply (eFun, _)) -> ( match printedStringAndFixityExpr eFun with | AlmostSimplePrefix _ -> source_map ~loc:x.pexp_loc (formatPrecedence (self#simplifyUnparseExpr x)) + | UnaryPostfix _ when except_unary_postfix -> + source_map ~loc:x.pexp_loc + (formatPrecedence (self#simplifyUnparseExpr x)) | UnaryPlusPrefix _ | UnaryMinusPrefix _ | UnaryNotPrefix _ From 202ccdaa11f1d375d29e2a1a621fd8dc88f10cb2 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Mon, 2 Jul 2018 13:55:34 -0700 Subject: [PATCH 2/2] code review tweaks --- src/reason-parser/reason_pprint_ast.ml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/reason-parser/reason_pprint_ast.ml b/src/reason-parser/reason_pprint_ast.ml index 472d7ffd9..ed67ff3f1 100644 --- a/src/reason-parser/reason_pprint_ast.ml +++ b/src/reason-parser/reason_pprint_ast.ml @@ -3380,7 +3380,7 @@ let printer = object(self:'self) high enough precedence for the parens to be worth adding back. *) let leftItm = self#simple_enough_to_be_lhs_dot_send - ~except_unary_postfix:true + ~infix_context:infixStr leftExpr in let rightItm = ( @@ -5281,22 +5281,22 @@ let printer = object(self:'self) * }; * *) - method simple_enough_to_be_lhs_dot_send ?(except_unary_postfix=false) x = + 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 _ when except_unary_postfix -> + | 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. *)