diff --git a/crates/backend/src/ast.rs b/crates/backend/src/ast.rs index 2e442c0b..e82d8717 100644 --- a/crates/backend/src/ast.rs +++ b/crates/backend/src/ast.rs @@ -618,6 +618,14 @@ impl Import { } impl ImportKind { + pub fn fits_on_impl(&self) -> bool { + match *self { + ImportKind::Function(_) => true, + ImportKind::Static(_) => false, + ImportKind::Type(_) => false, + } + } + fn shared(&self) -> shared::ImportKind { match *self { ImportKind::Function(ref f) => shared::ImportKind::Function(f.shared()), diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index 6019313d..97cb18fb 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -44,14 +44,16 @@ impl ToTokens for ast::Program { } } for i in self.imports.iter() { - match i.js_namespace { - Some(ns) if types.contains(&ns) => { + DescribeImport(&i.kind).to_tokens(tokens); + + if let Some(ns) = i.js_namespace { + if types.contains(&ns) && i.kind.fits_on_impl() { let kind = &i.kind; (quote! { impl #ns { #kind } }).to_tokens(tokens); } - _ => i.kind.to_tokens(tokens), } - DescribeImport(&i.kind).to_tokens(tokens); + + i.kind.to_tokens(tokens); } for e in self.enums.iter() { e.to_tokens(tokens); diff --git a/tests/all/imports.rs b/tests/all/imports.rs index f76e4d6a..915a01e2 100644 --- a/tests/all/imports.rs +++ b/tests/all/imports.rs @@ -464,3 +464,40 @@ fn rust_keyword() { "#) .test(); } + +#[test] +fn rust_keyword2() { + project() + .debug(false) + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section, wasm_import_module)] + + extern crate wasm_bindgen; + + use wasm_bindgen::prelude::*; + + #[wasm_bindgen(module = "./test")] + extern { + pub type bar; + #[wasm_bindgen(js_namespace = bar, js_name = foo)] + static FOO: JsValue; + } + + #[wasm_bindgen] + pub fn run() { + assert_eq!(FOO.as_f64(), Some(3.0)); + } + "#) + .file("test.ts", r#" + import { run } from "./out"; + + export const bar = { + foo: 3, + }; + + export function test() { + run(); + } + "#) + .test(); +}