Skip to content

Commit

Permalink
Add a deprecation notice about async constructors (#4402)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielronnkvist authored Jan 13, 2025
1 parent 2405ec2 commit 6167f66
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# `wasm-bindgen` Change Log
--------------------------------------------------------------------------------

## Unreleased

### Changed

* Deprecation warning when using an async function as a constructor
[#4402](https://github.com/rustwasm/wasm-bindgen/pull/4402)


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

Released 2025-01-12
Expand Down
12 changes: 12 additions & 0 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,18 @@ impl TryToTokens for ast::Export {
add_check(quote! {
let _: #wasm_bindgen::__rt::marker::CheckSupportsConstructor<#class>;
});

if self.function.r#async {
(quote_spanned! {
self.function.name_span =>
const _: () = {
#[deprecated(note = "async constructors produce invalid TS code and support will be removed in the future")]
const fn constructor() {}
constructor();
};
})
.to_tokens(into);
}
}
ast::MethodKind::Operation(operation) => match operation.kind {
ast::OperationKind::Getter(_) | ast::OperationKind::Setter(_) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/macro/ui-tests/unsupported-options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl RustStruct {
pub fn static_method() {}

#[wasm_bindgen(constructor)]
pub fn new() -> Self {
pub async fn new() -> Self {
Self { data: 0 }
}

Expand Down
8 changes: 8 additions & 0 deletions crates/macro/ui-tests/unsupported-options.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
warning: use of deprecated function `RustStruct::new::{closure#0}::_::constructor`: async constructors produce invalid TS code and support will be removed in the future
--> ui-tests/unsupported-options.rs:15:18
|
15 | pub async fn new() -> Self {
| ^^^
|
= note: `#[warn(deprecated)]` on by default

error[E0277]: JavaScript constructors are not supported for `RustEnum`
--> ui-tests/unsupported-options.rs:56:12
|
Expand Down
1 change: 1 addition & 0 deletions tests/wasm/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub struct AsyncStruct;
#[wasm_bindgen]
impl AsyncStruct {
#[wasm_bindgen(constructor)]
#[allow(deprecated)]
pub async fn new() -> AsyncStruct {
AsyncStruct
}
Expand Down

0 comments on commit 6167f66

Please sign in to comment.