Skip to content

Commit

Permalink
Support modifiers in eipw-lint-js
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilsn committed Aug 22, 2023
1 parent 4a15cf9 commit a1c2a74
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
20 changes: 14 additions & 6 deletions eipw-lint-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -130,17 +130,25 @@ pub async fn lint(sources: Vec<JsValue>, options: Option<Object>) -> Result<JsVa
if let Some(options) = options {
opts = serde_wasm_bindgen::from_value(options.deref().clone())?;

let mut options = Options::default();

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

linter = Linter::with_options(reporter, options);
linter = opts.apply(linter);
} else {
linter = Linter::new(reporter);
Expand Down
82 changes: 82 additions & 0 deletions eipw-lint-js/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions eipw-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,10 @@ impl<T> Iterator for NeverIter<T> {
#[non_exhaustive]
pub struct Options<M, L> {
#[serde(default, skip_serializing_if = "Option::is_none")]
modifiers: Option<M>,
pub modifiers: Option<M>,

#[serde(default, skip_serializing_if = "Option::is_none")]
lints: Option<L>,
pub lints: Option<L>,
}

impl<M, L> Default for Options<M, L> {
Expand Down

0 comments on commit a1c2a74

Please sign in to comment.