@@ -228118,7 +228118,8 @@ val subBinaryExprOperand : string -> string -> bool
228118228118val rhsBinaryExprOperand : string -> Parsetree.expression -> bool
228119228119val flattenOperandRhs : string -> Parsetree.expression -> bool
228120228120
228121- val lazyOrAssertOrAwaitExprRhs : Parsetree.expression -> kind
228121+ val binaryOperatorInsideAwaitNeedsParens : string -> bool
228122+ val lazyOrAssertOrAwaitExprRhs : ?inAwait:bool -> Parsetree.expression -> kind
228122228123
228123228124val fieldExpr : Parsetree.expression -> kind
228124228125
@@ -228322,7 +228323,11 @@ let flattenOperandRhs parentOperator rhs =
228322228323 | _ when ParsetreeViewer.isTernaryExpr rhs -> true
228323228324 | _ -> false
228324228325
228325- let lazyOrAssertOrAwaitExprRhs expr =
228326+ let binaryOperatorInsideAwaitNeedsParens operator =
228327+ ParsetreeViewer.operatorPrecedence operator
228328+ < ParsetreeViewer.operatorPrecedence "|."
228329+
228330+ let lazyOrAssertOrAwaitExprRhs ?(inAwait = false) expr =
228326228331 let optBraces, _ = ParsetreeViewer.processBracesAttr expr in
228327228332 match optBraces with
228328228333 | Some ({Location.loc = bracesLoc}, _) -> Braced bracesLoc
@@ -228333,7 +228338,14 @@ let lazyOrAssertOrAwaitExprRhs expr =
228333228338 | _ :: _ -> true
228334228339 | [] -> false ->
228335228340 Parenthesized
228336- | expr when ParsetreeViewer.isBinaryExpression expr -> Parenthesized
228341+ | {
228342+ pexp_desc =
228343+ Pexp_apply ({pexp_desc = Pexp_ident {txt = Longident.Lident operator}}, _);
228344+ }
228345+ when ParsetreeViewer.isBinaryExpression expr ->
228346+ if inAwait && not (binaryOperatorInsideAwaitNeedsParens operator) then
228347+ Nothing
228348+ else Parenthesized
228337228349 | {
228338228350 pexp_desc =
228339228351 Pexp_constraint ({pexp_desc = Pexp_pack _}, {ptyp_desc = Ptyp_package _});
@@ -228349,7 +228361,9 @@ let lazyOrAssertOrAwaitExprRhs expr =
228349228361 | Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
228350228362 } ->
228351228363 Parenthesized
228352- | _ when ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
228364+ | _
228365+ when (not inAwait)
228366+ && ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
228353228367 Parenthesized
228354228368 | _ -> Nothing)
228355228369
@@ -232365,13 +232379,13 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
232365232379 if ParsetreeViewer.hasAwaitAttribute e.pexp_attributes then
232366232380 let rhs =
232367232381 match
232368- Parens.lazyOrAssertOrAwaitExprRhs
232382+ Parens.lazyOrAssertOrAwaitExprRhs ~inAwait:true
232369232383 {
232370232384 e with
232371232385 pexp_attributes =
232372232386 List.filter
232373232387 (function
232374- | {Location.txt = "res.await" | " ns.braces"}, _ -> false
232388+ | {Location.txt = "ns.braces"}, _ -> false
232375232389 | _ -> true)
232376232390 e.pexp_attributes;
232377232391 }
@@ -232647,13 +232661,18 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl =
232647232661 in
232648232662 let doc =
232649232663 if isAwait then
232664+ let parens =
232665+ Res_parens.binaryOperatorInsideAwaitNeedsParens operator
232666+ in
232650232667 Doc.concat
232651232668 [
232652- Doc.text "await ";
232653232669 Doc.lparen;
232670+ Doc.text "await ";
232671+ (if parens then Doc.lparen else Doc.nil);
232654232672 leftPrinted;
232655232673 printBinaryOperator ~inlineRhs:false operator;
232656232674 rightPrinted;
232675+ (if parens then Doc.rparen else Doc.nil);
232657232676 Doc.rparen;
232658232677 ]
232659232678 else
@@ -299892,7 +299911,8 @@ and parseAwaitExpression p =
299892299911 let awaitLoc = mkLoc p.Parser.startPos p.endPos in
299893299912 let awaitAttr = makeAwaitAttr awaitLoc in
299894299913 Parser.expect Await p;
299895- let expr = parseUnaryExpr p in
299914+ let tokenPrec = Token.precedence MinusGreater in
299915+ let expr = parseBinaryExpr ~context:OrdinaryExpr p tokenPrec in
299896299916 {
299897299917 expr with
299898299918 pexp_attributes = awaitAttr :: expr.pexp_attributes;
0 commit comments