Skip to content

Commit 9e45637

Browse files
committed
tmp
1 parent aae3c87 commit 9e45637

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

crates/backend/src/ast.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,26 @@ impl Variant {
587587

588588
impl Import {
589589
fn shared(&self) -> shared::Import {
590+
match (&self.module, &self.version) {
591+
(&Some(ref m), None) if m.starts_with("./") => {}
592+
(&Some(ref m), &Some(_)) if m.starts_with("./") => {
593+
panic!("when a module path starts with `./` that indicates \
594+
that a local file is being imported so the `version` \
595+
key cannot also be specified");
596+
}
597+
(&Some(_), &Some(_)) => {}
598+
(&Some(_), &None) => {
599+
panic!("when the `module` directive doesn't start with `./` \
600+
then it's interpreted as an NPM package which requires \
601+
a `version` to be specified as well, try using \
602+
#[wasm_bindgen(module = \"...\", version = \"...\")]")
603+
}
604+
(&None, &Some(_)) => {
605+
panic!("the #[wasm_bindgen(version = \"...\")] attribute can only \
606+
be used when `module = \"...\"` is also specified");
607+
}
608+
(&None, &None) => {}
609+
}
590610
shared::Import {
591611
module: self.module.clone(),
592612
version: self.version.clone(),

crates/cli-support/src/js/mod.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,12 +1429,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
14291429
}
14301430

14311431
fn generate_import(&mut self, import: &shared::Import) -> Result<(), Error> {
1432-
match (&import.module, &import.version) {
1433-
(&Some(ref m), &Some(ref v)) => {
1434-
self.cx.module_versions.push((m.clone(), v.clone()));
1435-
}
1436-
_ => {}
1437-
}
1432+
self.validate_import_module(import)?;
14381433
match import.kind {
14391434
shared::ImportKind::Function(ref f) => {
14401435
self.generate_import_function(import, f)
@@ -1455,6 +1450,40 @@ impl<'a, 'b> SubContext<'a, 'b> {
14551450
Ok(())
14561451
}
14571452

1453+
fn validate_import_module(&mut self, import: &shared::Import)
1454+
-> Result<(), Error>
1455+
{
1456+
let version = match import.version {
1457+
Some(ref s) => s,
1458+
None => return Ok(()),
1459+
};
1460+
let module = match import.module {
1461+
Some(ref s) => s,
1462+
None => return Ok(()),
1463+
};
1464+
if module.starts_with("./") {
1465+
return Ok(())
1466+
}
1467+
let pkg = if module.starts_with("@") {
1468+
// Translate `@foo/bar/baz` to `@foo/bar` and `@foo/bar` to itself
1469+
let first_slash = match module.find('/') {
1470+
Some(i) => i,
1471+
None => {
1472+
bail!("packages starting with `@` must be of the form \
1473+
`@foo/bar`, but found: `{}`", module)
1474+
}
1475+
};
1476+
match module[first_slash + 1..].find('/') {
1477+
Some(i) => &module[..i],
1478+
None => module,
1479+
}
1480+
} else {
1481+
module.split('/').next().unwrap()
1482+
};
1483+
self.cx.module_versions.push((pkg.to_string(), version.clone()));
1484+
Ok(())
1485+
}
1486+
14581487
fn generate_import_static(
14591488
&mut self,
14601489
info: &shared::Import,

0 commit comments

Comments
 (0)