@@ -61,43 +61,37 @@ pub struct RawResolveOptions {
6161fn normalize_alias (
6262 alias : Option < Either < Vec < RawAliasOptionItem > , bool > > ,
6363) -> rspack_error:: Result < Option < Alias > > {
64- alias
65- . map ( |alias| match alias {
66- Either :: A ( alias) => {
67- let alias = alias
68- . into_iter ( )
69- . map ( |alias_item| {
70- alias_item
71- . redirect
72- . into_iter ( )
73- . map ( |value| {
74- if let Some ( s) = value. as_str ( ) {
75- Ok ( AliasMap :: Path ( s. to_string ( ) ) )
76- } else if let Some ( b) = value. as_bool ( ) {
77- if b {
78- Err ( error ! ( "Alias should not be true in {}" , alias_item. path) )
79- } else {
80- Ok ( AliasMap :: Ignore )
81- }
82- } else {
83- Err ( error ! (
84- "Alias should be false or string in {}" ,
85- alias_item. path
86- ) )
87- }
88- } )
89- . collect :: < rspack_error:: Result < Vec < _ > > > ( )
90- . map ( |value| ( alias_item. path , value) )
91- } )
92- . collect :: < rspack_error:: Result < Vec < _ > > > ( ) ;
93- alias. map ( Alias :: MergeAlias )
94- }
95- Either :: B ( falsy) => {
96- assert ! ( !falsy, "Alias should not be true" ) ;
97- Ok ( Alias :: OverwriteToNoAlias )
64+ let Some ( alias) = alias else {
65+ return Ok ( None ) ;
66+ } ;
67+
68+ match alias {
69+ Either :: A ( alias_items) => {
70+ let mut normalized_aliases = Vec :: with_capacity ( alias_items. len ( ) ) ;
71+ for alias_item in alias_items {
72+ let RawAliasOptionItem { path, redirect } = alias_item;
73+ let mut normalized_redirect = Vec :: with_capacity ( redirect. len ( ) ) ;
74+ for value in redirect {
75+ match value {
76+ AliasValue :: String ( string) => normalized_redirect. push ( AliasMap :: Path ( string) ) ,
77+ AliasValue :: Bool ( false ) => normalized_redirect. push ( AliasMap :: Ignore ) ,
78+ AliasValue :: Bool ( true ) => {
79+ return Err ( error ! ( "Alias should not be true in {}" , path) ) ;
80+ }
81+ _ => {
82+ return Err ( error ! ( "Alias should be false or string in {}" , path) ) ;
83+ }
84+ }
85+ }
86+ normalized_aliases. push ( ( path, normalized_redirect) ) ;
9887 }
99- } )
100- . map_or ( Ok ( None ) , |v| v. map ( Some ) )
88+ Ok ( Some ( Alias :: MergeAlias ( normalized_aliases) ) )
89+ }
90+ Either :: B ( falsy) => {
91+ assert ! ( !falsy, "Alias should not be true" ) ;
92+ Ok ( Some ( Alias :: OverwriteToNoAlias ) )
93+ }
94+ }
10195}
10296
10397impl TryFrom < RawResolveOptions > for Resolve {
@@ -205,6 +199,50 @@ impl TryFrom<RawResolveTsconfigOptions> for TsconfigOptions {
205199 }
206200}
207201
202+ #[ cfg( test) ]
203+ mod tests {
204+ use napi:: Either ;
205+ use rspack_core:: { Alias , AliasMap } ;
206+
207+ use super :: { RawAliasOptionItem , normalize_alias} ;
208+
209+ #[ test]
210+ fn normalize_alias_returns_none_for_absent_alias ( ) {
211+ assert_eq ! ( normalize_alias( None ) . unwrap( ) , None ) ;
212+ }
213+
214+ #[ test]
215+ fn normalize_alias_normalizes_string_and_false_redirects ( ) {
216+ let alias = normalize_alias ( Some ( Either :: A ( vec ! [ RawAliasOptionItem {
217+ path: "@" . to_string( ) ,
218+ redirect: vec![
219+ serde_json:: Value :: String ( "/src" . to_string( ) ) ,
220+ serde_json:: Value :: Bool ( false ) ,
221+ ] ,
222+ } ] ) ) )
223+ . unwrap ( ) ;
224+
225+ assert_eq ! (
226+ alias,
227+ Some ( Alias :: MergeAlias ( vec![ (
228+ "@" . to_string( ) ,
229+ vec![ AliasMap :: Path ( "/src" . to_string( ) ) , AliasMap :: Ignore ] ,
230+ ) ] ) )
231+ ) ;
232+ }
233+
234+ #[ test]
235+ fn normalize_alias_rejects_true_redirects ( ) {
236+ let error = normalize_alias ( Some ( Either :: A ( vec ! [ RawAliasOptionItem {
237+ path: "@" . to_string( ) ,
238+ redirect: vec![ serde_json:: Value :: Bool ( true ) ] ,
239+ } ] ) ) )
240+ . unwrap_err ( ) ;
241+
242+ assert_eq ! ( error. to_string( ) , "Alias should not be true in @" ) ;
243+ }
244+ }
245+
208246#[ derive( Debug ) ]
209247#[ napi( object, object_to_js = false ) ]
210248pub struct RawResolveOptionsWithDependencyType {
0 commit comments