From d48791eda2d1627b04c14d020e9647a910e2a481 Mon Sep 17 00:00:00 2001 From: Evgeny Kropotin Date: Fri, 14 Jun 2024 17:33:11 -0700 Subject: [PATCH] fix: some minor fixes - Rename rule id's: QM... -> MD... - Print linting violations in STDERR instead of STDOUT - Return proper exit code --- src/main.rs | 25 +++++++++++++++++++------ src/rules/{qm001.rs => md001.rs} | 22 +++++++++++----------- src/rules/mod.rs | 2 +- 3 files changed, 31 insertions(+), 18 deletions(-) rename src/rules/{qm001.rs => md001.rs} (87%) diff --git a/src/main.rs b/src/main.rs index 3370666..d9d9d86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,9 @@ use anyhow::Context; use clap::Parser; +use quickmark::linter::RuleViolationSeverity::Error; use quickmark::linter::{MultiRuleLinter, Settings}; -use std::{fs, path::PathBuf}; - +use std::cmp::min; +use std::{fs, path::PathBuf, process::exit}; #[derive(Parser, Debug)] #[command(version, about = "Quickmark: An extremely fast CommonMark linter")] struct Cli { @@ -17,7 +18,7 @@ fn main() -> anyhow::Result<()> { let file_content = fs::read_to_string(&file_path) .context(format!("Can't read file {}", &file_path.to_string_lossy()))?; - let rules = vec![quickmark::rules::qm001::QM001]; + let rules = vec![quickmark::rules::md001::MD001]; let context = quickmark::linter::Context { file_path: file_path.clone(), @@ -25,9 +26,21 @@ fn main() -> anyhow::Result<()> { }; let mut linter = MultiRuleLinter::new(&rules, context); - linter + + let (warns, errs) = linter .lint(&file_content) .iter() - .for_each(|v| print!("{}", v)); - Ok(()) + .fold((0, 0), |(warns, errs), v| { + eprint!("{}", v); + match &v.severity { + Error => (warns, errs + 1), + _ => (warns + 1, errs), + } + }); + + println!("\n\nErrors: {}", errs); + println!("Warnings: {}", warns); + + let exit_code = min(errs, 1); + exit(exit_code); } diff --git a/src/rules/qm001.rs b/src/rules/md001.rs similarity index 87% rename from src/rules/qm001.rs rename to src/rules/md001.rs index 2de2b61..58417a2 100644 --- a/src/rules/qm001.rs +++ b/src/rules/md001.rs @@ -4,12 +4,12 @@ use crate::{ }; use comrak::nodes::{Ast, NodeHeading, NodeValue}; -pub(crate) struct QM001Linter { +pub(crate) struct MD001Linter { context: Context, current_heading_level: u8, } -impl QM001Linter { +impl MD001Linter { pub fn new(context: Context) -> Self { Self { context, @@ -18,13 +18,13 @@ impl QM001Linter { } } -impl RuleLinter for QM001Linter { +impl RuleLinter for MD001Linter { fn feed(&mut self, node: &Ast) -> Option { if let NodeValue::Heading(NodeHeading { level, setext: _ }) = node.value { if self.current_heading_level > 0 && level as i8 - self.current_heading_level as i8 > 1 { return Option::Some(RuleViolation::new( - &QM001, + &MD001, RuleViolationSeverity::Error, self.context.file_path.clone(), &(node.sourcepos), @@ -36,12 +36,12 @@ impl RuleLinter for QM001Linter { } } -pub const QM001: Rule = Rule { - id: "QM001", +pub const MD001: Rule = Rule { + id: "MD001", alias: "heading-increment", tags: &["headings"], description: "Heading levels should only increment by one level at a time", - new_linter: |context| Box::new(QM001Linter::new(context)), + new_linter: |context| Box::new(MD001Linter::new(context)), }; #[cfg(test)] @@ -54,7 +54,7 @@ mod test { use crate::rules::Context; use super::super::RuleLinter; - use super::QM001; + use super::MD001; fn lint_content(input: &str, linter: &mut Box) -> Vec { parse_document(&Arena::new(), input, &Options::default()) @@ -83,7 +83,7 @@ foobar ### Heading level 3 "; - let violations = lint_content(input, &mut (QM001.new_linter)(test_context())); + let violations = lint_content(input, &mut (MD001.new_linter)(test_context())); assert_eq!(2, violations.len()); let mut iter = violations.iter(); let range1 = &iter.next().unwrap().location.range; @@ -113,7 +113,7 @@ foobar ###### Heading level 6 "; - let violations = lint_content(input, &mut (QM001.new_linter)(test_context())); + let violations = lint_content(input, &mut (MD001.new_linter)(test_context())); assert_eq!(0, violations.len()); } @@ -131,7 +131,7 @@ foobar # level 1 "; - let violations = lint_content(input, &mut (QM001.new_linter)(test_context())); + let violations = lint_content(input, &mut (MD001.new_linter)(test_context())); assert_eq!(0, violations.len()); } } diff --git a/src/rules/mod.rs b/src/rules/mod.rs index df6e60e..20dd2b4 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -1,6 +1,6 @@ use crate::linter::{Context, RuleLinter}; -pub mod qm001; +pub mod md001; #[derive(Debug)] pub struct Rule {