@@ -908,7 +908,7 @@ fn class_extends_needs_parens_through_non_null(expression: &Expression<'_>) -> b
908908}
909909
910910impl NeedsParentheses < ' _ > for AstNode < ' _ , ' _ , TSInstantiationExpression < ' _ > > {
911- fn needs_parentheses ( & self , _f : & JsFormatter < ' _ , ' _ > ) -> bool {
911+ fn needs_parentheses ( & self , f : & JsFormatter < ' _ , ' _ > ) -> bool {
912912 let object = match self . parent ( ) {
913913 AstNodes :: StaticMemberExpression ( expr) => & expr. object ,
914914 AstNodes :: ComputedMemberExpression ( expr) => & expr. object ,
@@ -920,7 +920,7 @@ impl NeedsParentheses<'_> for AstNode<'_, '_, TSInstantiationExpression<'_>> {
920920 AstNodes :: CallExpression ( expr) if expr. type_arguments . is_some ( ) => & expr. callee ,
921921 AstNodes :: NewExpression ( expr) if expr. type_arguments . is_some ( ) => & expr. callee ,
922922 AstNodes :: TaggedTemplateExpression ( expr) if expr. type_arguments . is_some ( ) => & expr. tag ,
923- _ => return is_followed_by_comparison_or_sign ( self ) ,
923+ _ => return is_followed_by_comparison_or_sign ( self , f ) ,
924924 } ;
925925
926926 self . span == object. span ( )
@@ -929,34 +929,43 @@ impl NeedsParentheses<'_> for AstNode<'_, '_, TSInstantiationExpression<'_>> {
929929
930930/// Whether the instantiation expression ends the left operand of `+`, `-`, `<` or `>`-led operators:
931931/// TypeScript reads `f<T> + x` as `f < T > +x` and rejects `f<T> < x`,
932- /// also when `f<T>` only ends the operand (`(a + f<T>) - x`, `(typeof f<T>) + x`).
932+ /// also when `f<T>` only ends that operand without parentheses (`(a + f<T>) - x`, `(<A>await f<T>) + x`).
933933fn is_followed_by_comparison_or_sign (
934934 node : & AstNode < ' _ , ' _ , TSInstantiationExpression < ' _ > > ,
935+ f : & JsFormatter < ' _ , ' _ > ,
935936) -> bool {
936937 let mut span = node. span ;
937- let mut parent = node. parent ( ) ;
938- loop {
939- match parent {
940- AstNodes :: BinaryExpression ( expr) if expr. right . span ( ) == span => {
941- ( span, parent) = ( expr. span , expr. parent ( ) ) ;
942- }
943- AstNodes :: UnaryExpression ( expr) => ( span, parent) = ( expr. span , expr. parent ( ) ) ,
944- AstNodes :: BinaryExpression ( expr) => {
945- return expr. left . span ( ) == span
946- && matches ! (
947- expr. operator,
948- BinaryOperator :: Addition
949- | BinaryOperator :: Subtraction
950- | BinaryOperator :: LessThan
951- | BinaryOperator :: GreaterThan
952- | BinaryOperator :: GreaterEqualThan
953- | BinaryOperator :: ShiftRight
954- | BinaryOperator :: ShiftRightZeroFill
955- ) ;
956- }
957- _ => return false ,
938+ for ancestor in node. ancestors ( ) {
939+ if let AstNodes :: BinaryExpression ( expr) = ancestor
940+ && expr. left . span ( ) == span
941+ {
942+ return matches ! (
943+ expr. operator,
944+ BinaryOperator :: Addition
945+ | BinaryOperator :: Subtraction
946+ | BinaryOperator :: LessThan
947+ | BinaryOperator :: GreaterThan
948+ | BinaryOperator :: GreaterEqualThan
949+ | BinaryOperator :: ShiftRight
950+ | BinaryOperator :: ShiftRightZeroFill
951+ ) ;
952+ }
953+ // `f<T>` still ends these as the right or only operand, unless their own parentheses close it.
954+ // Looser ones (`a && f<T>`, `yield f<T>`, ...) always print them before a binary operator.
955+ if !matches ! (
956+ ancestor,
957+ AstNodes :: BinaryExpression ( _)
958+ | AstNodes :: PrivateInExpression ( _)
959+ | AstNodes :: UnaryExpression ( _)
960+ | AstNodes :: AwaitExpression ( _)
961+ | AstNodes :: TSTypeAssertion ( _)
962+ ) || prints_own_parens ( ancestor, f)
963+ {
964+ return false ;
958965 }
966+ span = ancestor. span ( ) ;
959967 }
968+ false
960969}
961970
962971fn binary_like_needs_parens (
0 commit comments