Skip to content

Commit

Permalink
Merge pull request #37 from ethereum/bug-35
Browse files Browse the repository at this point in the history
Mandate two digit months and days (closes #35)
  • Loading branch information
SamWilsn authored Aug 27, 2022
2 parents a375535 + 9cc5d82 commit d8d5187
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion eipw-lint-js/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eipw-lint-js"
version = "0.1.6"
version = "0.1.7"
edition = "2021"
license = "MPL-2.0"
rust-version = "1.60"
Expand Down
18 changes: 14 additions & 4 deletions eipw-lint/src/lints/preamble/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@ impl<'n> Lint for Date<'n> {

let value = field.value().trim();

let e = match NaiveDate::parse_from_str(value, "%Y-%m-%d") {
Ok(_) => return Ok(()),
Err(e) => e,
let mut error = None;

let lengths: Vec<_> = value.split('-').map(str::len).collect();
if lengths != [4, 2, 2] {
error = Some("invalid length".to_string());
}

if let Err(e) = NaiveDate::parse_from_str(value, "%Y-%m-%d") {
error = Some(e.to_string());
}

let slice_label = match error {
Some(e) => e,
None => return Ok(()),
};

let label = format!(
"preamble header `{}` is not a date in the `YYYY-MM-DD` format",
self.0
);
let slice_label = e.to_string();

ctx.report(Snippet {
title: Some(Annotation {
Expand Down
54 changes: 54 additions & 0 deletions eipw-lint/tests/lint_preamble_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,60 @@ use eipw_lint::lints::preamble::Date;
use eipw_lint::reporters::Text;
use eipw_lint::Linter;

#[tokio::test]
async fn single_digit_month() {
let src = r#"---
header: 2022-1-01
---
hello world"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.add_lint("preamble-date", Date("header"))
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[preamble-date]: preamble header `header` is not a date in the `YYYY-MM-DD` format
|
2 | header: 2022-1-01
| ^^^^^^^^^^ invalid length
|
"#,
);
}

#[tokio::test]
async fn single_digit_day() {
let src = r#"---
header: 2022-01-1
---
hello world"#;

let reports = Linter::<Text<String>>::default()
.clear_lints()
.add_lint("preamble-date", Date("header"))
.check_slice(None, src)
.run()
.await
.unwrap()
.into_inner();

assert_eq!(
reports,
r#"error[preamble-date]: preamble header `header` is not a date in the `YYYY-MM-DD` format
|
2 | header: 2022-01-1
| ^^^^^^^^^^ invalid length
|
"#,
);
}

#[tokio::test]
async fn invalid() {
let src = r#"---
Expand Down

0 comments on commit d8d5187

Please sign in to comment.