mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 19:11:45 +03:00
Add support for importing default exports
This commit is contained in:
parent
7768328fc4
commit
371e864509
@ -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:
|
||||
|
@ -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, 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)
|
||||
@ -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()
|
||||
);
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user