@@ -260,6 +260,29 @@ const isTypeCastComment = (comment: PrettierComment): boolean =>
260260 comment . value . startsWith ( '*' ) &&
261261 / @ (?: t y p e | s a t i s f i e s ) \b / . test ( comment . value ) ;
262262
263+ /**
264+ * Returns the greatest value less than or equal to `target` from an ascending
265+ * array, or `undefined` when no such value exists.
266+ */
267+ const findLastAtOrBefore = (
268+ sortedValues : number [ ] ,
269+ target : number ,
270+ ) : number | undefined => {
271+ let lower = 0 ;
272+ let upper = sortedValues . length ;
273+
274+ while ( lower < upper ) {
275+ const middle = lower + Math . floor ( ( upper - lower ) / 2 ) ;
276+ if ( sortedValues [ middle ] <= target ) {
277+ lower = middle + 1 ;
278+ } else {
279+ upper = middle ;
280+ }
281+ }
282+
283+ return sortedValues [ lower - 1 ] ;
284+ } ;
285+
263286type VisitOptions = {
264287 onEnter ?: ( node : AstNode ) => AstNode | undefined ;
265288 onLeave ?: ( node : AstNode ) => AstNode | undefined ;
@@ -362,12 +385,14 @@ const postprocess = (
362385 const expression = asAstNode ( node . expression ) ;
363386 const start = locStart ( node ) ;
364387
388+ // Yuku comments are in source order, so these end offsets are sorted.
365389 typeCastCommentEnds ??= comments
366390 . filter ( isTypeCastComment )
367391 . map ( ( comment ) => locEnd ( comment ) ) ;
368392
369- const previousCommentEnd = typeCastCommentEnds . findLast (
370- ( end ) => end <= start ,
393+ const previousCommentEnd = findLastAtOrBefore (
394+ typeCastCommentEnds ,
395+ start ,
371396 ) ;
372397 const shouldKeepParentheses =
373398 previousCommentEnd !== undefined &&
0 commit comments