mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-08 05:08:09 +00:00
fixup! Fix optional arguments in TypeScript
Apply feedback about omittable and non-omittable arguments. Thanks @rhysd!
This commit is contained in:
parent
2384af21c1
commit
d9c559f2ca
@ -805,16 +805,30 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
|
|||||||
};
|
};
|
||||||
js.push_str(&invoc);
|
js.push_str(&invoc);
|
||||||
js.push_str("\n}");
|
js.push_str("\n}");
|
||||||
let ts_args = self
|
|
||||||
.js_arguments
|
// Determine TS parameter list
|
||||||
.iter()
|
let mut omittable = true;
|
||||||
.map(|s| if s.optional {
|
let mut ts_args = Vec::with_capacity(self.js_arguments.len());
|
||||||
format!("{}?: {}", s.name, s.type_)
|
for arg in self.js_arguments.iter().rev() {
|
||||||
|
// In TypeScript, we can mark optional parameters as omittable
|
||||||
|
// using the `?` suffix, but only if they're not followed by
|
||||||
|
// non-omittable parameters. Therefore iterate the parameter list
|
||||||
|
// in reverse and stop using the `?` suffix for optional params as
|
||||||
|
// soon as a non-optional parameter is encountered.
|
||||||
|
if arg.optional {
|
||||||
|
if omittable {
|
||||||
|
ts_args.push(format!("{}?: {}", arg.name, arg.type_));
|
||||||
} else {
|
} else {
|
||||||
format!("{}: {}", s.name, s.type_)
|
ts_args.push(format!("{}: {} | undefined", arg.name, arg.type_));
|
||||||
})
|
}
|
||||||
.collect::<Vec<_>>()
|
} else {
|
||||||
.join(", ");
|
omittable = false;
|
||||||
|
ts_args.push(format!("{}: {}", arg.name, arg.type_));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ts_args.reverse();
|
||||||
|
let ts_args = ts_args.join(", ");
|
||||||
|
|
||||||
let mut ts = if prefix.is_empty() {
|
let mut ts = if prefix.is_empty() {
|
||||||
format!("{}({})", self.js_name, ts_args)
|
format!("{}({})", self.js_name, ts_args)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user