@@ -27,6 +27,13 @@ pub struct PackageSource {
2727 pub type_ : Option < String > ,
2828}
2929
30+ pub fn get_type ( s : & Source ) -> Option < String > {
31+ match s {
32+ Source :: Shorthand ( _) => None ,
33+ Source :: Qualified ( PackageSource { type_, .. } ) => type_. clone ( ) ,
34+ }
35+ }
36+
3037/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
3138/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
3239/// folders, and optional subdirs, where potentially, the subdirs recurse or not.
@@ -39,7 +46,7 @@ pub fn to_qualified_without_children(s: &Source, sub_path: Option<PathBuf>) -> P
3946 . to_string_lossy ( )
4047 . to_string ( ) ,
4148 subdirs : None ,
42- type_ : None ,
49+ type_ : get_type ( s ) ,
4350 } ,
4451 Source :: Qualified ( PackageSource {
4552 dir,
@@ -74,6 +81,31 @@ pub enum Source {
7481 Shorthand ( String ) ,
7582 Qualified ( PackageSource ) ,
7683}
84+
85+ impl Source {
86+ /// When reading, we should propagate the sources all the way through the tree
87+ pub fn get_type ( & self ) -> Option < String > {
88+ match self {
89+ Source :: Shorthand ( _) => None ,
90+ Source :: Qualified ( PackageSource { type_, .. } ) => type_. clone ( ) ,
91+ }
92+ }
93+ pub fn set_type ( & self , type_ : Option < String > ) -> Source {
94+ match ( self , type_) {
95+ ( Source :: Shorthand ( dir) , Some ( type_) ) => Source :: Qualified ( PackageSource {
96+ dir : dir. to_string ( ) ,
97+ subdirs : None ,
98+ type_ : Some ( type_) ,
99+ } ) ,
100+ ( Source :: Qualified ( package_source) , type_) => Source :: Qualified ( PackageSource {
101+ type_,
102+ ..package_source. clone ( )
103+ } ) ,
104+ ( source, _) => source. clone ( ) ,
105+ }
106+ }
107+ }
108+
77109impl Eq for Source { }
78110
79111#[ derive( Deserialize , Debug , Clone ) ]
@@ -412,6 +444,39 @@ mod tests {
412444 assert_eq ! ( config. get_module( ) , "es6" ) ;
413445 }
414446
447+ #[ test]
448+ fn test_sources ( ) {
449+ let json = r#"
450+ {
451+ "name": "@rescript/core",
452+ "version": "0.5.0",
453+ "sources": {
454+ "dir": "test",
455+ "subdirs": ["intl"],
456+ "type": "dev"
457+ },
458+ "suffix": ".mjs",
459+ "package-specs": {
460+ "module": "esmodule",
461+ "in-source": true
462+ },
463+ "bs-dev-dependencies": ["@rescript/tools"],
464+ "warnings": {
465+ "error": "+101"
466+ }
467+ }
468+ "# ;
469+
470+ let config = serde_json:: from_str :: < Config > ( json) . unwrap ( ) ;
471+ if let OneOrMore :: Single ( source) = config. sources {
472+ let source = to_qualified_without_children ( & source, None ) ;
473+ assert_eq ! ( source. type_, Some ( String :: from( "dev" ) ) ) ;
474+ } else {
475+ dbg ! ( config. sources) ;
476+ unreachable ! ( )
477+ }
478+ }
479+
415480 #[ test]
416481 fn test_detect_gentypeconfig ( ) {
417482 let json = r#"
@@ -430,7 +495,7 @@ mod tests {
430495 "# ;
431496
432497 let config = serde_json:: from_str :: < Config > ( json) . unwrap ( ) ;
433- assert_eq ! ( config. gentype_config. is_some( ) , true ) ;
498+ assert ! ( config. gentype_config. is_some( ) ) ;
434499 assert_eq ! ( config. get_gentype_arg( ) , vec![ "-bs-gentype" . to_string( ) ] ) ;
435500 }
436501
0 commit comments