Don't try to codegen static into an impl

Closes #194
This commit is contained in:
Alex Crichton 2018-05-11 08:28:09 -07:00
parent 06664b34ce
commit ca8809b4e9
3 changed files with 51 additions and 4 deletions

View File

@ -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()),

View File

@ -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);

View File

@ -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();
}