Skip to content

Commit

Permalink
Merge pull request #267 from fitzgen/hyphens-and-underscores
Browse files Browse the repository at this point in the history
fix: Handle both underscores and hypthens as separators in "wasm-bind…
  • Loading branch information
fitzgen authored Aug 27, 2018
2 parents f2668f8 + b480674 commit d5bf5a3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ enum CargoDependency {
}

#[derive(Deserialize)]
struct DetailedCargoDependency {}
struct DetailedCargoDependency {
version: Option<String>,
}

#[derive(Deserialize)]
struct CargoLib {
Expand Down Expand Up @@ -194,17 +196,8 @@ pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), Error> {
}

fn check_wasm_bindgen(path: &Path) -> Result<(), Error> {
let cargo_toml = read_cargo_toml(path)?;
if cargo_toml
.dependencies
.map_or(false, |deps| deps.contains_key("wasm-bindgen"))
{
return Ok(());
}
Error::crate_config(&format!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n[dependencies]\nwasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim()
))
get_wasm_bindgen_version(path)?;
Ok(())
}

fn check_crate_type(path: &Path) -> Result<(), Error> {
Expand All @@ -222,9 +215,15 @@ fn check_crate_type(path: &Path) -> Result<(), Error> {
/// Get the version of `wasm-bindgen` specified as a dependency.
pub fn get_wasm_bindgen_version(path: &Path) -> Result<String, Error> {
if let Some(deps) = read_cargo_toml(path)?.dependencies {
match deps.get("wasm_bindgen") {
Some(CargoDependency::Simple(version)) => Ok(version.clone()),
Some(CargoDependency::Detailed(_)) => {
match deps
.get("wasm-bindgen")
.or_else(|| deps.get("wasm_bindgen"))
{
Some(CargoDependency::Simple(version))
| Some(CargoDependency::Detailed(DetailedCargoDependency {
version: Some(version),
})) => Ok(version.clone()),
Some(CargoDependency::Detailed(DetailedCargoDependency { version: None })) => {
let msg = format!(
"\"{}\" dependency is missing its version number",
style("wasm-bindgen").bold().dim()
Expand Down
18 changes: 18 additions & 0 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,21 @@ fn it_does_not_error_when_wasm_bindgen_is_declared() {
let step = wasm_pack::progressbar::Step::new(1);
assert!(manifest::check_crate_config(&fixture.path, &step).is_ok());
}

#[test]
fn it_gets_wasm_bindgen_version() {
let fixture = fixture::fixture("tests/fixtures/js-hello-world");
assert_eq!(
manifest::get_wasm_bindgen_version(&fixture.path).unwrap(),
"0.2"
);
}

#[test]
fn it_gets_wasm_bindgen_version_with_underscores() {
let fixture = fixture::fixture("tests/fixtures/with-underscores");
assert_eq!(
manifest::get_wasm_bindgen_version(&fixture.path).unwrap(),
"0.2"
);
}
14 changes: 14 additions & 0 deletions tests/fixtures/with-underscores/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "with-underscores"
version = "0.1.0"
authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
license = "WTFPL"
repository = "https://github.com/ashleygwilliams/wasm-pack"

[lib]
crate-type = ["cdylib"]

[dependencies]
# Cargo will normalize "wasm-bindgen" and "wasm_bindgen" and that shouldn't
# break wasm-pack.
wasm_bindgen = "0.2"
2 changes: 2 additions & 0 deletions tests/fixtures/with-underscores/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# js-hello-world
> an example rust -> wasm project
18 changes: 18 additions & 0 deletions tests/fixtures/with-underscores/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(use_extern_macros)]

extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}

// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}

0 comments on commit d5bf5a3

Please sign in to comment.