Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gentype support #138

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 73 additions & 6 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ pub struct JsxSpecs {
pub v3_dependencies: Option<Vec<String>>,
}

/// 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)]
Expand All @@ -157,6 +160,8 @@ pub struct Config {
pub namespace: Option<NamespaceConfig>,
pub jsx: Option<JsxSpecs>,
pub uncurried: Option<bool>,
#[serde(rename = "gentypeconfig")]
pub gentype_config: Option<GenTypeConfig>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably not have this as an empty struct, but rather just serde::Value - such that it represents some random piece of JSON which we don't care about. Right now it implies it's empty, rather than something we don't care about.

// 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<String>,
Expand Down Expand Up @@ -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::<usize>().unwrap() >= 11
fn check_if_rescript11_or_higher(version: &str) -> Result<bool, String> {
version
.split('.')
.next()
.and_then(|s| s.parse::<usize>().ok())
.map_or(
Err("Could not parse version".to_string()),
|major| Ok(major >= 11),
)
}

fn namespace_from_package_name(package_name: &str) -> String {
Expand Down Expand Up @@ -331,14 +343,17 @@ impl Config {
}

pub fn get_uncurried_args(&self, version: &str) -> Vec<String> {
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 parse version: {}", version);
rolandpeelen marked this conversation as resolved.
Show resolved Hide resolved
vec![]
}
} else {
vec![]
}
}

Expand Down Expand Up @@ -366,6 +381,13 @@ impl Config {
.or(self.suffix.to_owned())
.unwrap_or(".js".to_string())
}

pub fn get_gentype_arg(&self) -> Vec<String> {
match &self.gentype_config {
Some(_) => vec!["-bs-gentype".to_string()],
None => vec![],
}
}
}

#[cfg(test)]
Expand All @@ -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::<Config>(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),
}
}
}
2 changes: 2 additions & 0 deletions src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = match config.warnings.to_owned() {
None => vec![],
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/build/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ mod test {
namespace: None,
jsx: None,
uncurried: None,
gentype_config: None,
namespace_entry: None,
allowed_dependents,
},
Expand Down
Loading