Skip to content

Commit

Permalink
fix(cli-support) Differentiate JS docs and TS docs when generating ad…
Browse files Browse the repository at this point in the history
…apters.

A bug has been raised in
rustwasm#2947, which is the
consequence of a bug introduced in
rustwasm#2909.

This patch fixes it by differentiating JS docs and TS docs
clearly. The `Context::push_setter` and `Context::push_getter` methods
now receive a `js_docs` and a `ts_docs`, which prevent any confusions.
  • Loading branch information
Hywan committed Jun 20, 2022
1 parent af2a7b9 commit d529443
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions crates/cli-support/src/js/mod.rs
Expand Up @@ -2564,59 +2564,68 @@ impl<'a> Context<'a> {
false => None,
};

let js_docs = format_doc_comments(&export.comments, Some(js_doc));

match &export.kind {
AuxExportKind::Function(name) => {
let docs = format_doc_comments(&export.comments, Some(js_doc));

if let Some(ts_sig) = ts_sig {
self.typescript.push_str(&docs);
self.typescript.push_str(&js_docs);
self.typescript.push_str("export function ");
self.typescript.push_str(&name);
self.typescript.push_str(ts_sig);
self.typescript.push_str(";\n");
}
self.export(&name, &format!("function{}", code), Some(&docs))?;

self.export(&name, &format!("function{}", code), Some(&js_docs))?;
self.globals.push_str("\n");
}
AuxExportKind::Constructor(class) => {
let docs = format_doc_comments(&export.comments, Some(js_doc));
let exported = require_class(&mut self.exported_classes, class);

if exported.has_constructor {
bail!("found duplicate constructor for class `{}`", class);
}

exported.has_constructor = true;
exported.push(&docs, "constructor", "", &code, ts_sig);
exported.push(&js_docs, "constructor", "", &code, ts_sig);
}
AuxExportKind::Getter { class, field, .. } => {
let docs = format_doc_comments(&export.comments, None);
let ts_docs = format_doc_comments(&export.comments, None);
let ret_ty = match export.generate_typescript {
true => match &ts_ret_ty {
Some(s) => Some(s.as_str()),
_ => None,
},
false => None,
};

let exported = require_class(&mut self.exported_classes, class);
exported.push_getter(&docs, field, &code, ret_ty);
exported.push_getter(&js_docs, &ts_docs, field, &code, ret_ty);
}
AuxExportKind::Setter { class, field, .. } => {
let docs = format_doc_comments(&export.comments, None);
let ts_docs = format_doc_comments(&export.comments, None);
let arg_ty = match export.generate_typescript {
true => Some(ts_arg_tys[0].as_str()),
false => None,
};
let exported = require_class(&mut self.exported_classes, class);
exported.push_setter(&docs, field, &code, arg_ty, might_be_optional_field);

exported.push_setter(
&js_docs,
&ts_docs,
field,
&code,
arg_ty,
might_be_optional_field,
);
}
AuxExportKind::StaticFunction { class, name } => {
let docs = format_doc_comments(&export.comments, Some(js_doc));
let exported = require_class(&mut self.exported_classes, class);
exported.push(&docs, name, "static ", &code, ts_sig);
exported.push(&js_docs, name, "static ", &code, ts_sig);
}
AuxExportKind::Method { class, name, .. } => {
let docs = format_doc_comments(&export.comments, Some(js_doc));
let exported = require_class(&mut self.exported_classes, class);
exported.push(&docs, name, "", &code, ts_sig);
exported.push(&js_docs, name, "", &code, ts_sig);
}
}
}
Expand Down Expand Up @@ -3836,27 +3845,38 @@ impl ExportedClass {

/// Used for adding a getter to a class, mainly to ensure that TypeScript
/// generation is handled specially.
fn push_getter(&mut self, docs: &str, field: &str, js: &str, ret_ty: Option<&str>) {
self.push_accessor(docs, field, js, "get ");
fn push_getter(
&mut self,
js_docs: &str,
ts_docs: &str,
field: &str,
js: &str,
ret_ty: Option<&str>,
) {
self.push_accessor(js_docs, field, js, "get ");

if let Some(ret_ty) = ret_ty {
self.push_accessor_ts(docs, field, ret_ty, false);
self.push_accessor_ts(ts_docs, field, ret_ty, false);
}

self.readable_properties.push(field.to_string());
}

/// Used for adding a setter to a class, mainly to ensure that TypeScript
/// generation is handled specially.
fn push_setter(
&mut self,
docs: &str,
js_docs: &str,
ts_docs: &str,
field: &str,
js: &str,
ret_ty: Option<&str>,
might_be_optional_field: bool,
) {
self.push_accessor(docs, field, js, "set ");
self.push_accessor(js_docs, field, js, "set ");

if let Some(ret_ty) = ret_ty {
let is_optional = self.push_accessor_ts(docs, field, ret_ty, true);
let is_optional = self.push_accessor_ts(ts_docs, field, ret_ty, true);
*is_optional = might_be_optional_field;
}
}
Expand Down

0 comments on commit d529443

Please sign in to comment.