1
1
use rspack_cacheable:: { cacheable, cacheable_dyn, with:: Skip } ;
2
2
use 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 ,
7
6
} ;
8
7
use swc_core:: common:: Span ;
9
8
10
- #[ cacheable]
11
- #[ derive( Debug , Clone ) ]
12
- pub enum Position {
13
- Before ,
14
- After ,
15
- }
16
-
17
9
#[ cacheable]
18
10
#[ derive( Debug , Clone ) ]
19
11
pub struct MockMethodDependency {
@@ -24,16 +16,18 @@ pub struct MockMethodDependency {
24
16
request : String ,
25
17
hoist : bool ,
26
18
method : MockMethod ,
27
- module_dep_id : Option < DependencyId > ,
28
- position : Position ,
29
19
}
30
20
31
21
#[ cacheable]
32
22
#[ derive( Debug , Clone , PartialEq , Eq ) ]
23
+ #[ allow( dead_code) ]
33
24
pub enum MockMethod {
34
25
Mock ,
35
26
DoMock ,
27
+ MockRequire ,
28
+ DoMockRequire ,
36
29
Unmock ,
30
+ DoUnmock ,
37
31
Hoisted ,
38
32
}
39
33
@@ -44,17 +38,13 @@ impl MockMethodDependency {
44
38
request : String ,
45
39
hoist : bool ,
46
40
method : MockMethod ,
47
- module_dep_id : Option < DependencyId > ,
48
- position : Position ,
49
41
) -> Self {
50
42
Self {
51
43
call_expr_span,
52
44
callee_span,
53
45
request,
54
46
hoist,
55
47
method,
56
- module_dep_id,
57
- position,
58
48
}
59
49
}
60
50
}
@@ -86,14 +76,7 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
86
76
source : & mut TemplateReplaceSource ,
87
77
code_generatable_context : & mut TemplateContext ,
88
78
) {
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;
97
80
let dep = dep
98
81
. as_any ( )
99
82
. downcast_ref :: < MockMethodDependency > ( )
@@ -103,52 +86,33 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
103
86
let hoist_flag = match dep. method {
104
87
MockMethod :: Mock => "MOCK" ,
105
88
MockMethod :: DoMock => "" , // won't be used.
89
+ MockMethod :: MockRequire => "MOCKREQUIRE" ,
90
+ MockMethod :: DoMockRequire => "" , // won't be used.
106
91
MockMethod :: Unmock => "UNMOCK" ,
107
92
MockMethod :: Hoisted => "HOISTED" ,
93
+ MockMethod :: DoUnmock => "" , // won't be used.
108
94
} ;
109
95
110
96
let mock_method = match dep. method {
111
97
MockMethod :: Mock => "rstest_mock" ,
112
98
MockMethod :: DoMock => "rstest_do_mock" ,
99
+ MockMethod :: MockRequire => "rstest_mock_require" ,
100
+ MockMethod :: DoMockRequire => "rstest_do_mock_require" ,
113
101
MockMethod :: Unmock => "rstest_unmock" ,
114
102
MockMethod :: Hoisted => "rstest_hoisted" ,
103
+ MockMethod :: DoUnmock => "rstest_do_unmock" ,
115
104
} ;
116
105
117
106
// 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}" ) ) ,
149
113
None ,
150
- RuntimeCondition :: Boolean ( true ) ,
151
- ) ) ) ;
114
+ ) ;
115
+ init_fragments . push ( init . boxed ( ) ) ;
152
116
}
153
117
154
118
// Start before hoist.
0 commit comments