@@ -73,6 +73,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, Expression<'a>> {
7373
7474impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , IdentifierReference < ' a > > {
7575 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
76+ if f. comments ( ) . is_type_cast_node ( self ) {
77+ return false ;
78+ }
79+
7680 match self . name . as_str ( ) {
7781 "async" => {
7882 matches ! ( self . parent, AstNodes :: ForOfStatement ( stmt) if !stmt. r#await && stmt. left. span( ) . contains_inclusive( self . span) )
@@ -165,6 +169,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, Super> {
165169
166170impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , NumericLiteral < ' a > > {
167171 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
172+ if f. comments ( ) . is_type_cast_node ( self ) {
173+ return false ;
174+ }
175+
168176 if let AstNodes :: StaticMemberExpression ( member) = self . parent {
169177 return member. object . without_parentheses ( ) . span ( ) == self . span ( ) ;
170178 }
@@ -174,6 +182,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, NumericLiteral<'a>> {
174182
175183impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , StringLiteral < ' a > > {
176184 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
185+ if f. comments ( ) . is_type_cast_node ( self ) {
186+ return false ;
187+ }
188+
177189 if let AstNodes :: ExpressionStatement ( stmt) = self . parent {
178190 // `() => "foo"`
179191 if let AstNodes :: FunctionBody ( arrow) = stmt. parent {
@@ -207,6 +219,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, ArrayExpression<'a>> {
207219
208220impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , ObjectExpression < ' a > > {
209221 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
222+ if f. comments ( ) . is_type_cast_node ( self ) {
223+ return false ;
224+ }
225+
210226 let parent = self . parent ;
211227 is_class_extends ( self . span , parent)
212228 || is_first_in_statement (
@@ -239,6 +255,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, ComputedMemberExpression<'a>> {
239255
240256impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , StaticMemberExpression < ' a > > {
241257 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
258+ if f. comments ( ) . is_type_cast_node ( self ) {
259+ return false ;
260+ }
261+
242262 matches ! ( self . parent, AstNodes :: NewExpression ( _) ) && {
243263 ExpressionLeftSide :: Expression ( self . object ( ) ) . iter ( ) . any ( |expr| {
244264 matches ! ( expr, ExpressionLeftSide :: Expression ( e) if
@@ -258,6 +278,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, PrivateFieldExpression<'a>> {
258278
259279impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , CallExpression < ' a > > {
260280 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
281+ if f. comments ( ) . is_type_cast_node ( self ) {
282+ return false ;
283+ }
284+
261285 match self . parent {
262286 AstNodes :: NewExpression ( _) => true ,
263287 AstNodes :: ExportDefaultDeclaration ( _) => {
@@ -280,12 +304,20 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, CallExpression<'a>> {
280304
281305impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , NewExpression < ' a > > {
282306 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
307+ if f. comments ( ) . is_type_cast_node ( self ) {
308+ return false ;
309+ }
310+
283311 is_class_extends ( self . span , self . parent )
284312 }
285313}
286314
287315impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , UpdateExpression < ' a > > {
288316 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
317+ if f. comments ( ) . is_type_cast_node ( self ) {
318+ return false ;
319+ }
320+
289321 let parent = self . parent ;
290322 if self . prefix ( )
291323 && let AstNodes :: UnaryExpression ( unary) = parent
@@ -303,6 +335,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, UpdateExpression<'a>> {
303335
304336impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , UnaryExpression < ' a > > {
305337 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
338+ if f. comments ( ) . is_type_cast_node ( self ) {
339+ return false ;
340+ }
341+
306342 let parent = self . parent ;
307343 match parent {
308344 AstNodes :: UnaryExpression ( parent_unary) => {
@@ -323,6 +359,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, UnaryExpression<'a>> {
323359
324360impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , BinaryExpression < ' a > > {
325361 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
362+ if f. comments ( ) . is_type_cast_node ( self ) {
363+ return false ;
364+ }
365+
326366 ( self . operator . is_in ( ) && is_in_for_initializer ( self ) )
327367 || binary_like_needs_parens ( BinaryLikeExpression :: BinaryExpression ( self ) )
328368 }
@@ -371,12 +411,20 @@ fn is_in_for_initializer(expr: &AstNode<'_, BinaryExpression<'_>>) -> bool {
371411impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , PrivateInExpression < ' a > > {
372412 #[ inline]
373413 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
414+ if f. comments ( ) . is_type_cast_node ( self ) {
415+ return false ;
416+ }
417+
374418 is_class_extends ( self . span , self . parent )
375419 }
376420}
377421
378422impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , LogicalExpression < ' a > > {
379423 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
424+ if f. comments ( ) . is_type_cast_node ( self ) {
425+ return false ;
426+ }
427+
380428 let parent = self . parent ;
381429 if let AstNodes :: LogicalExpression ( parent) = parent {
382430 parent. operator ( ) != self . operator ( )
@@ -392,6 +440,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, LogicalExpression<'a>> {
392440
393441impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , ConditionalExpression < ' a > > {
394442 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
443+ if f. comments ( ) . is_type_cast_node ( self ) {
444+ return false ;
445+ }
446+
395447 let parent = self . parent ;
396448 if matches ! (
397449 parent,
@@ -420,6 +472,11 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, Function<'a>> {
420472 if self . r#type ( ) != FunctionType :: FunctionExpression {
421473 return false ;
422474 }
475+
476+ if f. comments ( ) . is_type_cast_node ( self ) {
477+ return false ;
478+ }
479+
423480 let parent = self . parent ;
424481 matches ! (
425482 parent,
@@ -436,6 +493,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, Function<'a>> {
436493
437494impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , AssignmentExpression < ' a > > {
438495 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
496+ if f. comments ( ) . is_type_cast_node ( self ) {
497+ return false ;
498+ }
499+
439500 match self . parent {
440501 // Expression statements, only object destructuring needs parens:
441502 // - `a = b` = no parens
@@ -519,6 +580,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, AssignmentExpression<'a>> {
519580
520581impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , SequenceExpression < ' a > > {
521582 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
583+ if f. comments ( ) . is_type_cast_node ( self ) {
584+ return false ;
585+ }
586+
522587 !matches ! (
523588 self . parent,
524589 AstNodes :: ReturnStatement ( _)
@@ -534,12 +599,20 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, SequenceExpression<'a>> {
534599
535600impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , AwaitExpression < ' a > > {
536601 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
602+ if f. comments ( ) . is_type_cast_node ( self ) {
603+ return false ;
604+ }
605+
537606 await_or_yield_needs_parens ( self . span ( ) , self . parent )
538607 }
539608}
540609
541610impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , ChainExpression < ' a > > {
542611 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
612+ if f. comments ( ) . is_type_cast_node ( self ) {
613+ return false ;
614+ }
615+
543616 match self . parent {
544617 AstNodes :: NewExpression ( _) => true ,
545618 AstNodes :: CallExpression ( call) => !call. optional ,
@@ -557,6 +630,11 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, Class<'a>> {
557630 if self . r#type ( ) != ClassType :: ClassExpression {
558631 return false ;
559632 }
633+
634+ if f. comments ( ) . is_type_cast_node ( self ) {
635+ return false ;
636+ }
637+
560638 let parent = self . parent ;
561639 match parent {
562640 AstNodes :: CallExpression ( _)
@@ -583,6 +661,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, ParenthesizedExpression<'a>> {
583661
584662impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , ArrowFunctionExpression < ' a > > {
585663 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
664+ if f. comments ( ) . is_type_cast_node ( self ) {
665+ return false ;
666+ }
667+
586668 let parent = self . parent ;
587669 if matches ! (
588670 parent,
@@ -606,6 +688,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, ArrowFunctionExpression<'a>> {
606688
607689impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , YieldExpression < ' a > > {
608690 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
691+ if f. comments ( ) . is_type_cast_node ( self ) {
692+ return false ;
693+ }
694+
609695 let parent = self . parent ;
610696 matches ! ( parent, AstNodes :: AwaitExpression ( _) | AstNodes :: TSTypeAssertion ( _) )
611697 || await_or_yield_needs_parens ( self . span ( ) , parent)
@@ -614,6 +700,10 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, YieldExpression<'a>> {
614700
615701impl < ' a > NeedsParentheses < ' a > for AstNode < ' a , ImportExpression < ' a > > {
616702 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' a > ) -> bool {
703+ if f. comments ( ) . is_type_cast_node ( self ) {
704+ return false ;
705+ }
706+
617707 matches ! ( self . parent, AstNodes :: NewExpression ( _) )
618708 }
619709}
@@ -1022,12 +1112,20 @@ fn jsx_element_or_fragment_needs_paren(span: Span, parent: &AstNodes<'_>) -> boo
10221112
10231113impl NeedsParentheses < ' _ > for AstNode < ' _ , JSXElement < ' _ > > {
10241114 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' _ > ) -> bool {
1115+ if f. comments ( ) . is_type_cast_node ( self ) {
1116+ return false ;
1117+ }
1118+
10251119 jsx_element_or_fragment_needs_paren ( self . span , self . parent )
10261120 }
10271121}
10281122
10291123impl NeedsParentheses < ' _ > for AstNode < ' _ , JSXFragment < ' _ > > {
10301124 fn needs_parentheses ( & self , f : & Formatter < ' _ , ' _ > ) -> bool {
1125+ if f. comments ( ) . is_type_cast_node ( self ) {
1126+ return false ;
1127+ }
1128+
10311129 jsx_element_or_fragment_needs_paren ( self . span , self . parent )
10321130 }
10331131}
0 commit comments