Skip to content

Commit

Permalink
fix: Optional JSDoc @param (#3577)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Aug 29, 2023
1 parent e7cfba5 commit 99983c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
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

0 comments on commit 99983c8

Please sign in to comment.