diff --git a/src/bsconfig.rs b/src/bsconfig.rs index 3abcf406..f5bf905a 100644 --- a/src/bsconfig.rs +++ b/src/bsconfig.rs @@ -133,6 +133,9 @@ pub struct JsxSpecs { pub v3_dependencies: Option>, } +/// We do not care about the internal structure because the gentype config is loaded by bsc. +pub type GenTypeConfig = serde_json::Value; + /// # bsconfig.json representation /// This is tricky, there is a lot of ambiguity. This is probably incomplete. #[derive(Deserialize, Debug, Clone)] @@ -157,6 +160,8 @@ pub struct Config { pub namespace: Option, pub jsx: Option, pub uncurried: Option, + #[serde(rename = "gentypeconfig")] + pub gentype_config: Option, // this is a new feature of rewatch, and it's not part of the bsconfig.json spec #[serde(rename = "namespace-entry")] pub namespace_entry: Option, @@ -238,8 +243,15 @@ pub fn read(path: String) -> Config { .expect("Errors reading bsconfig") } -fn check_if_rescript11_or_higher(version: &str) -> bool { - version.split('.').next().unwrap().parse::().unwrap() >= 11 +fn check_if_rescript11_or_higher(version: &str) -> Result { + version + .split('.') + .next() + .and_then(|s| s.parse::().ok()) + .map_or( + Err("Could not parse version".to_string()), + |major| Ok(major >= 11), + ) } fn namespace_from_package_name(package_name: &str) -> String { @@ -331,14 +343,17 @@ impl Config { } pub fn get_uncurried_args(&self, version: &str) -> Vec { - if check_if_rescript11_or_higher(version) { - match self.uncurried.to_owned() { + match check_if_rescript11_or_higher(version) { + Ok(true) => match self.uncurried.to_owned() { // v11 is always uncurried except iff explicitly set to false in the root rescript.json Some(false) => vec![], _ => vec!["-uncurried".to_string()], + }, + Ok(false) => vec![], + Err(_) => { + eprintln!("Could not establish Rescript Version number for uncurried mode. Defaulting to Rescript < 11, disabling uncurried mode. Please specify an exact version if you need > 11 and default uncurried mode. Version: {}", version); + vec![] } - } else { - vec![] } } @@ -366,6 +381,13 @@ impl Config { .or(self.suffix.to_owned()) .unwrap_or(".js".to_string()) } + + pub fn get_gentype_arg(&self) -> Vec { + match &self.gentype_config { + Some(_) => vec!["-bs-gentype".to_string()], + None => vec![], + } + } } #[cfg(test)] @@ -389,4 +411,49 @@ mod tests { assert_eq!(config.get_suffix(), ".mjs"); assert_eq!(config.get_module(), "es6"); } + + #[test] + fn test_detect_gentypeconfig() { + let json = r#" + { + "name": "my-monorepo", + "sources": [ { "dir": "src/", "subdirs": true } ], + "package-specs": [ { "module": "es6", "in-source": true } ], + "suffix": ".mjs", + "pinned-dependencies": [ "@teamwalnut/app" ], + "bs-dependencies": [ "@teamwalnut/app" ], + "gentypeconfig": { + "module": "esmodule", + "generatedFileExtension": ".gen.tsx" + } + } + "#; + + let config = serde_json::from_str::(json).unwrap(); + assert_eq!(config.gentype_config.is_some(), true); + assert_eq!(config.get_gentype_arg(), vec!["-bs-gentype".to_string()]); + } + + #[test] + fn test_check_if_rescript11_or_higher() { + assert_eq!(check_if_rescript11_or_higher("11.0.0"), Ok(true)); + assert_eq!(check_if_rescript11_or_higher("11.0.1"), Ok(true)); + assert_eq!(check_if_rescript11_or_higher("11.1.0"), Ok(true)); + + assert_eq!(check_if_rescript11_or_higher("12.0.0"), Ok(true)); + + assert_eq!(check_if_rescript11_or_higher("10.0.0"), Ok(false)); + assert_eq!(check_if_rescript11_or_higher("9.0.0"), Ok(false)); + } + + #[test] + fn test_check_if_rescript11_or_higher_misc() { + assert_eq!(check_if_rescript11_or_higher("11"), Ok(true)); + assert_eq!(check_if_rescript11_or_higher("12.0.0-alpha.4"), Ok(true)); + + match check_if_rescript11_or_higher("*") { + Ok(_) => unreachable!("Should not parse"), + Err(_) => assert!(true), + } + } } diff --git a/src/build/compile.rs b/src/build/compile.rs index ea52e6a3..4b069279 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -434,6 +434,7 @@ pub fn compiler_args( let jsx_module_args = root_config.get_jsx_module_args(); let jsx_mode_args = root_config.get_jsx_mode_args(); let uncurried_args = root_config.get_uncurried_args(version); + let gentype_arg = root_config.get_gentype_arg(); let warning_args: Vec = match config.warnings.to_owned() { None => vec![], @@ -494,6 +495,7 @@ pub fn compiler_args( read_cmi_args, vec!["-I".to_string(), ".".to_string()], deps.concat(), + gentype_arg, jsx_args, jsx_module_args, jsx_mode_args, diff --git a/src/build/packages.rs b/src/build/packages.rs index b5b3ec10..ee1ea170 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -864,6 +864,7 @@ mod test { namespace: None, jsx: None, uncurried: None, + gentype_config: None, namespace_entry: None, allowed_dependents, },