Skip to content

Gentype support #138

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

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 46 additions & 1 deletion src/bsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ pub struct JsxSpecs {
pub v3_dependencies: Option<Vec<String>>,
}

/// Empty struct - the gentype config is loaded by bsc
#[derive(Deserialize, Debug, Clone)]
pub struct GenTypeConfig {}

/// # bsconfig.json representation
/// This is tricky, there is a lot of ambiguity. This is probably incomplete.
#[derive(Deserialize, Debug, Clone)]
Expand All @@ -157,6 +161,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 @@ -239,7 +245,17 @@ pub fn read(path: String) -> Config {
}

fn check_if_rescript11_or_higher(version: &str) -> bool {
version.split('.').next().unwrap().parse::<usize>().unwrap() >= 11
// Non stable rescript versions might contain non-numeric characters
let filter_non_number_chars = |s: &str| s.chars().filter(|c| c.is_numeric()).collect::<String>();

version
.split('.')
.next()
.map(filter_non_number_chars)
.unwrap()
.parse::<usize>()
.unwrap()
>= 11
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this cuts it - I think you can do wildcards like: rescript: *, which I think would fail to parse, and that would throw in this case. To really check this, we should probably parse the output of the rescript -v. But, I'm ok to use this for the time being, albeit with some small changes:

  • Return Result<bool> here (probably my preference)
  • Default to false
  • Or write some heavy tests around this

Copy link
Member

Choose a reason for hiding this comment

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

I see we also hacked this in at some point. But perhaps, while we're touching it, maybe we just fix it up nicely 👌 - If I have some time today, I'll try and conjure up a version as a suggestion

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will go with the Result, then we can log an error at the call site and decide how to proceed.
In this case without uncurried mode.

}

fn namespace_from_package_name(package_name: &str) -> String {
Expand Down Expand Up @@ -366,6 +382,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 +412,26 @@ 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()]);
}
}
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