Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
feat: Use levenshtein function to warn about wasm-pack typos
Browse files Browse the repository at this point in the history
  • Loading branch information
drager committed Nov 13, 2018
1 parent 203cf69 commit c39410c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

22 changes: 13 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
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"
Expand All @@ -21,24 +21,28 @@ 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"
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"]
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 23 additions & 8 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -202,6 +205,7 @@ struct NpmData {
pub struct ManifestAndUnsedKeys {
pub manifest: CargoManifest,
pub unused_keys: BTreeSet<String>,
pub has_possible_typo: bool,
}

impl CrateData {
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
})
Expand All @@ -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<String>) {
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
Expand Down
5 changes: 3 additions & 2 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"#,
)
Expand All @@ -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"));
}

0 comments on commit c39410c

Please sign in to comment.