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(typescript) Getters correctly define the inner results. #2909

Merged
merged 2 commits into from May 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/cli-support/src/js/binding.rs
Expand Up @@ -229,6 +229,7 @@ impl<'a, 'b> Builder<'a, 'b> {
asyncness,
);
let js_doc = self.js_doc_comments(&function_args, &arg_tys, &ts_ret_ty);

Ok(JsFunction {
code,
ts_sig,
Expand Down
8 changes: 7 additions & 1 deletion crates/cli-support/src/js/mod.rs
Expand Up @@ -2504,9 +2504,10 @@ impl<'a> Context<'a> {
false => None,
};

let 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("export function ");
Expand All @@ -2518,6 +2519,7 @@ impl<'a> Context<'a> {
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);
Expand All @@ -2526,6 +2528,7 @@ impl<'a> Context<'a> {
exported.push(&docs, "constructor", "", &code, ts_sig);
}
AuxExportKind::Getter { class, field, .. } => {
let 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()),
Expand All @@ -2537,6 +2540,7 @@ impl<'a> Context<'a> {
exported.push_getter(&docs, field, &code, ret_ty);
}
AuxExportKind::Setter { class, field, .. } => {
let docs = format_doc_comments(&export.comments, None);
let arg_ty = match export.generate_typescript {
true => Some(ts_arg_tys[0].as_str()),
false => None,
Expand All @@ -2545,10 +2549,12 @@ impl<'a> Context<'a> {
exported.push_setter(&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);
}
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);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/src/wit/mod.rs
Expand Up @@ -806,7 +806,7 @@ impl<'a> Context<'a> {
arguments: vec![Descriptor::I32],
shim_idx: 0,
ret: descriptor.clone(),
inner_ret: None,
inner_ret: Some(descriptor.clone()),
};
let getter_id = self.export_adapter(getter_id, getter_descriptor)?;
self.aux.export_map.insert(
Expand Down
19 changes: 19 additions & 0 deletions crates/typescript-tests/src/getters_setters.rs
Expand Up @@ -66,3 +66,22 @@ impl ColorWithGetterAndSetter {
};
}
}

#[wasm_bindgen]
pub struct ColorWithReadonly {
#[wasm_bindgen(readonly)]
pub r: f64,
#[wasm_bindgen(readonly)]
pub g: f64,
#[wasm_bindgen(readonly)]
pub b: f64,
pub a: u8,
}

#[wasm_bindgen]
impl ColorWithReadonly {
#[wasm_bindgen(constructor)]
pub fn new(r: f64, g: f64, b: f64) -> ColorWithReadonly {
Self { r, b, g, a: 0 }
}
}
4 changes: 4 additions & 0 deletions crates/typescript-tests/src/getters_setters.ts
Expand Up @@ -9,3 +9,7 @@ colorWithSetter.r = 1;
const colorWithGetterAndSetter: wbg.ColorWithGetterAndSetter = new wbg.ColorWithGetterAndSetter;
colorWithGetterAndSetter.r = 1;
const _b = colorWithGetterAndSetter.r;

const colorWithReadonly: wbg.ColorWithReadonly = new wbg.ColorWithReadonly(1, 2, 3);
const _r: number = colorWithReadonly.r;
colorWithReadonly.a = 4;