Skip to content

Commit

Permalink
Polish up no_backticks lint
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilsn committed Aug 23, 2024
1 parent d88e4df commit adc4ec5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 81 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ error[preamble-order]: preamble header `description` must come after `title`
| `markdown-json-cite` | All `csl-json` code blocks adhere to the correct schema. |
| `markdown-link-first` | First mention of an EIP must be a link. |
| `markdown-link-status` | EIPs linked in the body have statuses further along than the current proposal. |
| `markdown-no-backticks` | No proposals are referenced inside backticks (eg. \`EIP-1234\`). |
| `markdown-order-section` | There are no extra sections and the sections are in the correct order. |
| `markdown-re-eip-dash` | Other EIPs are referenced using EIP-X, not EIPX or EIP X. |
| `markdown-re-erc-dash` | Other ERCs are referenced using ERC-X, not ERCX or ERC X. |
| `markdown-refs` | ERCs are referenced using ERC-X, while other proposals use EIP-X. |
| `markdown-rel-links` | All URLs in the page are relative. |
| `markdown-req-section` | Required sections are present in the body of the proposal. |
| `markdown-headings-space` | Headers have a space after the leading '#' characters |
| `markdown-headings-space` | Headers have a space after the leading '#' characters |
| `preamble-author` | The author header is correctly formatted, and there is at least one GitHub user listed. |
| `preamble-date-created` | The `created` header is a date. |
| `preamble-date-last-call-deadline` | The `last-call-deadline` header is a date. |
Expand Down
2 changes: 1 addition & 1 deletion eipw-lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ pub fn default_lints_enum() -> impl Iterator<Item = (&'static str, DefaultLint<&
(
"markdown-no-backticks",
MarkdownNoBackticks {
pattern: markdown::NoBackticks(r"EIP-[0-9]+"),
pattern: markdown::NoBackticks(r"(?i)(eip|erc)-[0-9]+"),
}
),
("markdown-rel-links", MarkdownRelativeLinks(markdown::RelativeLinks {
Expand Down
102 changes: 30 additions & 72 deletions eipw-lint/src/lints/markdown/no_backticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet};

use comrak::nodes::{Ast, NodeCode, NodeCodeBlock, NodeHtmlBlock, NodeLink};
use comrak::nodes::{Ast, NodeCode};

use crate::lints::{Context, Error, Lint};
use crate::tree::{self, Next, TraverseExt};
Expand Down Expand Up @@ -34,7 +34,6 @@ where
re,
pattern,
slug,
found_backticks: false,
};
ctx.body().traverse().visit(&mut visitor)?;
Ok(())
Expand All @@ -46,86 +45,45 @@ struct Visitor<'a, 'b, 'c> {
re: Regex,
pattern: &'c str,
slug: &'c str,
found_backticks: bool,
}

impl<'a, 'b, 'c> Visitor<'a, 'b, 'c> {
fn check(&mut self, ast: &Ast, text: &str) -> Result<Next, Error> {
// Remove the condition that checks for backticks at the start and end
if self.re.is_match(text) {
self.found_backticks = true;
let footer_label = format!("the pattern in question: `{}`", self.pattern);
let source = self.ctx.source_for_text(ast.sourcepos.start.line, text);
self.ctx.report(Snippet {
opt: Default::default(),
title: Some(Annotation {
annotation_type: self.ctx.annotation_type(),
id: Some(self.slug),
label: Some("EIP references should not be in backticks"),
}),
slices: vec![Slice {
fold: false,
line_start: ast.sourcepos.start.line,
origin: self.ctx.origin(),
source: &source,
annotations: vec![],
}],
footer: vec![Annotation {
annotation_type: AnnotationType::Help,
id: None,
label: Some(&footer_label),
}],
})?;
return Ok(Next::SkipChildren);
if !self.re.is_match(text) {
return Ok(Next::TraverseChildren);
}
Ok(Next::TraverseChildren)
}
}

impl<'a, 'b, 'c> tree::Visitor for Visitor<'a, 'b, 'c> {
type Error = Error;

fn enter_front_matter(&mut self, _: &Ast, _: &str) -> Result<Next, Self::Error> {
Ok(Next::SkipChildren)
}
let footer_label = format!("the pattern in question: `{}`", self.pattern);
let source = self.ctx.source_for_text(ast.sourcepos.start.line, text);
self.ctx.report(Snippet {
opt: Default::default(),
title: Some(Annotation {
annotation_type: self.ctx.annotation_type(),
id: Some(self.slug),
label: Some("EIP references should not be in backticks"),
}),
slices: vec![Slice {
fold: false,
line_start: ast.sourcepos.start.line,
origin: self.ctx.origin(),
source: &source,
annotations: vec![],
}],
footer: vec![Annotation {
annotation_type: AnnotationType::Info,
id: None,
label: Some(&footer_label),
}],
})?;

fn enter_code(&mut self, _ast: &Ast, _code: &NodeCode) -> Result<Next, Self::Error> {
Ok(Next::SkipChildren)
}
}

fn enter_code_block(&mut self, _: &Ast, _: &NodeCodeBlock) -> Result<Next, Self::Error> {
Ok(Next::SkipChildren)
}

fn enter_html_inline(&mut self, _: &Ast, _: &str) -> Result<Next, Self::Error> {
Ok(Next::SkipChildren)
}

fn enter_html_block(&mut self, _: &Ast, _: &NodeHtmlBlock) -> Result<Next, Self::Error> {
Ok(Next::SkipChildren)
}

fn enter_footnote_definition(&mut self, ast: &Ast, defn: &str) -> Result<Next, Self::Error> {
self.check(ast, defn)
}

fn enter_text(&mut self, ast: &Ast, txt: &str) -> Result<Next, Self::Error> {
self.check(ast, txt)
}

fn enter_link(&mut self, _: &Ast, _: &NodeLink) -> Result<Next, Self::Error> {
Ok(Next::TraverseChildren)
}

fn depart_link(&mut self, _: &Ast, _: &NodeLink) -> Result<(), Self::Error> {
Ok(())
}

fn enter_image(&mut self, ast: &Ast, link: &NodeLink) -> Result<Next, Self::Error> {
self.check(ast, &link.title)
}
impl<'a, 'b, 'c> tree::Visitor for Visitor<'a, 'b, 'c> {
type Error = Error;

fn enter_footnote_reference(&mut self, ast: &Ast, refn: &str) -> Result<Next, Self::Error> {
self.check(ast, refn)
fn enter_code(&mut self, ast: &Ast, code: &NodeCode) -> Result<Next, Self::Error> {
self.check(ast, &code.literal)
}
}
6 changes: 6 additions & 0 deletions eipw-lint/tests/eipv/markdown-malformed-eip/expected.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
error[markdown-no-backticks]: EIP references should not be in backticks
--> input.md
|
18 | This is the motivation for the EIP, which extends `ERC-721`.
|
= info: the pattern in question: `(?i)(eip|erc)-[0-9]+`
error[markdown-re-eip-dash]: proposals must be referenced with the form `EIP-N` (not `EIPN` or `EIP N`)
--> input.md
|
Expand Down
2 changes: 1 addition & 1 deletion eipw-lint/tests/eipv/markdown-proposal-ref/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ These are the backwards compatibility concerns for the `EIP1234`.
These are the test cases for the EIP.

## Reference Implementation
This is the implementation for the EIP, but `EIP-721` should be allowed.
This is the implementation for the EIP, but `EIP721` should be allowed.

## Security Considerations
These are the security considerations for the EIP.
Expand Down
10 changes: 4 additions & 6 deletions eipw-lint/tests/lint_markdown_no_backticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ hello
assert_eq!(
reports,
r#"error[markdown-no-backticks]: EIP references should not be in backticks
--> 7:1
|
7 | `EIP-1234`
| ^^^^^^^^^^
|
= info: the pattern in question: `EIP-[0-9]+`
|
7 | `EIP-1234`
|
= info: the pattern in question: `EIP-[0-9]+`
"#
);
}
Expand Down

0 comments on commit adc4ec5

Please sign in to comment.