Skip to content

Commit 1f7942d

Browse files
authored
Fix #[wasm_bindgen(js_name = default)] for module imports (#3823)
1 parent 1f019db commit 1f7942d

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
* Fixed temporary folder detection by `wasm-bindgen-test-runner` on MacOS.
4343
[#3817](https://github.com/rustwasm/wasm-bindgen/pull/3817)
4444

45+
* Fixed using `#[wasm_bindgen(js_name = default)]` with `#[wasm_bindgen(module = ...)]`.
46+
[#3823](https://github.com/rustwasm/wasm-bindgen/pull/3823)
47+
4548
## [0.2.90](https://github.com/rustwasm/wasm-bindgen/compare/0.2.89...0.2.90)
4649

4750
Released 2024-01-06

crates/macro-support/src/parser.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ impl<'a> ConvertToAst<(&ast::Program, BindgenAttrs, &'a Option<ast::ImportModule
505505
false,
506506
None,
507507
false,
508+
Some(&["default"]),
508509
)?
509510
.0;
510511
let catch = opts.catch().is_some();
@@ -793,14 +794,18 @@ impl ConvertToAst<BindgenAttrs> for syn::ItemFn {
793794
false,
794795
None,
795796
false,
797+
None,
796798
)?;
797799
attrs.check_used();
798800
Ok(ret.0)
799801
}
800802
}
801803

802-
pub(crate) fn is_js_keyword(keyword: &str) -> bool {
803-
JS_KEYWORDS.contains(&keyword)
804+
pub(crate) fn is_js_keyword(keyword: &str, skip: Option<&[&str]>) -> bool {
805+
JS_KEYWORDS
806+
.iter()
807+
.filter(|keyword| skip.filter(|skip| skip.contains(keyword)).is_none())
808+
.any(|this| *this == keyword)
804809
}
805810

806811
/// Construct a function (and gets the self type if appropriate) for our AST from a syn function.
@@ -814,6 +819,7 @@ fn function_from_decl(
814819
allow_self: bool,
815820
self_ty: Option<&Ident>,
816821
is_from_impl: bool,
822+
skip_keywords: Option<&[&str]>,
817823
) -> Result<(ast::Function, Option<ast::MethodSelf>), Diagnostic> {
818824
if sig.variadic.is_some() {
819825
bail_span!(sig.variadic, "can't #[wasm_bindgen] variadic functions");
@@ -852,7 +858,7 @@ fn function_from_decl(
852858
let replace_colliding_arg = |i: &mut syn::PatType| {
853859
if let syn::Pat::Ident(ref mut i) = *i.pat {
854860
let ident = i.ident.to_string();
855-
if is_js_keyword(ident.as_str()) {
861+
if is_js_keyword(ident.as_str(), skip_keywords) {
856862
i.ident = Ident::new(format!("_{}", ident).as_str(), i.ident.span());
857863
}
858864
}
@@ -889,29 +895,33 @@ fn function_from_decl(
889895
syn::ReturnType::Type(_, ty) => Some(replace_self(*ty)),
890896
};
891897

892-
let (name, name_span, renamed_via_js_name) = if let Some((js_name, js_name_span)) =
893-
opts.js_name()
894-
{
895-
let kind = operation_kind(opts);
896-
let prefix = match kind {
897-
OperationKind::Setter(_) => "set_",
898-
_ => "",
899-
};
900-
let name = if prefix.is_empty() && opts.method().is_none() && is_js_keyword(js_name) {
901-
format!("_{}", js_name)
898+
let (name, name_span, renamed_via_js_name) =
899+
if let Some((js_name, js_name_span)) = opts.js_name() {
900+
let kind = operation_kind(opts);
901+
let prefix = match kind {
902+
OperationKind::Setter(_) => "set_",
903+
_ => "",
904+
};
905+
let name = if prefix.is_empty()
906+
&& opts.method().is_none()
907+
&& is_js_keyword(js_name, skip_keywords)
908+
{
909+
format!("_{}", js_name)
910+
} else {
911+
format!("{}{}", prefix, js_name)
912+
};
913+
(name, js_name_span, true)
902914
} else {
903-
format!("{}{}", prefix, js_name)
904-
};
905-
(name, js_name_span, true)
906-
} else {
907-
let name =
908-
if !is_from_impl && opts.method().is_none() && is_js_keyword(&decl_name.to_string()) {
915+
let name = if !is_from_impl
916+
&& opts.method().is_none()
917+
&& is_js_keyword(&decl_name.to_string(), skip_keywords)
918+
{
909919
format!("_{}", decl_name)
910920
} else {
911921
decl_name.to_string()
912922
};
913-
(name, decl_name.span(), false)
914-
};
923+
(name, decl_name.span(), false)
924+
};
915925
Ok((
916926
ast::Function {
917927
arguments,
@@ -1188,6 +1198,7 @@ impl<'a> MacroParse<&ClassMarker> for &'a mut syn::ImplItemFn {
11881198
true,
11891199
Some(class),
11901200
true,
1201+
None,
11911202
)?;
11921203
let method_kind = if opts.constructor().is_some() {
11931204
ast::MethodKind::Constructor

0 commit comments

Comments
 (0)