Skip to content

Commit

Permalink
Bit of work on implementation and more tests (ethereum#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilsn committed Apr 21, 2023
1 parent 63e9a46 commit 45bc5f9
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 19 deletions.
29 changes: 10 additions & 19 deletions eipw-lint/src/lints/markdown/relative_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,19 @@ impl<'e> Lint for RelativeLinks<'e> {
{
let mut footer_label = String::new();
let mut footer = vec![];
let mut link_md = String::new();

let line_with_address = str::from_utf8(&address).unwrap();

if line_with_address != "://" {
match re_eip_num.captures(line_with_address.as_bytes()) {
Some(num) => {
let eip_num = str::from_utf8(&num[0]).unwrap();

write!(link_md, "`[EIP-{}](./eip-{}.md`", &eip_num, &eip_num).unwrap();
write!(footer_label, "\n use: {} instead \n", &link_md,).unwrap();

footer.push(Annotation {
annotation_type: AnnotationType::Help,
id: None,
label: Some(&footer_label),
});
}
None => {
write!(footer_label, "None",).unwrap();
}
}
if let Some(num) = re_eip_num.captures(line_with_address.as_bytes()) {
let eip_num = str::from_utf8(&num[0]).unwrap();

write!(footer_label, "use `./eip-{0}.md` instead", &eip_num).unwrap();

footer.push(Annotation {
annotation_type: AnnotationType::Help,
id: None,
label: Some(&footer_label),
});
}

ctx.report(Snippet {
Expand Down
146 changes: 146 additions & 0 deletions eipw-lint/tests/lint_markdown_relative_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,63 @@ header: value1
);
}

#[tokio::test]
async fn inline_link_with_scheme_to_eips_ethereum_org() {
let src = r#"---
header: value1
---
[hello](https://eips.ethereum.org/EIPS/eip-1234)
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | [hello](https://eips.ethereum.org/EIPS/eip-1234)
|
= help: use `./eip-1234.md` instead
"#
);
}

#[tokio::test]
async fn inline_link_with_scheme_and_numbers() {
let src = r#"---
header: value1
---
[hi](https://example.com/4444)
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | [hello](https://example.com/4444)
|
"#
);
}

#[tokio::test]
async fn inline_link_protocol_relative() {
let src = r#"---
Expand Down Expand Up @@ -194,6 +251,37 @@ Hello [hi][hello]!
assert_eq!(reports, "");
}

#[tokio::test]
async fn reference_link_with_scheme_to_eips_ethereum_org() {
let src = r#"---
header: value1
---
Hello [hi][hello]!
[hello]: https://eips.ethereum.org/EIPS/eip-1234
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | Hello [hi][hello]!
|
= help: use `./eip-1234.md` instead
"#
);
}

#[tokio::test]
async fn inline_autolink() {
let src = r#"---
Expand Down Expand Up @@ -280,6 +368,35 @@ header: value1
);
}

#[tokio::test]
async fn anchor_link_protocol_relative_to_eips_ethereum_org() {
let src = r#"---
header: value1
---
<a href="//eips.ethereum.org/EIPS/eip-1234">example</a>
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | <a href="//eips.ethereum.org/EIPS/eip-1234">example</a>
|
= help: use `./eip-1234.md` instead
"#
);
}

#[tokio::test]
async fn anchor_link_relative_double_slash() {
let src = r#"---
Expand Down Expand Up @@ -349,3 +466,32 @@ header: value1
"#
);
}

#[tokio::test]
async fn img_protocol_relative_to_eips_ethereum_org() {
let src = r#"---
header: value1
---
<img src="//eips.ethereum.org/assets/eip-712/eth_sign.png">
"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.deny("markdown-rel", RelativeLinks { exceptions: &[] })
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[markdown-rel]: non-relative link or image
|
5 | <img src="//eips.ethereum.org/assets/eip-712/eth_sign.png">
|
= help: use `../assets/eip-712/eth_sign.png` instead
"#
);
}

0 comments on commit 45bc5f9

Please sign in to comment.