Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make lints configurable #82

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 80 additions & 3 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ members = [ "eipw-lint", "eipw-lint-js" ]
[package]
name = "eipw"
description = "Ethereum Improvement Proposal linter that's one more than eipv"
version = "0.4.1"
version = "0.5.0"
edition = "2021"
license = "MPL-2.0"
rust-version = "1.65"
Expand All @@ -16,9 +16,11 @@ repository = "https://github.com/ethereum/eipw"
annotate-snippets = "0.9.1"
tokio = { version = "1.29.0", features = [ "macros" ] }
clap = { version = "4.3.9", features = [ "derive" ] }
eipw-lint = { version = "0.4.1", path = "eipw-lint", features = [ "tokio" ] }
eipw-lint = { version = "0.5.0", path = "eipw-lint", features = [ "tokio" ] }
serde_json = "1.0.99"
thiserror = "1.0.40"
toml = "0.7.5"
serde = { version = "1.0.164", features = [ "derive" ] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
tokio = { version = "1.29.0", features = [ "macros", "rt" ] }
Expand Down
4 changes: 2 additions & 2 deletions eipw-lint-js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eipw-lint-js"
version = "0.4.9"
version = "0.5.0"
edition = "2021"
license = "MPL-2.0"
rust-version = "1.65"
Expand All @@ -23,7 +23,7 @@ wasm-bindgen = { version = "0.2.87", features = [ "serde-serialize" ] }
serde-wasm-bindgen = "0.5"
wasm-bindgen-futures = "0.4.37"
console_error_panic_hook = { version = "0.1.7", optional = true }
eipw-lint = { version = "0.4.1", path = "../eipw-lint" }
eipw-lint = { version = "0.5.0", path = "../eipw-lint" }
js-sys = "0.3.64"
serde_json = "1.0.99"
serde = { version = "1.0", features = [ "derive" ] }
Expand Down
35 changes: 28 additions & 7 deletions eipw-lint-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

use eipw_lint::fetch::Fetch;
use eipw_lint::lints::{DefaultLint, Lint};
use eipw_lint::reporters::{AdditionalHelp, Json};
use eipw_lint::{default_lints, Linter};

Expand Down Expand Up @@ -76,25 +77,28 @@ struct Opts {

#[serde(default)]
deny: Vec<String>,

#[serde(default)]
default_lints: Option<HashMap<String, DefaultLint<String>>>,
}

impl Opts {
fn apply<R>(self, mut linter: Linter<R>) -> Linter<R> {
for allow in self.allow {
linter = linter.allow(&allow);
fn apply<'a, 'b: 'a, R>(&'a self, mut linter: Linter<'b, R>) -> Linter<'a, R> {
for allow in &self.allow {
linter = linter.allow(allow);
}

if !self.warn.is_empty() {
let mut lints: HashMap<_, _> = default_lints().collect();
for warn in self.warn {
for warn in &self.warn {
let (k, v) = lints.remove_entry(warn.as_str()).unwrap();
linter = linter.warn(k, v);
}
}

if !self.deny.is_empty() {
let mut lints: HashMap<_, _> = default_lints().collect();
for deny in self.deny {
for deny in &self.deny {
let (k, v) = lints.remove_entry(deny.as_str()).unwrap();
linter = linter.deny(k, v);
}
Expand All @@ -116,13 +120,30 @@ pub async fn lint(sources: Vec<JsValue>, options: Option<Object>) -> Result<JsVa
let reporter = AdditionalHelp::new(reporter, |t: &str| {
Ok(format!("see https://ethereum.github.io/eipw/{}/", t))
});
let mut linter = Linter::new(reporter).set_fetch(NodeFetch);

let opts: Opts;
let mut linter;
if let Some(options) = options {
let opts: Opts = serde_wasm_bindgen::from_value(options.deref().clone())?;
opts = serde_wasm_bindgen::from_value(options.deref().clone())?;

if let Some(ref lints) = opts.default_lints {
linter = Linter::with_lints(
reporter,
lints
.iter()
.map(|(k, v)| (k.as_str(), Box::new(v.clone()) as Box<dyn Lint>)),
);
} else {
linter = Linter::new(reporter);
}

linter = opts.apply(linter);
} else {
linter = Linter::new(reporter);
}

linter = linter.set_fetch(NodeFetch);

for source in &sources {
linter = linter.check_file(source);
}
Expand Down
83 changes: 83 additions & 0 deletions eipw-lint-js/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,89 @@ async fn lint_one_with_options() {
assert_eq!(expected, actual);
}

#[wasm_bindgen_test]
async fn lint_one_with_default_lints() {
let mut path = PathBuf::from("tests");
path.push("eips");
path.push("eip-1000.md");

let path = path.to_str().unwrap();

let opts = json!(
{
"default_lints": {
"banana": {
"kind": "preamble-regex",
"name": "requires",
"mode": "includes",
"pattern": "banana",
"message": "requires must include banana"
}
}
}
);

let opts_js = opts
.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
.unwrap();
let opts = Object::try_from(&opts_js).unwrap().to_owned();

let result = lint(vec![JsValue::from_str(path)], Some(opts))
.await
.ok()
.unwrap();

let actual: serde_json::Value = serde_wasm_bindgen::from_value(result).unwrap();
let expected = json! {
[
{
"formatted": "error[banana]: requires must include banana\n --> tests/eips/eip-1000.md:12:10\n |\n12 | requires: 20\n | ^^^ required pattern was not matched\n |\n = info: the pattern in question: `banana`\n = help: see https://ethereum.github.io/eipw/banana/",
"footer": [
{
"annotation_type": "Info",
"id": null,
"label": "the pattern in question: `banana`"
},
{
"annotation_type": "Help",
"id": null,
"label": "see https://ethereum.github.io/eipw/banana/"
}
],
"opt": {
"anonymized_line_numbers": false,
"color": false
},
"slices": [
{
"annotations": [
{
"annotation_type": "Error",
"label": "required pattern was not matched",
"range": [
9,
12
]
}
],
"fold": false,
"line_start": 12,
"origin": "tests/eips/eip-1000.md",
"source": "requires: 20"
}
],
"title": {
"annotation_type": "Error",
"id": "banana",
"label": "requires must include banana"
}
}
]
};

assert_eq!(expected, actual);
}

#[wasm_bindgen_test]
async fn format_one() {
let mut path = PathBuf::from("tests");
Expand Down
3 changes: 2 additions & 1 deletion eipw-lint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eipw-lint"
version = "0.4.1"
version = "0.5.0"
edition = "2021"
license = "MPL-2.0"
rust-version = "1.65"
Expand Down Expand Up @@ -31,3 +31,4 @@ tokio = { version = "1.29.0", features = [ "fs", "macros", "rt" ] }
[dev-dependencies]
assert_matches = "1.5.0"
tokio = { version = "1.29.0", features = [ "macros", "rt" ] }
toml = "0.7.5"
Loading
Loading