From c39410c81e8f69c2449e0a1465544ff744664f48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesper=20H=C3=A5kansson?= Date: Tue, 13 Nov 2018 22:42:59 +0100 Subject: [PATCH] feat: Use levenshtein function to warn about wasm-pack typos --- Cargo.lock | 7 +++++++ Cargo.toml | 22 +++++++++++++--------- src/lib.rs | 1 + src/manifest/mod.rs | 31 +++++++++++++++++++++++-------- tests/all/manifest.rs | 5 +++-- 5 files changed, 47 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d07284f1..d2eaa239 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -835,6 +835,11 @@ name = "strsim" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "structopt" version = "0.2.13" @@ -1070,6 +1075,7 @@ dependencies = [ "slog 2.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "slog-async 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "slog-term 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.4.18 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1245,6 +1251,7 @@ dependencies = [ "checksum socket2 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c4d11a52082057d87cb5caa31ad812f4504b97ab44732cd8359df2e9ff9f48e7" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" +"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum structopt 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "41c4a2479a078509940d82773d90ff824a8c89533ab3b59cd3ce8b0c0e369c02" "checksum structopt-derive 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "5352090cfae7a2c85e1a31146268b53396106c88ca5d6ccee2e3fae83b6e35c2" "checksum syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)" = "90c39a061e2f412a9f869540471ab679e85e50c6b05604daf28bc3060f75c430" diff --git a/Cargo.toml b/Cargo.toml index 0c7a45bb..b39ba120 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,13 @@ [package] -name = "wasm-pack" -description = "pack up the wasm and publish it to npm!" -version = "0.5.1" authors = ["Ashley Williams "] -repository = "https://github.com/ashleygwilliams/wasm-pack.git" -license = "MIT/Apache-2.0" -readme = "README.md" categories = ["wasm"] +description = "pack up the wasm and publish it to npm!" documentation = "https://rustwasm.github.io/wasm-pack/" +license = "MIT/Apache-2.0" +name = "wasm-pack" +readme = "README.md" +repository = "https://github.com/ashleygwilliams/wasm-pack.git" +version = "0.5.1" [dependencies] atty = "0.2.11" @@ -21,7 +21,6 @@ hex = "0.3" human-panic = "1.0.1" indicatif = "0.9.0" lazy_static = "1.1.0" -openssl = { version = '0.10.11', optional = true } parking_lot = "0.6" serde = "1.0.74" serde_derive = "1.0.74" @@ -29,16 +28,21 @@ serde_ignored = "0.0.4" serde_json = "1.0.26" siphasher = "0.2.3" slog = "2.3" -slog-term = "2.4" slog-async = "2.3" +slog-term = "2.4" +strsim = "0.8.0" structopt = "0.2" tar = "0.4.16" toml = "0.4" which = "2.0.0" zip = "0.4.2" +[dependencies.openssl] +optional = true +version = "0.10.11" + [dev-dependencies] tempfile = "3" [features] -vendored-openssl = ['openssl/vendored'] +vendored-openssl = ["openssl/vendored"] diff --git a/src/lib.rs b/src/lib.rs index 0000c4ad..7ced3bb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ extern crate cargo_metadata; extern crate console; extern crate curl; extern crate dirs; +extern crate strsim; #[macro_use] extern crate failure; extern crate flate2; diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 9211303e..6efe1a1c 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -16,9 +16,12 @@ use progressbar::Step; use serde::{self, Deserialize}; use serde_json; use std::collections::BTreeSet; +use strsim::levenshtein; use toml; use PBAR; +const WASM_PACK_METADATA_KEY: &'static str = "package.metadata.wasm-pack"; + /// Store for metadata learned about a crate pub struct CrateData { data: Metadata, @@ -202,6 +205,7 @@ struct NpmData { pub struct ManifestAndUnsedKeys { pub manifest: CargoManifest, pub unused_keys: BTreeSet, + pub has_possible_typo: bool, } impl CrateData { @@ -220,12 +224,10 @@ impl CrateData { let data = cargo_metadata::metadata(Some(&manifest_path)).map_err(error_chain_to_failure)?; - let ManifestAndUnsedKeys { - manifest, - unused_keys, - } = CrateData::parse_crate_data(&manifest_path)?; - CrateData::warn_for_unused_keys(&unused_keys); + let manifest_and_keys = CrateData::parse_crate_data(&manifest_path)?; + CrateData::warn_for_unused_keys(&manifest_and_keys); + let manifest = manifest_and_keys.manifest; let current_idx = data .packages .iter() @@ -264,11 +266,17 @@ impl CrateData { let manifest = &mut toml::Deserializer::new(&manifest); let mut unused_keys = BTreeSet::new(); + let mut has_possible_typo = false; + let levenshtein_threshold = 1; let manifest: CargoManifest = serde_ignored::deserialize(manifest, |path| { let path_string = path.to_string(); - if path_string.contains("metadata") { + if levenshtein(WASM_PACK_METADATA_KEY, &path_string) == levenshtein_threshold { + has_possible_typo = true; + } + + if path_string.contains(WASM_PACK_METADATA_KEY) { unused_keys.insert(path_string); } }) @@ -277,13 +285,20 @@ impl CrateData { Ok(ManifestAndUnsedKeys { manifest, unused_keys, + has_possible_typo, }) } /// Iterating through all the passed `unused_keys` and output /// a warning for each unknown key. - pub fn warn_for_unused_keys(unused_keys: &BTreeSet) { - unused_keys.iter().for_each(|path| { + pub fn warn_for_unused_keys(manifest_and_keys: &ManifestAndUnsedKeys) { + if manifest_and_keys.has_possible_typo { + PBAR.warn(&format!( + "It's possible that you misspelled the \"{}\" setting in your Cargo.toml.", + WASM_PACK_METADATA_KEY + )); + } + manifest_and_keys.unused_keys.iter().for_each(|path| { PBAR.warn(&format!( "\"{}\" is a unknown key and will be ignored. Please check your Cargo.toml.", path diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index dce984fb..1d631c84 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -353,7 +353,8 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { [dependencies] wasm-bindgen = "0.2" - [package.metadata.wasmpack.profile.dev.wasm-bindgen] + # Note: production is not valid. + [package.metadata.wasm-pack.profile.production.wasm-bindgen] debug-js-glue = true "#, ) @@ -365,5 +366,5 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { let manifest::ManifestAndUnsedKeys { unused_keys, .. } = result.unwrap(); - assert!(unused_keys.contains("package.metadata.wasmpack")); + assert!(unused_keys.contains("package.metadata.wasm-pack.profile.production")); }