Skip to content

Commit

Permalink
Merge pull request #1106 from mvlabat/default-support
Browse files Browse the repository at this point in the history
Add support for importing default exports
  • Loading branch information
alexcrichton authored Dec 11, 2018
2 parents 01fa5dd + 371e864 commit c6b74ff
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 25 additions & 2 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -2649,7 +2649,9 @@ impl<'a> Import<'a> {
fn generate_identifier(name: &str, used_names: &mut HashMap<String, usize>) -> 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)
Expand All @@ -2668,3 +2670,24 @@ fn format_doc_comments(comments: &[&str], js_doc_comments: Option<String>) -> St
};
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()
);
}
7 changes: 5 additions & 2 deletions tests/wasm/import_class.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ class Construct {
Construct.internal_string = '';
exports.Construct = Construct;

exports.NewConstructors = class {
class NewConstructor {
constructor(field) {
this.field = field;
}

get() {
return this.field + 1;
}
};
}

exports.NewConstructors = NewConstructor;
exports.default = NewConstructor;

let switch_called = false;
class SwitchMethods {
Expand Down
13 changes: 13 additions & 0 deletions tests/wasm/import_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit c6b74ff

Please sign in to comment.