Skip to content

Commit

Permalink
Change the precedence of fast pipe in the presence of function applic…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
anmonteiro committed Jul 15, 2018
1 parent 2e2c562 commit 357d235
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
11 changes: 7 additions & 4 deletions formatTest/unit_tests/expected_output/fastPipe.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ bar->f->g->h;
compilation
->Plugin.buildAssets
->Js.Json.stringify
->Node.Fs.writeFileAsUtf8Sync(_, path);
->Node.Fs.writeFileAsUtf8Sync(
_,
path,
);

foo->bar->baz >>= monadicFunction |> bind;

Expand Down Expand Up @@ -53,12 +56,12 @@ event->(target##value[0]);

event->target(foo);

event->target(foo);

(event->target)(foo);
event->(target(foo));

event->target(foo);

event->(target(foo));

foo->bar := baz;

foo->bar === baz;
Expand Down
2 changes: 1 addition & 1 deletion src/reason-parser/reason_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -3105,7 +3105,7 @@ parenthesized_expr:
| E as_loc(SHARPEQUAL) simple_expr
{ let op = { $2 with txt = "#=" } in
mkinfixop $1 (mkoperator op) $3 }
| E as_loc(MINUSGREATER) simple_expr
| E as_loc(MINUSGREATER) simple_expr_no_call
{ mkinfixop $1 (mkoperator {$2 with txt = "|."}) $3 }
| as_loc(mod_longident) DOT LPAREN MODULE module_expr COLON package_type RPAREN
{ let loc = mklocation $symbolstartpos $endpos in
Expand Down
9 changes: 4 additions & 5 deletions src/reason-parser/reason_pprint_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3542,10 +3542,8 @@ let printer = object(self:'self)
let funApplExpr = formatAttachmentApplication applicationFinalWrapping None (itms, Some reducesAfterRight.pexp_loc)
in
(* Little hack: need to print parens for the `bar` application in e.g.
`foo->other##(bar(baz))`. The exception to this is the fast pipe
operator, which binds tighter to function application. *)
if higherPrecedenceThan withPrecedence (Custom "prec_functionAppl") &&
withPrecedence <> (Token fastPipeToken)
`foo->other##(bar(baz))` or `foo->other->(bar(baz))`. *)
if higherPrecedenceThan withPrecedence (Custom "prec_functionAppl")
then LayoutNode (formatPrecedence ~loc:reducesAfterRight.pexp_loc funApplExpr)
else LayoutNode funApplExpr
| PotentiallyLowPrecedence itm -> LayoutNode (formatPrecedence ~loc:reducesAfterRight.pexp_loc itm)
Expand Down Expand Up @@ -7195,6 +7193,7 @@ let printer = object(self:'self)
makeTup ~uncurried (List.map self#label_x_expression_param params)
method formatFunAppl ~jsxAttrs ~args ~funExpr ~applicationExpr ?(uncurried=false) () =
let funExpr = Reason_ast.processFastPipe funExpr in
let uncurriedApplication = uncurried in
(* If there was a JSX attribute BUT JSX component wasn't detected,
that JSX attribute needs to be pretty printed so it doesn't get
Expand All @@ -7213,7 +7212,7 @@ let printer = object(self:'self)
let formattedFunExpr = match funExpr.pexp_desc with
(* fast pipe chain or sharpop chain as funExpr, no parens needed, we know how to parse *)
| Pexp_apply ({pexp_desc = Pexp_ident {txt = Lident s}}, _)
when requireNoSpaceFor s && s <> fastPipeToken ->
when requireNoSpaceFor s ->
self#unparseExpr funExpr
| _ -> self#simplifyUnparseExpr funExpr
in
Expand Down

0 comments on commit 357d235

Please sign in to comment.