From b480674df4bbfe1178a98cc3880cdfab0d226adc Mon Sep 17 00:00:00 2001
From: Nick Fitzgerald <fitzgen@gmail.com>
Date: Fri, 24 Aug 2018 15:59:09 -0700
Subject: [PATCH] fix: Handle both underscores and hypthens as separators in
 "wasm-bindgen" dependencies

---
 Cargo.lock                                 | 12 ++++-----
 src/manifest.rs                            | 29 +++++++++++-----------
 tests/all/manifest.rs                      | 18 ++++++++++++++
 tests/fixtures/with-underscores/Cargo.toml | 14 +++++++++++
 tests/fixtures/with-underscores/README.md  |  2 ++
 tests/fixtures/with-underscores/src/lib.rs | 18 ++++++++++++++
 6 files changed, 72 insertions(+), 21 deletions(-)
 create mode 100644 tests/fixtures/with-underscores/Cargo.toml
 create mode 100644 tests/fixtures/with-underscores/README.md
 create mode 100644 tests/fixtures/with-underscores/src/lib.rs

diff --git a/Cargo.lock b/Cargo.lock
index acf271a..d919a76 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,6 +1,6 @@
 [[package]]
 name = "aho-corasick"
-version = "0.6.7"
+version = "0.6.8"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -67,7 +67,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "chrono"
-version = "0.4.5"
+version = "0.4.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -361,7 +361,7 @@ name = "regex"
 version = "0.2.11"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "aho-corasick 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -444,7 +444,7 @@ name = "slog-term"
 version = "2.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "chrono 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "isatty 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
  "slog 2.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "term 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -735,7 +735,7 @@ dependencies = [
 ]
 
 [metadata]
-"checksum aho-corasick 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "7ee94e9463ccb9d681e7b708082687b2c56d2bd420ca8a3d3157d27d59508ec0"
+"checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a"
 "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
 "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652"
 "checksum backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "89a47830402e9981c5c41223151efcced65a0510c13097c769cede7efb34782a"
@@ -744,7 +744,7 @@ dependencies = [
 "checksum byteorder 1.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "90492c5858dd7d2e78691cfb89f90d273a2800fc11d98f60786e5d87e2f83781"
 "checksum cc 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)" = "4a6007c146fdd28d4512a794b07ffe9d8e89e6bf86e2e0c4ddff2e1fb54a0007"
 "checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3"
-"checksum chrono 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e48d85528df61dc964aa43c5f6ca681a19cfa74939b2348d204bd08a981f2fb0"
+"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
 "checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
 "checksum clicolors-control 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f84dec9bc083ce2503908cd305af98bd363da6f54bf8d4bf0ac14ee749ad5d1"
 "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
diff --git a/src/manifest.rs b/src/manifest.rs
index 8d071e7..70c92fa 100644
--- a/src/manifest.rs
+++ b/src/manifest.rs
@@ -38,7 +38,9 @@ enum CargoDependency {
 }
 
 #[derive(Deserialize)]
-struct DetailedCargoDependency {}
+struct DetailedCargoDependency {
+    version: Option<String>,
+}
 
 #[derive(Deserialize)]
 struct CargoLib {
@@ -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> {
@@ -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()
diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs
index 714d0a4..a709b42 100644
--- a/tests/all/manifest.rs
+++ b/tests/all/manifest.rs
@@ -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"
+    );
+}
diff --git a/tests/fixtures/with-underscores/Cargo.toml b/tests/fixtures/with-underscores/Cargo.toml
new file mode 100644
index 0000000..f4293c1
--- /dev/null
+++ b/tests/fixtures/with-underscores/Cargo.toml
@@ -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"
diff --git a/tests/fixtures/with-underscores/README.md b/tests/fixtures/with-underscores/README.md
new file mode 100644
index 0000000..8fbd2b3
--- /dev/null
+++ b/tests/fixtures/with-underscores/README.md
@@ -0,0 +1,2 @@
+# js-hello-world
+> an example rust -> wasm project
diff --git a/tests/fixtures/with-underscores/src/lib.rs b/tests/fixtures/with-underscores/src/lib.rs
new file mode 100644
index 0000000..1785f72
--- /dev/null
+++ b/tests/fixtures/with-underscores/src/lib.rs
@@ -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));
+}