From a1c2a744a96578643ad6501768b15a0bc6a33c35 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Tue, 22 Aug 2023 12:46:29 -0400 Subject: [PATCH] Support modifiers in eipw-lint-js --- eipw-lint-js/src/lib.rs | 20 +++++++--- eipw-lint-js/tests/main.rs | 82 ++++++++++++++++++++++++++++++++++++++ eipw-lint/src/lib.rs | 4 +- 3 files changed, 98 insertions(+), 8 deletions(-) diff --git a/eipw-lint-js/src/lib.rs b/eipw-lint-js/src/lib.rs index d8cd71ca..d1ed248d 100644 --- a/eipw-lint-js/src/lib.rs +++ b/eipw-lint-js/src/lib.rs @@ -6,9 +6,9 @@ use eipw_lint::fetch::Fetch; use eipw_lint::lints::{DefaultLint, Lint}; -use eipw_lint::modifiers::DefaultModifier; +use eipw_lint::modifiers::{DefaultModifier, Modifier}; use eipw_lint::reporters::{AdditionalHelp, Json}; -use eipw_lint::{default_lints, Linter}; +use eipw_lint::{default_lints, Linter, Options}; use js_sys::{JsString, Object}; @@ -130,17 +130,25 @@ pub async fn lint(sources: Vec, options: Option) -> Result)), ); - } else { - linter = Linter::new(reporter); } + if let Some(ref modifiers) = opts.default_modifiers { + options.modifiers = Some( + modifiers + .iter() + .map(|m| Box::new(m.clone()) as Box), + ); + } + + linter = Linter::with_options(reporter, options); linter = opts.apply(linter); } else { linter = Linter::new(reporter); diff --git a/eipw-lint-js/tests/main.rs b/eipw-lint-js/tests/main.rs index b8e68e2d..d9346d5a 100644 --- a/eipw-lint-js/tests/main.rs +++ b/eipw-lint-js/tests/main.rs @@ -314,6 +314,88 @@ async fn lint_one_with_default_lints() { assert_eq!(expected, actual); } +#[wasm_bindgen_test] +async fn lint_one_with_default_modifiers() { + let mut path = PathBuf::from("tests"); + path.push("eips"); + path.push("eip-1000.md"); + + let path = path.to_str().unwrap(); + + let opts = json!( + { + "default_modifiers": [ + { + "kind": "set-default-annotation", + "name": "status", + "value": "Last Call", + "annotation_type": "info", + } + ] + } + ); + + 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": "info[preamble-requires-status]: preamble header `requires` contains items not stable enough for a `status` of `Last Call`\n --> tests/eips/eip-1000.md:12:10\n |\n12 | requires: 20\n | --- info: has a less advanced status\n |\n = help: valid `status` values for this proposal are: `Draft`, `Stagnant`\n = help: see https://ethereum.github.io/eipw/preamble-requires-status/", + "footer": [ + { + "annotation_type": "Help", + "id": null, + "label": "valid `status` values for this proposal are: `Draft`, `Stagnant`" + }, + { + "annotation_type": "Help", + "id": null, + "label": "see https://ethereum.github.io/eipw/preamble-requires-status/" + } + ], + "opt": { + "anonymized_line_numbers": false, + "color": false + }, + "slices": [ + { + "annotations": [ + { + "annotation_type": "Info", + "label": "has a less advanced status", + "range": [ + 9, + 12 + ] + } + ], + "fold": false, + "line_start": 12, + "origin": "tests/eips/eip-1000.md", + "source": "requires: 20" + } + ], + "title": { + "annotation_type": "Info", + "id": "preamble-requires-status", + "label": "preamble header `requires` contains items not stable enough for a `status` of `Last Call`" + } + } + ] + }; + + assert_eq!(expected, actual); +} + #[wasm_bindgen_test] async fn format_one() { let mut path = PathBuf::from("tests"); diff --git a/eipw-lint/src/lib.rs b/eipw-lint/src/lib.rs index 9748eed0..40d981de 100644 --- a/eipw-lint/src/lib.rs +++ b/eipw-lint/src/lib.rs @@ -517,10 +517,10 @@ impl Iterator for NeverIter { #[non_exhaustive] pub struct Options { #[serde(default, skip_serializing_if = "Option::is_none")] - modifiers: Option, + pub modifiers: Option, #[serde(default, skip_serializing_if = "Option::is_none")] - lints: Option, + pub lints: Option, } impl Default for Options {