From 1e8970398f245795c719fa260509c6fefeaa9e86 Mon Sep 17 00:00:00 2001 From: Abeeujah Date: Mon, 4 Nov 2024 16:08:11 +0100 Subject: [PATCH] refac: Implemented reviewed changes to maintain consistency and improve error message ergonomics. --- eipw-lint/src/lints/markdown.rs | 4 ++-- .../{headings_only.rs => heading_first.rs} | 23 +++++++++++-------- ...only.rs => lint_markdown_heading_first.rs} | 8 +++---- 3 files changed, 20 insertions(+), 15 deletions(-) rename eipw-lint/src/lints/markdown/{headings_only.rs => heading_first.rs} (56%) rename eipw-lint/tests/{lint_markdown_headings_only.rs => lint_markdown_heading_first.rs} (91%) diff --git a/eipw-lint/src/lints/markdown.rs b/eipw-lint/src/lints/markdown.rs index 4e17e3e4..41d07591 100644 --- a/eipw-lint/src/lints/markdown.rs +++ b/eipw-lint/src/lints/markdown.rs @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -pub mod headings_only; +pub mod heading_first; pub mod headings_space; pub mod html_comments; pub mod json_schema; @@ -17,7 +17,7 @@ pub mod relative_links; pub mod section_order; pub mod section_required; -pub use self::headings_only::HeadingsOnly; +pub use self::heading_first::HeadingFirst; pub use self::headings_space::HeadingsSpace; pub use self::html_comments::HtmlComments; pub use self::json_schema::JsonSchema; diff --git a/eipw-lint/src/lints/markdown/headings_only.rs b/eipw-lint/src/lints/markdown/heading_first.rs similarity index 56% rename from eipw-lint/src/lints/markdown/headings_only.rs rename to eipw-lint/src/lints/markdown/heading_first.rs index b891a28b..8e13d482 100644 --- a/eipw-lint/src/lints/markdown/headings_only.rs +++ b/eipw-lint/src/lints/markdown/heading_first.rs @@ -5,26 +5,31 @@ */ use comrak::nodes::NodeValue; -use eipw_snippets::Level; use crate::lints::{Error, Lint}; #[derive(Debug)] -pub struct HeadingsOnly; +pub struct HeadingFirst; -impl Lint for HeadingsOnly { +impl Lint for HeadingFirst { fn lint<'a>(&self, slug: &'a str, ctx: &crate::lints::Context<'a, '_>) -> Result<(), Error> { - let annotation_type = Level::Error; let second = match ctx.body().descendants().nth(1) { Some(el) => el.data.borrow().to_owned().value, - None => return ctx.report(annotation_type.title("Cannot submit an empty proposal")), + None => { + return ctx.report( + ctx.annotation_level() + .title("Cannot submit an empty proposal") + .id(slug), + ) + } }; match second { NodeValue::Heading(_) => Ok(()), - _ => { - let annotation_type = Level::Error; - ctx.report(annotation_type.title("Only Heading is allowed after FrontMatter")) - } + _ => ctx.report( + ctx.annotation_level() + .title("Nothing is permitted between the preamble and the first heading") + .id(slug), + ), } } } diff --git a/eipw-lint/tests/lint_markdown_headings_only.rs b/eipw-lint/tests/lint_markdown_heading_first.rs similarity index 91% rename from eipw-lint/tests/lint_markdown_headings_only.rs rename to eipw-lint/tests/lint_markdown_heading_first.rs index 02ad0b4e..5a030829 100644 --- a/eipw-lint/tests/lint_markdown_headings_only.rs +++ b/eipw-lint/tests/lint_markdown_heading_first.rs @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use eipw_lint::{lints::markdown::HeadingsOnly, reporters::Text, Linter}; +use eipw_lint::{lints::markdown::HeadingFirst, reporters::Text, Linter}; #[tokio::test] async fn invalid_eip() { @@ -22,7 +22,7 @@ After the "Abstract" heading is the first place we want to allow text."#; let reports = Linter::>::default() .clear_lints() - .deny("markdown-headings-only", HeadingsOnly {}) + .deny("markdown-headings-only", HeadingFirst {}) .check_slice(None, src) .run() .await @@ -31,7 +31,7 @@ After the "Abstract" heading is the first place we want to allow text."#; assert_eq!( reports.trim(), - "error: Only Heading is allowed after FrontMatter" + "error[markdown-headings-only]: Nothing is permitted between the preamble and the first heading" ); } @@ -79,7 +79,7 @@ Changing the denominator from 10 to 9 ensures that the block time remains roughl let reports = Linter::>::default() .clear_lints() - .deny("markdown-headings-only", HeadingsOnly {}) + .deny("markdown-headings-only", HeadingFirst {}) .check_slice(None, src) .run() .await