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..ed67ff3f1 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 + ~infix_context:infixStr + 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,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. *)