1+ use std:: iter;
2+
13use oxc_ast:: ast:: * ;
4+ use oxc_span:: GetSpan ;
25
3- use crate :: ast_nodes:: { AstNode , AstNodes } ;
6+ use crate :: {
7+ ast_nodes:: { AstNode , AstNodes } ,
8+ formatter:: Comments ,
9+ } ;
410
511/// This is a specialized function that checks if the current [call expression]
612/// resembles a call expression usually used by a testing frameworks.
@@ -17,13 +23,26 @@ use crate::ast_nodes::{AstNode, AstNodes};
1723/// 4. The third argument, if present, has to be a number literal
1824/// 5. The second argument has to be an [arrow function expression] or [function expression]
1925/// 6. Both function must have zero or one parameters
26+ /// 7. No comments around the [arguments]
2027///
2128/// [call expression]: CallExpression
2229/// [callee]: Expression
2330/// [arguments]: CallExpression::arguments
2431/// [arrow function expression]: ArrowFunctionExpression
2532/// [function expression]: Function
26- pub fn is_test_call_expression ( call : & AstNode < CallExpression < ' _ > > ) -> bool {
33+ pub fn is_test_call_expression ( call : & AstNode < CallExpression < ' _ > > , comments : & Comments ) -> bool {
34+ is_test_call_shape ( call, comments) && !has_comment_around_arguments ( call, comments)
35+ }
36+
37+ /// The compact test call layout joins arguments with spaces, which breaks comment order.
38+ fn has_comment_around_arguments ( call : & CallExpression , comments : & Comments ) -> bool {
39+ let first = call. type_arguments . as_ref ( ) . map_or ( call. callee . span ( ) . end , |t| t. span . end ) ;
40+ let starts = iter:: once ( first) . chain ( call. arguments . iter ( ) . map ( |a| a. span ( ) . end ) ) ;
41+ let ends = call. arguments . iter ( ) . map ( |a| a. span ( ) . start ) . chain ( iter:: once ( call. span . end ) ) ;
42+ starts. zip ( ends) . any ( |( start, end) | comments. has_any_comment_in_range ( start, end) )
43+ }
44+
45+ fn is_test_call_shape ( call : & AstNode < CallExpression < ' _ > > , comments : & Comments ) -> bool {
2746 if call. optional {
2847 return false ;
2948 }
@@ -37,7 +56,7 @@ pub fn is_test_call_expression(call: &AstNode<CallExpression<'_>>) -> bool {
3756 ( Some ( argument) , None , None ) if arguments. len ( ) == 1 => {
3857 if is_angular_test_wrapper ( call) && {
3958 if let AstNodes :: CallExpression ( call) = call. parent ( ) {
40- is_test_call_expression ( call)
59+ is_test_call_expression ( call, comments )
4160 } else {
4261 false
4362 }
0 commit comments