Skip to content

Commit

Permalink
fix: some minor fixes
Browse files Browse the repository at this point in the history
- Rename rule id's: QM... -> MD...
- Print linting violations in STDERR instead of STDOUT
- Return proper exit code
  • Loading branch information
ekropotin committed Jun 15, 2024
1 parent b8f7030 commit d48791e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
25 changes: 19 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,17 +18,29 @@ 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(),
settings: Settings {},
};

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);
}
22 changes: 11 additions & 11 deletions src/rules/qm001.rs → src/rules/md001.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,13 +18,13 @@ impl QM001Linter {
}
}

impl RuleLinter for QM001Linter {
impl RuleLinter for MD001Linter {
fn feed(&mut self, node: &Ast) -> Option<RuleViolation> {
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),
Expand All @@ -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)]
Expand All @@ -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<dyn RuleLinter>) -> Vec<RuleViolation> {
parse_document(&Arena::new(), input, &Options::default())
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::linter::{Context, RuleLinter};

pub mod qm001;
pub mod md001;

#[derive(Debug)]
pub struct Rule {
Expand Down

0 comments on commit d48791e

Please sign in to comment.