11use rspack_cacheable:: { cacheable, cacheable_dyn, with:: Skip } ;
22use rspack_core:: {
3- AsContextDependency , AsModuleDependency , ConditionalInitFragment , DependencyCodeGeneration ,
4- DependencyId , DependencyRange , DependencyTemplate , DependencyTemplateType , DependencyType ,
5- InitFragmentExt , InitFragmentKey , InitFragmentStage , NormalInitFragment , RuntimeCondition ,
6- TemplateContext , TemplateReplaceSource , import_statement,
3+ AsContextDependency , AsModuleDependency , DependencyCodeGeneration , DependencyRange ,
4+ DependencyTemplate , DependencyTemplateType , DependencyType , InitFragmentExt , InitFragmentKey ,
5+ InitFragmentStage , NormalInitFragment , TemplateContext , TemplateReplaceSource ,
76} ;
87use swc_core:: common:: Span ;
98
10- #[ cacheable]
11- #[ derive( Debug , Clone ) ]
12- pub enum Position {
13- Before ,
14- After ,
15- }
16-
179#[ cacheable]
1810#[ derive( Debug , Clone ) ]
1911pub struct MockMethodDependency {
@@ -24,16 +16,18 @@ pub struct MockMethodDependency {
2416 request : String ,
2517 hoist : bool ,
2618 method : MockMethod ,
27- module_dep_id : Option < DependencyId > ,
28- position : Position ,
2919}
3020
3121#[ cacheable]
3222#[ derive( Debug , Clone , PartialEq , Eq ) ]
23+ #[ allow( dead_code) ]
3324pub enum MockMethod {
3425 Mock ,
3526 DoMock ,
27+ MockRequire ,
28+ DoMockRequire ,
3629 Unmock ,
30+ DoUnmock ,
3731 Hoisted ,
3832}
3933
@@ -44,17 +38,13 @@ impl MockMethodDependency {
4438 request : String ,
4539 hoist : bool ,
4640 method : MockMethod ,
47- module_dep_id : Option < DependencyId > ,
48- position : Position ,
4941 ) -> Self {
5042 Self {
5143 call_expr_span,
5244 callee_span,
5345 request,
5446 hoist,
5547 method,
56- module_dep_id,
57- position,
5848 }
5949 }
6050}
@@ -86,14 +76,7 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
8676 source : & mut TemplateReplaceSource ,
8777 code_generatable_context : & mut TemplateContext ,
8878 ) {
89- let TemplateContext {
90- module,
91- runtime_requirements,
92- compilation,
93- init_fragments,
94- runtime,
95- ..
96- } = code_generatable_context;
79+ let TemplateContext { init_fragments, .. } = code_generatable_context;
9780 let dep = dep
9881 . as_any ( )
9982 . downcast_ref :: < MockMethodDependency > ( )
@@ -103,52 +86,33 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
10386 let hoist_flag = match dep. method {
10487 MockMethod :: Mock => "MOCK" ,
10588 MockMethod :: DoMock => "" , // won't be used.
89+ MockMethod :: MockRequire => "MOCKREQUIRE" ,
90+ MockMethod :: DoMockRequire => "" , // won't be used.
10691 MockMethod :: Unmock => "UNMOCK" ,
10792 MockMethod :: Hoisted => "HOISTED" ,
93+ MockMethod :: DoUnmock => "" , // won't be used.
10894 } ;
10995
11096 let mock_method = match dep. method {
11197 MockMethod :: Mock => "rstest_mock" ,
11298 MockMethod :: DoMock => "rstest_do_mock" ,
99+ MockMethod :: MockRequire => "rstest_mock_require" ,
100+ MockMethod :: DoMockRequire => "rstest_do_mock_require" ,
113101 MockMethod :: Unmock => "rstest_unmock" ,
114102 MockMethod :: Hoisted => "rstest_hoisted" ,
103+ MockMethod :: DoUnmock => "rstest_do_unmock" ,
115104 } ;
116105
117106 // Hoist placeholder init fragment.
118- let init = NormalInitFragment :: new (
119- format ! ( "/* RSTEST:{hoist_flag}_PLACEHOLDER:{request} */;" ) ,
120- InitFragmentStage :: StageESMImports ,
121- match dep. position {
122- Position :: Before => 0 ,
123- Position :: After => i32:: MAX - 1 ,
124- } ,
125- InitFragmentKey :: Const ( format ! ( "rstest mock_hoist {request}" ) ) ,
126- None ,
127- ) ;
128- init_fragments. push ( init. boxed ( ) ) ;
129-
130- if dep. method == MockMethod :: Mock
131- && let Some ( module_dep_id) = dep. module_dep_id
132- {
133- let content: ( String , String ) = import_statement (
134- * module,
135- * runtime,
136- compilation,
137- runtime_requirements,
138- & module_dep_id,
139- request,
140- false ,
141- ) ;
142-
143- // Redeclaration init fragment.
144- init_fragments. push ( Box :: new ( ConditionalInitFragment :: new (
145- format ! ( "{}{}" , content. 0 , content. 1 ) ,
146- InitFragmentStage :: StageAsyncESMImports ,
147- i32:: MAX ,
148- InitFragmentKey :: ESMImport ( format ! ( "{} {}" , request, "mock" ) ) ,
107+ if !hoist_flag. is_empty ( ) {
108+ let init = NormalInitFragment :: new (
109+ format ! ( "/* RSTEST:{hoist_flag}_PLACEHOLDER:{request} */;" ) ,
110+ InitFragmentStage :: StageESMImports ,
111+ 0 ,
112+ InitFragmentKey :: Const ( format ! ( "rstest mock_hoist {request}" ) ) ,
149113 None ,
150- RuntimeCondition :: Boolean ( true ) ,
151- ) ) ) ;
114+ ) ;
115+ init_fragments . push ( init . boxed ( ) ) ;
152116 }
153117
154118 // Start before hoist.
0 commit comments