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

fix: Optional JSDoc @param #3577

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
* Fixed bug allowing JS primitives to be returned from exported constructors.
[#3562](https://github.com/rustwasm/wasm-bindgen/pull/3562)

* Fixed optional parameters in JSDoc.
[#3577](https://github.com/rustwasm/wasm-bindgen/pull/3577)

## [0.2.87](https://github.com/rustwasm/wasm-bindgen/compare/0.2.86...0.2.87)

Released 2023-06-12.
Expand Down
32 changes: 25 additions & 7 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,36 @@ impl<'a, 'b> Builder<'a, 'b> {
ts_ret: &Option<String>,
variadic: bool,
) -> String {
let mut ret = String::new();
let (variadic_arg, fn_arg_names) = match arg_names.split_last() {
Some((last, args)) if variadic => (Some(last), args),
_ => (None, arg_names),
};
for (name, ty) in fn_arg_names.iter().zip(arg_tys) {
ret.push_str("@param {");
adapter2ts(ty, &mut ret);
ret.push_str("} ");
ret.push_str(name);
ret.push('\n');

let mut omittable = true;
let mut js_doc_args = Vec::new();

for (name, ty) in fn_arg_names.iter().zip(arg_tys).rev() {
let mut arg = "@param {".to_string();

adapter2ts(ty, &mut arg);
arg.push_str("} ");
match ty {
AdapterType::Option(..) if omittable => {
arg.push('[');
arg.push_str(name);
arg.push(']');
}
_ => {
omittable = false;
arg.push_str(name);
}
}
arg.push('\n');
js_doc_args.push(arg);
}

let mut ret: String = js_doc_args.into_iter().rev().collect();

if let (Some(name), Some(ty)) = (variadic_arg, arg_tys.last()) {
ret.push_str("@param {...");
adapter2ts(ty, &mut ret);
Expand Down