-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: Add lint preventing EIP references in backticks (#93)
* FEAT: Add lint preventing EIP references in backticks * WIP:code updates * Delete .vscode/launch.json * update code for the lint-backticks and tests * updates * WIP: test passed valid_code_in_backticks * cargo fmt * Remove printlns * Polish up no_backticks lint --------- Co-authored-by: Sam Wilson <[email protected]> Co-authored-by: Vickish <[email protected]> Co-authored-by: Sam Wilson <[email protected]>
- Loading branch information
1 parent
97e667a
commit 411b101
Showing
10 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>markdown-no-backticks</title> | ||
<link rel="stylesheet" href="../main.css" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
</head> | ||
<body> | ||
<article> | ||
<h1><code>markdown-no-backticks</code></h1> | ||
<p>EIP references should not be in backticks.</p> | ||
|
||
<section> | ||
<h2>Examples</h2> | ||
|
||
<pre> | ||
error[markdown-no-backticks]: EIP references should not be in backticks | ||
--> input.md | ||
| | ||
5 | `EIP-1234` | ||
| | ||
= info: the pattern in question: `EIP-[0-9]+`</pre | ||
> | ||
</section> | ||
<section> | ||
<h2>Explanation</h2> | ||
|
||
<p> | ||
<code>markdown-no-backticks</code> ensures that authors do not | ||
circumvent EIP link requirements by putting EIP references in | ||
backticks. | ||
</p> | ||
|
||
<p> | ||
While references like <code>`ERC20`</code> or | ||
<code>`IERC7777`</code> are valid as interface names, | ||
<code>`EIP-7777`</code> should be rejected since it is not valid code. | ||
</p> | ||
</section> | ||
</article> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet}; | ||
|
||
use comrak::nodes::{Ast, NodeCode}; | ||
|
||
use crate::lints::{Context, Error, Lint}; | ||
use crate::tree::{self, Next, TraverseExt}; | ||
|
||
use ::regex::Regex; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
// use std::collections::HashSet; | ||
use std::fmt::{Debug, Display}; | ||
|
||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct NoBackticks<S>(pub S); | ||
|
||
impl<S> Lint for NoBackticks<S> | ||
where | ||
S: Display + Debug + AsRef<str>, | ||
{ | ||
fn lint<'a>(&self, slug: &'a str, ctx: &Context<'a, '_>) -> Result<(), Error> { | ||
let pattern = self.0.as_ref(); | ||
let re = Regex::new(pattern).map_err(Error::custom)?; | ||
let mut visitor = Visitor { | ||
ctx, | ||
re, | ||
pattern, | ||
slug, | ||
}; | ||
ctx.body().traverse().visit(&mut visitor)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
struct Visitor<'a, 'b, 'c> { | ||
ctx: &'c Context<'a, 'b>, | ||
re: Regex, | ||
pattern: &'c str, | ||
slug: &'c str, | ||
} | ||
|
||
impl<'a, 'b, 'c> Visitor<'a, 'b, 'c> { | ||
fn check(&mut self, ast: &Ast, text: &str) -> Result<Next, Error> { | ||
if !self.re.is_match(text) { | ||
return Ok(Next::TraverseChildren); | ||
} | ||
|
||
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), | ||
}], | ||
})?; | ||
|
||
Ok(Next::SkipChildren) | ||
} | ||
} | ||
|
||
impl<'a, 'b, 'c> tree::Visitor for Visitor<'a, 'b, 'c> { | ||
type Error = Error; | ||
|
||
fn enter_code(&mut self, ast: &Ast, code: &NodeCode) -> Result<Next, Self::Error> { | ||
self.check(ast, &code.literal) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use eipw_lint::lints::markdown::NoBackticks; | ||
use eipw_lint::reporters::Text; | ||
use eipw_lint::Linter; | ||
|
||
#[tokio::test] | ||
async fn eip_in_backticks() { | ||
let src = r#"--- | ||
header: value1 | ||
--- | ||
hello | ||
`EIP-1234` | ||
"#; | ||
|
||
let linter = Linter::<Text<String>>::default() | ||
.clear_lints() | ||
.deny("markdown-no-backticks", NoBackticks(r"EIP-[0-9]+")); | ||
|
||
let reports = linter | ||
.check_slice(None, src) | ||
.run() | ||
.await | ||
.unwrap() | ||
.into_inner(); | ||
|
||
assert_eq!( | ||
reports, | ||
r#"error[markdown-no-backticks]: EIP references should not be in backticks | ||
| | ||
7 | `EIP-1234` | ||
| | ||
= info: the pattern in question: `EIP-[0-9]+` | ||
"# | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn valid_code_in_backticks() { | ||
let src = r#"--- | ||
header: value1 | ||
--- | ||
hello | ||
`ERC20` and `IERC7777` | ||
"#; | ||
|
||
let reports = Linter::<Text<String>>::default() | ||
.clear_lints() | ||
.deny("markdown-no-backticks", NoBackticks(r"EIP-[0-9]+")) | ||
.check_slice(None, src) | ||
.run() | ||
.await | ||
.unwrap() | ||
.into_inner(); | ||
|
||
assert_eq!(reports, ""); | ||
} |