diff --git a/.travis.yml b/.travis.yml index de257e3fedb..4788a5adec4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -120,6 +120,11 @@ matrix: - if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then aws s3 sync ~/$TRAVIS_BUILD_NUMBER s3://wasm-bindgen-ci/$TRAVIS_BUILD_NUMBER; fi if: branch = master + # The `cli-support` crate's tests pass + - name: "test cli-support crate" + script: cargo test -p wasm-bindgen-cli-support + if: branch = master + # The `web-sys` crate's tests pass - name: "test web-sys crate" install: diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 65258e06c9e..cf661e8f4af 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -1858,7 +1858,7 @@ impl<'a> Context<'a> { let class = self.import_identifier(name); let op = match &method_data.kind { decode::MethodKind::Constructor => { - return Ok(ImportTarget::Constructor(class.to_string())) + return Ok(ImportTarget::Constructor(class.to_string())); } decode::MethodKind::Operation(op) => op, }; @@ -2649,7 +2649,9 @@ impl<'a> Import<'a> { fn generate_identifier(name: &str, used_names: &mut HashMap) -> String { let cnt = used_names.entry(name.to_string()).or_insert(0); *cnt += 1; - if *cnt == 1 { + // We want to mangle `default` at once, so we can support default exports and don't generate + // invalid glue code like this: `import { default } from './module';`. + if *cnt == 1 && name != "default" { name.to_string() } else { format!("{}{}", name, cnt) @@ -2668,3 +2670,24 @@ fn format_doc_comments(comments: &[&str], js_doc_comments: Option) -> St }; format!("/**\n{}{}*/\n", body, doc) } + +#[test] +fn test_generate_identifier() { + let mut used_names: HashMap = HashMap::new(); + assert_eq!( + generate_identifier("someVar", &mut used_names), + "someVar".to_string() + ); + assert_eq!( + generate_identifier("someVar", &mut used_names), + "someVar2".to_string() + ); + assert_eq!( + generate_identifier("default", &mut used_names), + "default1".to_string() + ); + assert_eq!( + generate_identifier("default", &mut used_names), + "default2".to_string() + ); +} diff --git a/tests/wasm/import_class.js b/tests/wasm/import_class.js index ebc29759266..b86ab92be04 100644 --- a/tests/wasm/import_class.js +++ b/tests/wasm/import_class.js @@ -30,7 +30,7 @@ class Construct { Construct.internal_string = ''; exports.Construct = Construct; -exports.NewConstructors = class { +class NewConstructor { constructor(field) { this.field = field; } @@ -38,7 +38,10 @@ exports.NewConstructors = class { get() { return this.field + 1; } -}; +} + +exports.NewConstructors = NewConstructor; +exports.default = NewConstructor; let switch_called = false; class SwitchMethods { diff --git a/tests/wasm/import_class.rs b/tests/wasm/import_class.rs index 60c7668d2a2..080e8f1ce91 100644 --- a/tests/wasm/import_class.rs +++ b/tests/wasm/import_class.rs @@ -29,6 +29,13 @@ extern "C" { #[wasm_bindgen(method)] fn get(this: &NewConstructors) -> i32; + #[wasm_bindgen(js_name = default)] + type RenamedTypes; + #[wasm_bindgen(constructor, js_class = default)] + fn new(arg: i32) -> RenamedTypes; + #[wasm_bindgen(method, js_class = default)] + fn get(this: &RenamedTypes) -> i32; + fn switch_methods_a(); fn switch_methods_b(); type SwitchMethods; @@ -125,6 +132,12 @@ fn new_constructors() { assert_eq!(f.get(), 2); } +#[wasm_bindgen_test] +fn rename_type() { + let f = RenamedTypes::new(1); + assert_eq!(f.get(), 2); +} + #[wasm_bindgen_test] fn switch_methods() { assert!(!switch_methods_called());