feat: Use levenshtein function to warn about wasm-pack typos

master
Jesper Håkansson 7 years ago
parent 203cf6996c
commit c39410c81e
  1. 7
      Cargo.lock
  2. 22
      Cargo.toml
  3. 1
      src/lib.rs
  4. 31
      src/manifest/mod.rs
  5. 5
      tests/all/manifest.rs

7
Cargo.lock generated

@ -835,6 +835,11 @@ name = "strsim"
version = "0.7.0" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "structopt" name = "structopt"
version = "0.2.13" version = "0.2.13"
@ -1070,6 +1075,7 @@ dependencies = [
"slog 2.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "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-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)", "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)", "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)", "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)", "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 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 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.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 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 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" "checksum syn 0.15.18 (registry+https://github.com/rust-lang/crates.io-index)" = "90c39a061e2f412a9f869540471ab679e85e50c6b05604daf28bc3060f75c430"

@ -1,13 +1,13 @@
[package] [package]
name = "wasm-pack"
description = "pack up the wasm and publish it to npm!"
version = "0.5.1"
authors = ["Ashley Williams <ashley666ashley@gmail.com>"] authors = ["Ashley Williams <ashley666ashley@gmail.com>"]
repository = "https://github.com/ashleygwilliams/wasm-pack.git"
license = "MIT/Apache-2.0"
readme = "README.md"
categories = ["wasm"] categories = ["wasm"]
description = "pack up the wasm and publish it to npm!"
documentation = "https://rustwasm.github.io/wasm-pack/" 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] [dependencies]
atty = "0.2.11" atty = "0.2.11"
@ -21,7 +21,6 @@ hex = "0.3"
human-panic = "1.0.1" human-panic = "1.0.1"
indicatif = "0.9.0" indicatif = "0.9.0"
lazy_static = "1.1.0" lazy_static = "1.1.0"
openssl = { version = '0.10.11', optional = true }
parking_lot = "0.6" parking_lot = "0.6"
serde = "1.0.74" serde = "1.0.74"
serde_derive = "1.0.74" serde_derive = "1.0.74"
@ -29,16 +28,21 @@ serde_ignored = "0.0.4"
serde_json = "1.0.26" serde_json = "1.0.26"
siphasher = "0.2.3" siphasher = "0.2.3"
slog = "2.3" slog = "2.3"
slog-term = "2.4"
slog-async = "2.3" slog-async = "2.3"
slog-term = "2.4"
strsim = "0.8.0"
structopt = "0.2" structopt = "0.2"
tar = "0.4.16" tar = "0.4.16"
toml = "0.4" toml = "0.4"
which = "2.0.0" which = "2.0.0"
zip = "0.4.2" zip = "0.4.2"
[dependencies.openssl]
optional = true
version = "0.10.11"
[dev-dependencies] [dev-dependencies]
tempfile = "3" tempfile = "3"
[features] [features]
vendored-openssl = ['openssl/vendored'] vendored-openssl = ["openssl/vendored"]

@ -6,6 +6,7 @@ extern crate cargo_metadata;
extern crate console; extern crate console;
extern crate curl; extern crate curl;
extern crate dirs; extern crate dirs;
extern crate strsim;
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
extern crate flate2; extern crate flate2;

@ -16,9 +16,12 @@ use progressbar::Step;
use serde::{self, Deserialize}; use serde::{self, Deserialize};
use serde_json; use serde_json;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use strsim::levenshtein;
use toml; use toml;
use PBAR; use PBAR;
const WASM_PACK_METADATA_KEY: &'static str = "package.metadata.wasm-pack";
/// Store for metadata learned about a crate /// Store for metadata learned about a crate
pub struct CrateData { pub struct CrateData {
data: Metadata, data: Metadata,
@ -202,6 +205,7 @@ struct NpmData {
pub struct ManifestAndUnsedKeys { pub struct ManifestAndUnsedKeys {
pub manifest: CargoManifest, pub manifest: CargoManifest,
pub unused_keys: BTreeSet<String>, pub unused_keys: BTreeSet<String>,
pub has_possible_typo: bool,
} }
impl CrateData { impl CrateData {
@ -220,12 +224,10 @@ impl CrateData {
let data = let data =
cargo_metadata::metadata(Some(&manifest_path)).map_err(error_chain_to_failure)?; cargo_metadata::metadata(Some(&manifest_path)).map_err(error_chain_to_failure)?;
let ManifestAndUnsedKeys { let manifest_and_keys = CrateData::parse_crate_data(&manifest_path)?;
manifest, CrateData::warn_for_unused_keys(&manifest_and_keys);
unused_keys,
} = CrateData::parse_crate_data(&manifest_path)?;
CrateData::warn_for_unused_keys(&unused_keys);
let manifest = manifest_and_keys.manifest;
let current_idx = data let current_idx = data
.packages .packages
.iter() .iter()
@ -264,11 +266,17 @@ impl CrateData {
let manifest = &mut toml::Deserializer::new(&manifest); let manifest = &mut toml::Deserializer::new(&manifest);
let mut unused_keys = BTreeSet::new(); 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 manifest: CargoManifest = serde_ignored::deserialize(manifest, |path| {
let path_string = path.to_string(); 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); unused_keys.insert(path_string);
} }
}) })
@ -277,13 +285,20 @@ impl CrateData {
Ok(ManifestAndUnsedKeys { Ok(ManifestAndUnsedKeys {
manifest, manifest,
unused_keys, unused_keys,
has_possible_typo,
}) })
} }
/// Iterating through all the passed `unused_keys` and output /// Iterating through all the passed `unused_keys` and output
/// a warning for each unknown key. /// a warning for each unknown key.
pub fn warn_for_unused_keys(unused_keys: &BTreeSet<String>) { pub fn warn_for_unused_keys(manifest_and_keys: &ManifestAndUnsedKeys) {
unused_keys.iter().for_each(|path| { 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!( PBAR.warn(&format!(
"\"{}\" is a unknown key and will be ignored. Please check your Cargo.toml.", "\"{}\" is a unknown key and will be ignored. Please check your Cargo.toml.",
path path

@ -353,7 +353,8 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() {
[dependencies] [dependencies]
wasm-bindgen = "0.2" 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 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(); let manifest::ManifestAndUnsedKeys { unused_keys, .. } = result.unwrap();
assert!(unused_keys.contains("package.metadata.wasmpack")); assert!(unused_keys.contains("package.metadata.wasm-pack.profile.production"));
} }

Loading…
Cancel
Save