Conversation
…development easier Implement support for ReScript language rescript support devcontainer lock integer types snapshot decoded variable name snapshot type alias snapshot and fix enum alias snapshot simple enum case serialize as string serialized as serde url serde 8601 serde default struct optional type alias char fix double optional fix type override comment unused code default decorators cleanup Improve ReScript support and add tests Add 2 rescript tests one more test one more test add test new test change Vec<u8> conversion remove unnecessary file add namespacing to anonymous structs add mention of ReScript into readme remove devcontainer config
51c1176 to
eec36e3
Compare
| let combined_decorators: Vec<String> = self | ||
| .default_decorators | ||
| .iter() | ||
| .chain(rescript_decs.iter()) | ||
| .cloned() | ||
| .collect(); | ||
| combined_decorators.join(" ") |
There was a problem hiding this comment.
You can use Itertools::join
self.default_decorators
.iter()
.chain(rescript_decs.iter())
.join(" ")
| // if self.custom_translations(mapped).is_some() { | ||
| // self.types_for_custom_json_translation | ||
| // .insert(mapped.to_string(), BTreeSet::new()); | ||
| // } |
There was a problem hiding this comment.
Is this a leftover or it's supposed to be uncommented?
| let _hashtype = match rtype1.as_ref() { | ||
| RustType::Simple { id } if generic_types.contains(id) => { | ||
| return Err(RustTypeFormatError::GenericKeyForbiddenInTS(id.clone())); | ||
| } | ||
| _ => self.format_type(rtype1, generic_types)?, | ||
| }; |
There was a problem hiding this comment.
A doubt: in Rescript Dict is "only" with string keys, right? Leave alone the madness about using numbers that are automagically converted into strings, and forget about symbols, the intrinsic logic here forbids the use of arbitrary types for rtype1 or that should be actually checked to be some kind of string?
| writeln!( | ||
| w, | ||
| "type {}{} = {}\n", | ||
| rescript_keyword_aware_rename(ty.id.renamed.to_camel_case()), | ||
| if !ty.generic_types.is_empty() { | ||
| format!( | ||
| "<{}>", | ||
| ty.generic_types | ||
| .iter() | ||
| .map(|s| format!("'{}", s.to_camel_case())) | ||
| .join(", ") | ||
| ) | ||
| } else { | ||
| Default::default() | ||
| }, | ||
| if ty.r#type.is_optional() { | ||
| format!("option<{}>", r#type) | ||
| } else { | ||
| r#type | ||
| }, | ||
| )?; |
There was a problem hiding this comment.
I think you could split the two last formatted string into separate expression in order to make the code a bit more clear. I also optimized a bit to avoid string allocation, but feel free to do something in between to keep the code more simple.
let generics = fmt::from_fn(|f| {
if !ty.generic_types.is_empty() {
f.write_char('<')?;
fn format_generic(generic_type: &String) -> impl fmt::Display + use<'_> {
fmt::from_fn(move |f| write!(f, "'{}", generic_type.to_camel_case()))
}
let mut generic_types = ty.generic_types.iter();
if let Some(generic_type) = generic_types.next() {
write!(f, "{}", format_generic(generic_type))?;
for generic_type in generic_types {
write!(f, ", {}", format_generic(generic_type))?;
}
}
f.write_char('>')?;
}
Ok(())
});
let type_writer = fmt::from_fn(|f| {
if ty.r#type.is_optional() {
write!(f, "option<{}>", r#type)
} else {
f.write_str(&r#type)
}
});
writeln!(
w,
"type {}{} = {}\n",
rescript_keyword_aware_rename(ty.id.renamed.to_camel_case()),
generics,
type_writer,
)?;| rescript_keyword_aware_rename(type_name), | ||
| generic_parameters | ||
| )?; | ||
| // writeln!(w, "type t = {{")?; |
| .iter() | ||
| .try_for_each(|f| self.write_field(w, f, rs.generic_types.as_slice()))?; | ||
|
|
||
| // writeln!(w, "}}\n")?; |
| if RESCRIPT_KEYWORDS.contains(&name.as_ref()) { | ||
| Cow::Owned(format!("\\\"{name}\"")) | ||
| } else { | ||
| // If name contains hyphen | ||
| if name.contains('-') { | ||
| Cow::Owned(format!("\\\"{name}\"")) | ||
| } else { | ||
| name | ||
| } | ||
| } |
There was a problem hiding this comment.
It can be simplified to
if RESCRIPT_KEYWORDS.contains(&name.as_ref()) || name.contains('-') {
Cow::Owned(format!("\\\"{name}\""))
} else {
name
}
This PR adds initial support for ReScript language, including almost all of the tests.