@@ -59,27 +59,21 @@ impl<'a> Format<'a> for AstNode<'a, ArenaVec<'a, Argument<'a>>> {
5959 ) ;
6060 }
6161
62- let call_expression =
63- if let AstNodes :: CallExpression ( call) = self . parent { Some ( call) } else { None } ;
62+ let is_simple_module_import = is_simple_module_import ( self , f. comments ( ) ) ;
6463
65- let ( is_commonjs_or_amd_call, is_test_call) = call_expression
66- . map ( |call| ( is_commonjs_or_amd_call ( self , call) , is_test_call_expression ( call) ) )
67- . unwrap_or_default ( ) ;
64+ let call_expression =
65+ if !is_simple_module_import && let AstNodes :: CallExpression ( call) = self . parent {
66+ Some ( call)
67+ } else {
68+ None
69+ } ;
6870
69- if is_commonjs_or_amd_call
71+ if is_simple_module_import
72+ || call_expression. is_some_and ( |call| {
73+ is_commonjs_or_amd_call ( self , call, f) || is_test_call_expression ( call)
74+ } )
7075 || is_multiline_template_only_args ( self , f. source_text ( ) )
7176 || is_react_hook_with_deps_array ( self , f. comments ( ) )
72- || ( is_test_call && {
73- self . len ( ) != 2
74- || matches ! (
75- arguments. first( ) ,
76- Some (
77- Argument :: StringLiteral ( _)
78- | Argument :: TemplateLiteral ( _)
79- | Argument :: TaggedTemplateExpression ( _)
80- )
81- )
82- } )
8377 {
8478 return write ! (
8579 f,
@@ -929,20 +923,71 @@ fn function_has_only_simple_parameters(params: &FormalParameters<'_>) -> bool {
929923 has_only_simple_parameters ( params, false )
930924}
931925
932- /// Tests if this is a call to commonjs [`require`](https://nodejs.org/api/modules.html#requireid)
933- /// or amd's [`define`](https://github.com/amdjs/amdjs-api/wiki/AMD#define-function-) function.
926+ /// Tests if this a simple module import like `import("module-name")` or `require("module-name")`.
927+ pub fn is_simple_module_import (
928+ arguments : & AstNode < ' _ , ArenaVec < ' _ , Argument < ' _ > > > ,
929+ comments : & Comments ,
930+ ) -> bool {
931+ if arguments. len ( ) != 1 {
932+ return false ;
933+ }
934+
935+ match arguments. parent {
936+ AstNodes :: ImportExpression ( _) => { }
937+ AstNodes :: CallExpression ( call) => {
938+ match & call. callee {
939+ Expression :: StaticMemberExpression ( member) => match member. property . name . as_str ( ) {
940+ "resolve" => {
941+ match & member. object {
942+ Expression :: Identifier ( ident) if ident. name . as_str ( ) == "require" => {
943+ // `require.resolve("foo")`
944+ }
945+ Expression :: MetaProperty ( _) => {
946+ // `import.meta.resolve("foo")`
947+ }
948+ _ => return false ,
949+ }
950+ }
951+ "paths" => {
952+ if !matches ! (
953+ & member. object, Expression :: StaticMemberExpression ( member)
954+ if matches!( & member. object, Expression :: Identifier ( ident)
955+ if ident. name == "require" ) && member. property. name. as_str( ) == "resolve"
956+ ) {
957+ return false ;
958+ }
959+ }
960+ _ => return false ,
961+ } ,
962+ _ => {
963+ return false ;
964+ }
965+ }
966+ }
967+ _ => return false ,
968+ }
969+
970+ matches ! ( arguments. as_ref( ) [ 0 ] , Argument :: StringLiteral ( _) )
971+ && !comments. has_comment_before ( arguments. parent . span ( ) . end )
972+ }
973+
974+ /// Tests if amd's [`define`](https://github.com/amdjs/amdjs-api/wiki/AMD#define-function-) function.
934975fn is_commonjs_or_amd_call (
935976 arguments : & [ Argument < ' _ > ] ,
936977 call : & AstNode < ' _ , CallExpression < ' _ > > ,
978+ f : & Formatter < ' _ , ' _ > ,
937979) -> bool {
938980 let Expression :: Identifier ( ident) = & call. callee else {
939981 return false ;
940982 } ;
941983
942984 match ident. name . as_str ( ) {
943985 "require" => {
986+ let first_argument = & arguments[ 0 ] ;
987+ if f. comments ( ) . has_comment_before ( first_argument. span ( ) . start ) {
988+ return false ;
989+ }
944990 match arguments. len ( ) {
945- 0 => false ,
946991 // `require` can be called with any expression that resolves to a
947992 // string. This check is only an escape hatch to allow a complex
948993 // expression to break rather than group onto the previous line.
@@ -956,7 +1001,9 @@ fn is_commonjs_or_amd_call(
9561001 // require(
9571002 // path.join(__dirname, 'relative/path')
9581003 // );
959- 1 => matches ! ( arguments. first( ) , Some ( Argument :: StringLiteral ( _) ) ) ,
1004+ 1 => {
1005+ matches ! ( first_argument, Argument :: StringLiteral ( _) )
1006+ }
9601007 _ => true ,
9611008 }
9621009 }
0 commit comments