@@ -27,6 +27,13 @@ pub struct PackageSource {
27
27
pub type_ : Option < String > ,
28
28
}
29
29
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
+
30
37
/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
31
38
/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
32
39
/// 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
39
46
. to_string_lossy ( )
40
47
. to_string ( ) ,
41
48
subdirs : None ,
42
- type_ : None ,
49
+ type_ : get_type ( s ) ,
43
50
} ,
44
51
Source :: Qualified ( PackageSource {
45
52
dir,
@@ -74,6 +81,31 @@ pub enum Source {
74
81
Shorthand ( String ) ,
75
82
Qualified ( PackageSource ) ,
76
83
}
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
+
77
109
impl Eq for Source { }
78
110
79
111
#[ derive( Deserialize , Debug , Clone ) ]
@@ -412,6 +444,39 @@ mod tests {
412
444
assert_eq ! ( config. get_module( ) , "es6" ) ;
413
445
}
414
446
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
+
415
480
#[ test]
416
481
fn test_detect_gentypeconfig ( ) {
417
482
let json = r#"
@@ -430,7 +495,7 @@ mod tests {
430
495
"# ;
431
496
432
497
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( ) ) ;
434
499
assert_eq ! ( config. get_gentype_arg( ) , vec![ "-bs-gentype" . to_string( ) ] ) ;
435
500
}
436
501
0 commit comments