Skip to content

Commit

Permalink
issue-6: Rule porting: MD003 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekropotin authored Jun 17, 2024
1 parent bcdd0c9 commit d05d0dd
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 14 deletions.
15 changes: 14 additions & 1 deletion src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ impl Display for RuleViolation {
)
}
}
#[derive(Clone, Debug)]
pub enum HeadingStyle {
Atx,
Consistent,
Setext,
}

#[derive(Clone)]
pub struct Settings {
//TBD
pub heading_style: HeadingStyle,
}
#[derive(Clone)]
pub struct Context {
Expand Down Expand Up @@ -122,3 +128,10 @@ impl<'a> MultiRuleLinter {
.collect()
}
}

pub fn lint_content(input: &str, linter: &mut Box<dyn RuleLinter>) -> Vec<RuleViolation> {
parse_document(&Arena::new(), input, &Options::default())
.descendants()
.filter_map(|node| linter.feed(&node.data.borrow()))
.collect()
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ fn main() -> anyhow::Result<()> {

let context = quickmark::linter::Context {
file_path: file_path.clone(),
settings: Settings {},
settings: Settings {
heading_style: quickmark::linter::HeadingStyle::Consistent,
},
};

let mut linter = MultiRuleLinter::new(&rules, context);
Expand Down
16 changes: 4 additions & 12 deletions src/rules/md001.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,17 @@ pub const MD001: Rule = Rule {
mod test {
use std::path::PathBuf;

use comrak::{parse_document, Arena, Options};

use crate::linter::{RuleViolation, Settings};
use crate::linter::{lint_content, HeadingStyle, Settings};
use crate::rules::Context;

use super::super::RuleLinter;
use super::MD001;

fn lint_content(input: &str, linter: &mut Box<dyn RuleLinter>) -> Vec<RuleViolation> {
parse_document(&Arena::new(), input, &Options::default())
.descendants()
.filter_map(|node| linter.feed(&node.data.borrow()))
.collect()
}

fn test_context() -> Context {
Context {
file_path: PathBuf::from("test.md"),
settings: Settings {},
settings: Settings {
heading_style: HeadingStyle::Consistent,
},
}
}

Expand Down
134 changes: 134 additions & 0 deletions src/rules/md003.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use comrak::nodes::{Ast, NodeHeading, NodeValue};

use crate::linter::{Context, HeadingStyle, RuleLinter, RuleViolation, RuleViolationSeverity};

use super::Rule;

#[derive(PartialEq, Debug)]
enum Style {
Setext,
Atx,
}

pub(crate) struct MD003Linter {
context: Context,
enforced_style: Option<Style>,
}

impl MD003Linter {
pub fn new(context: Context) -> Self {
let enforced_style = match context.settings.heading_style {
HeadingStyle::Atx => Some(Style::Atx),
HeadingStyle::Setext => Some(Style::Setext),
_ => None,
};
Self {
context,
enforced_style,
}
}
}

fn heading_style(heading: &NodeHeading) -> Style {
match heading.setext {
true => Style::Setext,
false => Style::Atx,
}
}

impl RuleLinter for MD003Linter {
fn feed(&mut self, node: &Ast) -> Option<RuleViolation> {
if let NodeValue::Heading(heading) = node.value {
let style = heading_style(&heading);
if let Some(enforced_style) = &self.enforced_style {
if style != *enforced_style {
return Option::Some(RuleViolation::new(
&MD003,
RuleViolationSeverity::Error,
self.context.file_path.clone(),
&node.sourcepos,
));
}
} else {
self.enforced_style = Some(style);
}
}
None
}
}

pub const MD003: Rule = Rule {
id: "MD003",
alias: "heading-style",
tags: &["headings"],
description: "Heading style",
new_linter: |context| Box::new(MD003Linter::new(context)),
};

#[cfg(test)]
mod test {
use std::path::PathBuf;

use crate::linter::{lint_content, HeadingStyle, Settings};
use crate::rules::Context;

use super::MD003;

#[test]
fn test_heading_style_consistent_positive() {
let context = Context {
file_path: PathBuf::from("test.md"),
settings: Settings {
heading_style: HeadingStyle::Consistent,
},
};
let mut linter = (MD003.new_linter)(context);

let input = "Setext level 1
--------------
Setext level 2
==============
### ATX header level 3
#### ATX header level 4
";
let violations = lint_content(input, &mut linter);
assert_eq!(violations.len(), 2);
}

#[test]
fn test_heading_style_consistent_negative_setext() {
let context = Context {
file_path: PathBuf::from("test.md"),
settings: Settings {
heading_style: HeadingStyle::Consistent,
},
};
let mut linter = (MD003.new_linter)(context);

let input = "Setext level 1
--------------
Setext level 2
==============
";
let violations = lint_content(input, &mut linter);
assert_eq!(violations.len(), 0);
}

#[test]
fn test_heading_style_consistent_negative_atx() {
let context = Context {
file_path: PathBuf::from("test.md"),
settings: Settings {
heading_style: HeadingStyle::Consistent,
},
};
let mut linter = (MD003.new_linter)(context);

let input = "# Atx heading 1
## Atx heading 2
### Atx heading 3
";
let violations = lint_content(input, &mut linter);
assert_eq!(violations.len(), 0);
}
}
1 change: 1 addition & 0 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::linter::{Context, RuleLinter};

pub mod md001;
pub mod md003;

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

0 comments on commit d05d0dd

Please sign in to comment.