Add support for importing default exports

This commit is contained in:
mvlabat 2018-12-11 20:42:37 +02:00
parent 7768328fc4
commit 371e864509
4 changed files with 48 additions and 4 deletions

View File

@ -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 [ "$TRAVIS_PULL_REQUEST" = "false" ]; then aws s3 sync ~/$TRAVIS_BUILD_NUMBER s3://wasm-bindgen-ci/$TRAVIS_BUILD_NUMBER; fi
if: branch = master 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 # The `web-sys` crate's tests pass
- name: "test web-sys crate" - name: "test web-sys crate"
install: install:

View File

@ -1858,7 +1858,7 @@ impl<'a> Context<'a> {
let class = self.import_identifier(name); let class = self.import_identifier(name);
let op = match &method_data.kind { let op = match &method_data.kind {
decode::MethodKind::Constructor => { decode::MethodKind::Constructor => {
return Ok(ImportTarget::Constructor(class.to_string())) return Ok(ImportTarget::Constructor(class.to_string()));
} }
decode::MethodKind::Operation(op) => op, decode::MethodKind::Operation(op) => op,
}; };
@ -2649,7 +2649,9 @@ impl<'a> Import<'a> {
fn generate_identifier(name: &str, used_names: &mut HashMap<String, usize>) -> String { fn generate_identifier(name: &str, used_names: &mut HashMap<String, usize>) -> String {
let cnt = used_names.entry(name.to_string()).or_insert(0); let cnt = used_names.entry(name.to_string()).or_insert(0);
*cnt += 1; *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() name.to_string()
} else { } else {
format!("{}{}", name, cnt) format!("{}{}", name, cnt)
@ -2668,3 +2670,24 @@ fn format_doc_comments(comments: &[&str], js_doc_comments: Option<String>) -> St
}; };
format!("/**\n{}{}*/\n", body, doc) format!("/**\n{}{}*/\n", body, doc)
} }
#[test]
fn test_generate_identifier() {
let mut used_names: HashMap<String, usize> = 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()
);
}

View File

@ -30,7 +30,7 @@ class Construct {
Construct.internal_string = ''; Construct.internal_string = '';
exports.Construct = Construct; exports.Construct = Construct;
exports.NewConstructors = class { class NewConstructor {
constructor(field) { constructor(field) {
this.field = field; this.field = field;
} }
@ -38,7 +38,10 @@ exports.NewConstructors = class {
get() { get() {
return this.field + 1; return this.field + 1;
} }
}; }
exports.NewConstructors = NewConstructor;
exports.default = NewConstructor;
let switch_called = false; let switch_called = false;
class SwitchMethods { class SwitchMethods {

View File

@ -29,6 +29,13 @@ extern "C" {
#[wasm_bindgen(method)] #[wasm_bindgen(method)]
fn get(this: &NewConstructors) -> i32; 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_a();
fn switch_methods_b(); fn switch_methods_b();
type SwitchMethods; type SwitchMethods;
@ -125,6 +132,12 @@ fn new_constructors() {
assert_eq!(f.get(), 2); assert_eq!(f.get(), 2);
} }
#[wasm_bindgen_test]
fn rename_type() {
let f = RenamedTypes::new(1);
assert_eq!(f.get(), 2);
}
#[wasm_bindgen_test] #[wasm_bindgen_test]
fn switch_methods() { fn switch_methods() {
assert!(!switch_methods_called()); assert!(!switch_methods_called());