use crate::utils::fast_hash;
use crate::utils::regex_cache::{escape_regex, get_cached_regex};
use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
mod md044_config;
pub use md044_config::MD044Config;
type WarningPosition = (usize, usize, String);
fn is_inline_config_comment(trimmed: &str) -> bool {
trimmed.starts_with("<!-- rumdl-")
|| trimmed.starts_with("<!-- markdownlint-")
|| trimmed.starts_with("<!-- vale off")
|| trimmed.starts_with("<!-- vale on")
|| (trimmed.starts_with("<!-- vale ") && trimmed.contains(" = "))
|| trimmed.starts_with("<!-- vale style")
|| trimmed.starts_with("<!-- lint disable ")
|| trimmed.starts_with("<!-- lint enable ")
|| trimmed.starts_with("<!-- lint ignore ")
}
#[derive(Clone)]
pub struct MD044ProperNames {
config: MD044Config,
combined_pattern: Option<String>,
name_variants: Vec<String>,
content_cache: Arc<Mutex<HashMap<u64, Vec<WarningPosition>>>>,
}
impl MD044ProperNames {
pub fn new(names: Vec<String>, code_blocks: bool) -> Self {
let config = MD044Config {
names,
code_blocks,
html_elements: true, html_comments: true, };
let combined_pattern = Self::create_combined_pattern(&config);
let name_variants = Self::build_name_variants(&config);
Self {
config,
combined_pattern,
name_variants,
content_cache: Arc::new(Mutex::new(HashMap::new())),
}
}
fn ascii_normalize(s: &str) -> String {
s.replace(['é', 'è', 'ê', 'ë'], "e")
.replace(['à ', 'á', 'â', 'ä', 'ã', 'å'], "a")
.replace(['ï', 'î', 'Ã', 'ì'], "i")
.replace(['ü', 'ú', 'ù', 'û'], "u")
.replace(['ö', 'ó', 'ò', 'ô', 'õ'], "o")
.replace('ñ', "n")
.replace('ç', "c")
}
pub fn from_config_struct(config: MD044Config) -> Self {
let combined_pattern = Self::create_combined_pattern(&config);
let name_variants = Self::build_name_variants(&config);
Self {
config,
combined_pattern,
name_variants,
content_cache: Arc::new(Mutex::new(HashMap::new())),
}
}
fn create_combined_pattern(config: &MD044Config) -> Option<String> {
if config.names.is_empty() {
return None;
}
let mut patterns: Vec<String> = config
.names
.iter()
.flat_map(|name| {
let mut variations = vec![];
let lower_name = name.to_lowercase();
variations.push(escape_regex(&lower_name));
let lower_name_no_dots = lower_name.replace('.', "");
if lower_name != lower_name_no_dots {
variations.push(escape_regex(&lower_name_no_dots));
}
let ascii_normalized = Self::ascii_normalize(&lower_name);
if ascii_normalized != lower_name {
variations.push(escape_regex(&ascii_normalized));
let ascii_no_dots = ascii_normalized.replace('.', "");
if ascii_normalized != ascii_no_dots {
variations.push(escape_regex(&ascii_no_dots));
}
}
variations
})
.collect();
patterns.sort_by_key(|b| std::cmp::Reverse(b.len()));
Some(format!(r"(?i)({})", patterns.join("|")))
}
fn build_name_variants(config: &MD044Config) -> Vec<String> {
let mut variants = HashSet::new();
for name in &config.names {
let lower_name = name.to_lowercase();
variants.insert(lower_name.clone());
let lower_no_dots = lower_name.replace('.', "");
if lower_name != lower_no_dots {
variants.insert(lower_no_dots);
}
let ascii_normalized = Self::ascii_normalize(&lower_name);
if ascii_normalized != lower_name {
variants.insert(ascii_normalized.clone());
let ascii_no_dots = ascii_normalized.replace('.', "");
if ascii_normalized != ascii_no_dots {
variants.insert(ascii_no_dots);
}
}
}
variants.into_iter().collect()
}
fn find_name_violations(
&self,
content: &str,
ctx: &crate::lint_context::LintContext,
content_lower: &str,
) -> Vec<WarningPosition> {
if self.config.names.is_empty() || content.is_empty() || self.combined_pattern.is_none() {
return Vec::new();
}
let has_potential_matches = self.name_variants.iter().any(|name| content_lower.contains(name));
if !has_potential_matches {
return Vec::new();
}
let hash = fast_hash(content);
{
if let Ok(cache) = self.content_cache.lock()
&& let Some(cached) = cache.get(&hash)
{
return cached.clone();
}
}
let mut violations = Vec::new();
let combined_regex = match &self.combined_pattern {
Some(pattern) => match get_cached_regex(pattern) {
Ok(regex) => regex,
Err(_) => return Vec::new(),
},
None => return Vec::new(),
};
for (line_idx, line_info) in ctx.lines.iter().enumerate() {
let line_num = line_idx + 1;
let line = line_info.content(ctx.content);
let trimmed = line.trim_start();
if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
continue;
}
if !self.config.code_blocks && line_info.in_code_block {
continue;
}
if !self.config.html_elements && line_info.in_html_block {
continue;
}
if !self.config.html_comments && line_info.in_html_comment {
continue;
}
if line_info.in_jsx_expression || line_info.in_mdx_comment {
continue;
}
if line_info.in_obsidian_comment {
continue;
}
let fm_value_offset = if line_info.in_front_matter {
Self::frontmatter_value_offset(line)
} else {
0
};
if fm_value_offset == usize::MAX {
continue;
}
if is_inline_config_comment(trimmed) {
continue;
}
let line_lower = line.to_lowercase();
let has_line_matches = self.name_variants.iter().any(|name| line_lower.contains(name));
if !has_line_matches {
continue;
}
for cap in combined_regex.find_iter(line) {
let found_name = &line[cap.start()..cap.end()];
let start_pos = cap.start();
let end_pos = cap.end();
if start_pos < fm_value_offset {
continue;
}
let byte_pos = line_info.byte_offset + start_pos;
if ctx.is_in_html_tag(byte_pos) {
continue;
}
if !Self::is_at_word_boundary(line, start_pos, true) || !Self::is_at_word_boundary(line, end_pos, false)
{
continue; }
if !self.config.code_blocks {
if ctx.is_in_code_block_or_span(byte_pos) {
continue;
}
if (line_info.in_html_comment || line_info.in_html_block || line_info.in_front_matter)
&& Self::is_in_backtick_code_in_line(line, start_pos)
{
continue;
}
}
if Self::is_in_link(ctx, byte_pos) {
continue;
}
if Self::is_in_angle_bracket_url(line, start_pos) {
continue;
}
if let Some(proper_name) = self.get_proper_name_for(found_name) {
if found_name != proper_name {
violations.push((line_num, cap.start() + 1, found_name.to_string()));
}
}
}
}
if let Ok(mut cache) = self.content_cache.lock() {
cache.insert(hash, violations.clone());
}
violations
}
fn is_in_link(ctx: &crate::lint_context::LintContext, byte_pos: usize) -> bool {
use pulldown_cmark::LinkType;
let link_idx = ctx.links.partition_point(|link| link.byte_offset <= byte_pos);
if link_idx > 0 {
let link = &ctx.links[link_idx - 1];
if byte_pos < link.byte_end {
let text_start = if matches!(link.link_type, LinkType::WikiLink { .. }) {
link.byte_offset + 2
} else {
link.byte_offset + 1
};
let text_end = text_start + link.text.len();
if byte_pos >= text_start && byte_pos < text_end {
return Self::link_text_is_url(&link.text);
}
return true;
}
}
let image_idx = ctx.images.partition_point(|img| img.byte_offset <= byte_pos);
if image_idx > 0 {
let image = &ctx.images[image_idx - 1];
if byte_pos < image.byte_end {
let alt_start = image.byte_offset + 2;
let alt_end = alt_start + image.alt_text.len();
if byte_pos >= alt_start && byte_pos < alt_end {
return false;
}
return true;
}
}
ctx.is_in_reference_def(byte_pos)
}
fn link_text_is_url(text: &str) -> bool {
let lower = text.trim().to_ascii_lowercase();
lower.starts_with("http://") || lower.starts_with("https://") || lower.starts_with("www.")
}
fn is_in_angle_bracket_url(line: &str, pos: usize) -> bool {
let bytes = line.as_bytes();
let len = bytes.len();
let mut i = 0;
while i < len {
if bytes[i] == b'<' {
let after_open = i + 1;
if after_open < len && bytes[after_open].is_ascii_alphabetic() {
let mut s = after_open + 1;
let scheme_max = (after_open + 32).min(len);
while s < scheme_max
&& (bytes[s].is_ascii_alphanumeric()
|| bytes[s] == b'+'
|| bytes[s] == b'-'
|| bytes[s] == b'.')
{
s += 1;
}
if s < len && bytes[s] == b':' {
let mut j = s + 1;
let mut found_close = false;
while j < len {
match bytes[j] {
b'>' => {
found_close = true;
break;
}
b' ' | b'<' => break,
_ => j += 1,
}
}
if found_close && pos >= i && pos <= j {
return true;
}
if found_close {
i = j + 1;
continue;
}
}
}
}
i += 1;
}
false
}
fn is_in_backtick_code_in_line(line: &str, pos: usize) -> bool {
let bytes = line.as_bytes();
let len = bytes.len();
let mut i = 0;
while i < len {
if bytes[i] == b'`' {
let open_start = i;
while i < len && bytes[i] == b'`' {
i += 1;
}
let tick_len = i - open_start;
while i < len {
if bytes[i] == b'`' {
let close_start = i;
while i < len && bytes[i] == b'`' {
i += 1;
}
if i - close_start == tick_len {
let content_start = open_start + tick_len;
let content_end = close_start;
if pos >= content_start && pos < content_end {
return true;
}
break;
}
} else {
i += 1;
}
}
} else {
i += 1;
}
}
false
}
fn is_word_boundary_char(c: char) -> bool {
!c.is_alphanumeric()
}
fn is_at_word_boundary(content: &str, pos: usize, is_start: bool) -> bool {
if is_start {
if pos == 0 {
return true;
}
match content[..pos].chars().next_back() {
None => true,
Some(c) => Self::is_word_boundary_char(c),
}
} else {
if pos >= content.len() {
return true;
}
match content[pos..].chars().next() {
None => true,
Some(c) => Self::is_word_boundary_char(c),
}
}
}
fn frontmatter_value_offset(line: &str) -> usize {
let trimmed = line.trim();
if trimmed == "---" || trimmed == "+++" || trimmed.is_empty() {
return usize::MAX;
}
if trimmed.starts_with('#') {
return usize::MAX;
}
let stripped = line.trim_start();
if let Some(after_dash) = stripped.strip_prefix("- ") {
let leading = line.len() - stripped.len();
if let Some(result) = Self::kv_value_offset(line, after_dash, leading + 2) {
return result;
}
return leading + 2;
}
if stripped == "-" {
return usize::MAX;
}
if let Some(result) = Self::kv_value_offset(line, stripped, line.len() - stripped.len()) {
return result;
}
if let Some(eq_pos) = line.find('=') {
let after_eq = eq_pos + 1;
if after_eq < line.len() && line.as_bytes()[after_eq] == b' ' {
let value_start = after_eq + 1;
let value_slice = &line[value_start..];
let value_trimmed = value_slice.trim();
if value_trimmed.is_empty() {
return usize::MAX;
}
if (value_trimmed.starts_with('"') && value_trimmed.ends_with('"'))
|| (value_trimmed.starts_with('\'') && value_trimmed.ends_with('\''))
{
let quote_offset = value_slice.find(['"', '\'']).unwrap_or(0);
return value_start + quote_offset + 1;
}
return value_start;
}
return usize::MAX;
}
0
}
fn kv_value_offset(line: &str, content: &str, base_offset: usize) -> Option<usize> {
let colon_pos = content.find(':')?;
let abs_colon = base_offset + colon_pos;
let after_colon = abs_colon + 1;
if after_colon < line.len() && line.as_bytes()[after_colon] == b' ' {
let value_start = after_colon + 1;
let value_slice = &line[value_start..];
let value_trimmed = value_slice.trim();
if value_trimmed.is_empty() {
return Some(usize::MAX);
}
if value_trimmed.starts_with('{') || value_trimmed.starts_with('[') {
return Some(usize::MAX);
}
if (value_trimmed.starts_with('"') && value_trimmed.ends_with('"'))
|| (value_trimmed.starts_with('\'') && value_trimmed.ends_with('\''))
{
let quote_offset = value_slice.find(['"', '\'']).unwrap_or(0);
return Some(value_start + quote_offset + 1);
}
return Some(value_start);
}
Some(usize::MAX)
}
fn get_proper_name_for(&self, found_name: &str) -> Option<String> {
let found_lower = found_name.to_lowercase();
for name in &self.config.names {
let lower_name = name.to_lowercase();
let lower_name_no_dots = lower_name.replace('.', "");
if found_lower == lower_name || found_lower == lower_name_no_dots {
return Some(name.clone());
}
let ascii_normalized = Self::ascii_normalize(&lower_name);
let ascii_no_dots = ascii_normalized.replace('.', "");
if found_lower == ascii_normalized || found_lower == ascii_no_dots {
return Some(name.clone());
}
}
None
}
}
impl Rule for MD044ProperNames {
fn name(&self) -> &'static str {
"MD044"
}
fn description(&self) -> &'static str {
"Proper names should have the correct capitalization"
}
fn category(&self) -> RuleCategory {
RuleCategory::Other
}
fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
if self.config.names.is_empty() {
return true;
}
let content_lower = if ctx.content.is_ascii() {
ctx.content.to_ascii_lowercase()
} else {
ctx.content.to_lowercase()
};
!self.name_variants.iter().any(|name| content_lower.contains(name))
}
fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
let content = ctx.content;
if content.is_empty() || self.config.names.is_empty() || self.combined_pattern.is_none() {
return Ok(Vec::new());
}
let content_lower = if content.is_ascii() {
content.to_ascii_lowercase()
} else {
content.to_lowercase()
};
let has_potential_matches = self.name_variants.iter().any(|name| content_lower.contains(name));
if !has_potential_matches {
return Ok(Vec::new());
}
let line_index = &ctx.line_index;
let violations = self.find_name_violations(content, ctx, &content_lower);
let warnings = violations
.into_iter()
.filter_map(|(line, column, found_name)| {
self.get_proper_name_for(&found_name).map(|proper_name| LintWarning {
rule_name: Some(self.name().to_string()),
line,
column,
end_line: line,
end_column: column + found_name.len(),
message: format!("Proper name '{found_name}' should be '{proper_name}'"),
severity: Severity::Warning,
fix: Some(Fix {
range: line_index.line_col_to_byte_range_with_length(line, column, found_name.len()),
replacement: proper_name,
}),
})
})
.collect();
Ok(warnings)
}
fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
let content = ctx.content;
if content.is_empty() || self.config.names.is_empty() {
return Ok(content.to_string());
}
let content_lower = if content.is_ascii() {
content.to_ascii_lowercase()
} else {
content.to_lowercase()
};
let violations = self.find_name_violations(content, ctx, &content_lower);
if violations.is_empty() {
return Ok(content.to_string());
}
let mut fixed_lines = Vec::new();
let mut violations_by_line: HashMap<usize, Vec<(usize, String)>> = HashMap::new();
for (line_num, col_num, found_name) in violations {
violations_by_line
.entry(line_num)
.or_default()
.push((col_num, found_name));
}
for violations in violations_by_line.values_mut() {
violations.sort_by_key(|b| std::cmp::Reverse(b.0));
}
for (line_idx, line_info) in ctx.lines.iter().enumerate() {
let line_num = line_idx + 1;
if ctx.inline_config().is_rule_disabled(self.name(), line_num) {
fixed_lines.push(line_info.content(ctx.content).to_string());
continue;
}
if let Some(line_violations) = violations_by_line.get(&line_num) {
let mut fixed_line = line_info.content(ctx.content).to_string();
for (col_num, found_name) in line_violations {
if let Some(proper_name) = self.get_proper_name_for(found_name) {
let start_col = col_num - 1; let end_col = start_col + found_name.len();
if end_col <= fixed_line.len()
&& fixed_line.is_char_boundary(start_col)
&& fixed_line.is_char_boundary(end_col)
{
fixed_line.replace_range(start_col..end_col, &proper_name);
}
}
}
fixed_lines.push(fixed_line);
} else {
fixed_lines.push(line_info.content(ctx.content).to_string());
}
}
let mut result = fixed_lines.join("\n");
if content.ends_with('\n') && !result.ends_with('\n') {
result.push('\n');
}
Ok(result)
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn default_config_section(&self) -> Option<(String, toml::Value)> {
let json_value = serde_json::to_value(&self.config).ok()?;
Some((
self.name().to_string(),
crate::rule_config_serde::json_to_toml_value(&json_value)?,
))
}
fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
where
Self: Sized,
{
let rule_config = crate::rule_config_serde::load_rule_config::<MD044Config>(config);
Box::new(Self::from_config_struct(rule_config))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::lint_context::LintContext;
fn create_context(content: &str) -> LintContext<'_> {
LintContext::new(content, crate::config::MarkdownFlavor::Standard, None)
}
#[test]
fn test_correctly_capitalized_names() {
let rule = MD044ProperNames::new(
vec![
"JavaScript".to_string(),
"TypeScript".to_string(),
"Node.js".to_string(),
],
true,
);
let content = "This document uses JavaScript, TypeScript, and Node.js correctly.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Should not flag correctly capitalized names");
}
#[test]
fn test_incorrectly_capitalized_names() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
let content = "This document uses javascript and typescript incorrectly.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should flag two incorrect capitalizations");
assert_eq!(result[0].message, "Proper name 'javascript' should be 'JavaScript'");
assert_eq!(result[0].line, 1);
assert_eq!(result[0].column, 20);
assert_eq!(result[1].message, "Proper name 'typescript' should be 'TypeScript'");
assert_eq!(result[1].line, 1);
assert_eq!(result[1].column, 35);
}
#[test]
fn test_names_at_beginning_of_sentences() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "Python".to_string()], true);
let content = "javascript is a great language. python is also popular.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should flag names at beginning of sentences");
assert_eq!(result[0].line, 1);
assert_eq!(result[0].column, 1);
assert_eq!(result[1].line, 1);
assert_eq!(result[1].column, 33);
}
#[test]
fn test_names_in_code_blocks_checked_by_default() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = r#"Here is some text with JavaScript.
```javascript
// This javascript should be checked
const lang = "javascript";
```
But this javascript should be flagged."#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 3, "Should flag javascript inside and outside code blocks");
assert_eq!(result[0].line, 4);
assert_eq!(result[1].line, 5);
assert_eq!(result[2].line, 8);
}
#[test]
fn test_names_in_code_blocks_ignored_when_disabled() {
let rule = MD044ProperNames::new(
vec!["JavaScript".to_string()],
false, );
let content = r#"```
javascript in code block
```"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
0,
"Should not flag javascript in code blocks when code_blocks is false"
);
}
#[test]
fn test_names_in_inline_code_checked_by_default() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = "This is `javascript` in inline code and javascript outside.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should flag javascript inside and outside inline code");
assert_eq!(result[0].column, 10); assert_eq!(result[1].column, 41); }
#[test]
fn test_multiple_names_in_same_line() {
let rule = MD044ProperNames::new(
vec!["JavaScript".to_string(), "TypeScript".to_string(), "React".to_string()],
true,
);
let content = "I use javascript, typescript, and react in my projects.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 3, "Should flag all three incorrect names");
assert_eq!(result[0].message, "Proper name 'javascript' should be 'JavaScript'");
assert_eq!(result[1].message, "Proper name 'typescript' should be 'TypeScript'");
assert_eq!(result[2].message, "Proper name 'react' should be 'React'");
}
#[test]
fn test_case_sensitivity() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = "JAVASCRIPT, Javascript, javascript, and JavaScript variations.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 3, "Should flag all incorrect case variations");
assert!(result.iter().all(|w| w.message.contains("should be 'JavaScript'")));
}
#[test]
fn test_configuration_with_custom_name_list() {
let config = MD044Config {
names: vec!["GitHub".to_string(), "GitLab".to_string(), "DevOps".to_string()],
code_blocks: true,
html_elements: true,
html_comments: true,
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "We use github, gitlab, and devops for our workflow.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 3, "Should flag all custom names");
assert_eq!(result[0].message, "Proper name 'github' should be 'GitHub'");
assert_eq!(result[1].message, "Proper name 'gitlab' should be 'GitLab'");
assert_eq!(result[2].message, "Proper name 'devops' should be 'DevOps'");
}
#[test]
fn test_empty_configuration() {
let rule = MD044ProperNames::new(vec![], true);
let content = "This has javascript and typescript but no configured names.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Should not flag anything with empty configuration");
}
#[test]
fn test_names_with_special_characters() {
let rule = MD044ProperNames::new(
vec!["Node.js".to_string(), "ASP.NET".to_string(), "C++".to_string()],
true,
);
let content = "We use nodejs, asp.net, ASP.NET, and c++ in our stack.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 3, "Should handle special characters correctly");
let messages: Vec<&str> = result.iter().map(|w| w.message.as_str()).collect();
assert!(messages.contains(&"Proper name 'nodejs' should be 'Node.js'"));
assert!(messages.contains(&"Proper name 'asp.net' should be 'ASP.NET'"));
assert!(messages.contains(&"Proper name 'c++' should be 'C++'"));
}
#[test]
fn test_word_boundaries() {
let rule = MD044ProperNames::new(vec!["Java".to_string(), "Script".to_string()], true);
let content = "JavaScript is not java or script, but Java and Script are separate.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should respect word boundaries");
assert!(result.iter().any(|w| w.column == 19)); assert!(result.iter().any(|w| w.column == 27)); }
#[test]
fn test_fix_method() {
let rule = MD044ProperNames::new(
vec![
"JavaScript".to_string(),
"TypeScript".to_string(),
"Node.js".to_string(),
],
true,
);
let content = "I love javascript, typescript, and nodejs!";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(fixed, "I love JavaScript, TypeScript, and Node.js!");
}
#[test]
fn test_fix_multiple_occurrences() {
let rule = MD044ProperNames::new(vec!["Python".to_string()], true);
let content = "python is great. I use python daily. PYTHON is powerful.";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(fixed, "Python is great. I use Python daily. Python is powerful.");
}
#[test]
fn test_fix_checks_code_blocks_by_default() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = r#"I love javascript.
```
const lang = "javascript";
```
More javascript here."#;
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
let expected = r#"I love JavaScript.
```
const lang = "JavaScript";
```
More JavaScript here."#;
assert_eq!(fixed, expected);
}
#[test]
fn test_multiline_content() {
let rule = MD044ProperNames::new(vec!["Rust".to_string(), "Python".to_string()], true);
let content = r#"First line with rust.
Second line with python.
Third line with RUST and PYTHON."#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 4, "Should flag all incorrect occurrences");
assert_eq!(result[0].line, 1);
assert_eq!(result[1].line, 2);
assert_eq!(result[2].line, 3);
assert_eq!(result[3].line, 3);
}
#[test]
fn test_default_config() {
let config = MD044Config::default();
assert!(config.names.is_empty());
assert!(!config.code_blocks);
assert!(config.html_elements);
assert!(config.html_comments);
}
#[test]
fn test_default_config_checks_html_comments() {
let config = MD044Config {
names: vec!["JavaScript".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "# Guide\n\n<!-- javascript mentioned here -->\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Default config should check HTML comments");
assert_eq!(result[0].line, 3);
}
#[test]
fn test_default_config_skips_code_blocks() {
let config = MD044Config {
names: vec!["JavaScript".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "# Guide\n\n```\njavascript in code\n```\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 0, "Default config should skip code blocks");
}
#[test]
fn test_standalone_html_comment_checked() {
let config = MD044Config {
names: vec!["Test".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "# Heading\n\n<!-- this is a test example -->\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should flag proper name in standalone HTML comment");
assert_eq!(result[0].line, 3);
}
#[test]
fn test_inline_config_comments_not_flagged() {
let config = MD044Config {
names: vec!["RUMDL".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "<!-- rumdl-disable MD044 -->\nSome rumdl text here.\n<!-- rumdl-enable MD044 -->\n<!-- markdownlint-disable -->\nMore rumdl text.\n<!-- markdownlint-enable -->\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should only flag body lines, not config comments");
assert_eq!(result[0].line, 2);
assert_eq!(result[1].line, 5);
}
#[test]
fn test_html_comment_skipped_when_disabled() {
let config = MD044Config {
names: vec!["Test".to_string()],
code_blocks: true,
html_elements: true,
html_comments: false,
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "# Heading\n\n<!-- this is a test example -->\n\nRegular test here.\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only flag 'test' outside HTML comment when html_comments=false"
);
assert_eq!(result[0].line, 5);
}
#[test]
fn test_fix_corrects_html_comment_content() {
let config = MD044Config {
names: vec!["JavaScript".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "# Guide\n\n<!-- javascript mentioned here -->\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(fixed, "# Guide\n\n<!-- JavaScript mentioned here -->\n");
}
#[test]
fn test_fix_does_not_modify_inline_config_comments() {
let config = MD044Config {
names: vec!["RUMDL".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "<!-- rumdl-disable -->\nSome rumdl text.\n<!-- rumdl-enable -->\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert!(fixed.contains("<!-- rumdl-disable -->"));
assert!(fixed.contains("<!-- rumdl-enable -->"));
assert!(
fixed.contains("Some rumdl text."),
"Line inside rumdl-disable block should not be modified by fix()"
);
}
#[test]
fn test_fix_respects_inline_disable_partial() {
let config = MD044Config {
names: vec!["RUMDL".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content =
"<!-- rumdl-disable MD044 -->\nSome rumdl text.\n<!-- rumdl-enable MD044 -->\n\nSome rumdl text outside.\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert!(
fixed.contains("Some rumdl text.\n<!-- rumdl-enable"),
"Line inside disable block should not be modified"
);
assert!(
fixed.contains("Some RUMDL text outside."),
"Line outside disable block should be fixed"
);
}
#[test]
fn test_performance_with_many_names() {
let mut names = vec![];
for i in 0..50 {
names.push(format!("ProperName{i}"));
}
let rule = MD044ProperNames::new(names, true);
let content = "This has propername0, propername25, and propername49 incorrectly.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 3, "Should handle many configured names efficiently");
}
#[test]
fn test_large_name_count_performance() {
let names = (0..1000).map(|i| format!("ProperName{i}")).collect::<Vec<_>>();
let rule = MD044ProperNames::new(names, true);
assert!(rule.combined_pattern.is_some());
let content = "This has propername0 and propername999 in it.";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should handle 1000 names without issues");
}
#[test]
fn test_cache_behavior() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = "Using javascript here.";
let ctx = create_context(content);
let result1 = rule.check(&ctx).unwrap();
assert_eq!(result1.len(), 1);
let result2 = rule.check(&ctx).unwrap();
assert_eq!(result2.len(), 1);
assert_eq!(result1[0].line, result2[0].line);
assert_eq!(result1[0].column, result2[0].column);
}
#[test]
fn test_html_comments_not_checked_when_disabled() {
let config = MD044Config {
names: vec!["JavaScript".to_string()],
code_blocks: true, html_elements: true, html_comments: false, };
let rule = MD044ProperNames::from_config_struct(config);
let content = r#"Regular javascript here.
<!-- This javascript in HTML comment should be ignored -->
More javascript outside."#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should only flag javascript outside HTML comments");
assert_eq!(result[0].line, 1);
assert_eq!(result[1].line, 3);
}
#[test]
fn test_html_comments_checked_when_enabled() {
let config = MD044Config {
names: vec!["JavaScript".to_string()],
code_blocks: true, html_elements: true, html_comments: true, };
let rule = MD044ProperNames::from_config_struct(config);
let content = r#"Regular javascript here.
<!-- This javascript in HTML comment should be checked -->
More javascript outside."#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
3,
"Should flag all javascript occurrences including in HTML comments"
);
}
#[test]
fn test_multiline_html_comments() {
let config = MD044Config {
names: vec!["Python".to_string(), "JavaScript".to_string()],
code_blocks: true, html_elements: true, html_comments: false, };
let rule = MD044ProperNames::from_config_struct(config);
let content = r#"Regular python here.
<!--
This is a multiline comment
with javascript and python
that should be ignored
-->
More javascript outside."#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should only flag names outside HTML comments");
assert_eq!(result[0].line, 1); assert_eq!(result[1].line, 7); }
#[test]
fn test_fix_preserves_html_comments_when_disabled() {
let config = MD044Config {
names: vec!["JavaScript".to_string()],
code_blocks: true, html_elements: true, html_comments: false, };
let rule = MD044ProperNames::from_config_struct(config);
let content = r#"javascript here.
<!-- javascript in comment -->
More javascript."#;
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
let expected = r#"JavaScript here.
<!-- javascript in comment -->
More JavaScript."#;
assert_eq!(
fixed, expected,
"Should not fix names inside HTML comments when disabled"
);
}
#[test]
fn test_proper_names_in_link_text_are_flagged() {
let rule = MD044ProperNames::new(
vec!["JavaScript".to_string(), "Node.js".to_string(), "Python".to_string()],
true,
);
let content = r#"Check this [javascript documentation](https://javascript.info) for info.
Visit [node.js homepage](https://nodejs.org) and [python tutorial](https://python.org).
Real javascript should be flagged.
Also see the [typescript guide][ts-ref] for more.
Real python should be flagged too.
[ts-ref]: https://typescript.org/handbook"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 5, "Expected 5 warnings: 3 in link text + 2 standalone");
let line_1_warnings: Vec<_> = result.iter().filter(|w| w.line == 1).collect();
assert_eq!(line_1_warnings.len(), 1);
assert!(
line_1_warnings[0]
.message
.contains("'javascript' should be 'JavaScript'")
);
let line_3_warnings: Vec<_> = result.iter().filter(|w| w.line == 3).collect();
assert_eq!(line_3_warnings.len(), 2);
assert!(result.iter().any(|w| w.line == 5 && w.message.contains("'javascript'")));
assert!(result.iter().any(|w| w.line == 9 && w.message.contains("'python'")));
}
#[test]
fn test_link_urls_not_flagged() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = r#"[Link Text](https://javascript.info/guide)"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "URLs should not be checked for proper names");
}
#[test]
fn test_proper_names_in_image_alt_text_are_flagged() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = r#"Here is a  image.
Real javascript should be flagged."#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in alt text + 1 standalone");
assert!(result[0].message.contains("'javascript' should be 'JavaScript'"));
assert!(result[0].line == 1); assert!(result[1].message.contains("'javascript' should be 'JavaScript'"));
assert!(result[1].line == 3); }
#[test]
fn test_image_urls_not_flagged() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = r#""#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Image URLs should not be checked for proper names");
}
#[test]
fn test_reference_link_text_flagged_but_definition_not() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
let content = r#"Check the [javascript guide][js-ref] for details.
Real javascript should be flagged.
[js-ref]: https://javascript.info/typescript/guide"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in link text + 1 standalone");
assert!(result.iter().any(|w| w.line == 1 && w.message.contains("'javascript'")));
assert!(result.iter().any(|w| w.line == 3 && w.message.contains("'javascript'")));
}
#[test]
fn test_reference_definitions_not_flagged() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = r#"[js-ref]: https://javascript.info/guide"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Reference definitions should not be checked");
}
#[test]
fn test_wikilinks_text_is_flagged() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
let content = r#"[[javascript]]
Regular javascript here.
[[JavaScript|display text]]"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in WikiLink + 1 standalone");
assert!(
result
.iter()
.any(|w| w.line == 1 && w.column == 3 && w.message.contains("'javascript'"))
);
assert!(result.iter().any(|w| w.line == 3 && w.message.contains("'javascript'")));
}
#[test]
fn test_url_link_text_not_flagged() {
let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
let content = r#"[https://github.com/org/repo](https://github.com/org/repo)
[http://github.com/org/repo](http://github.com/org/repo)
[www.github.com/org/repo](https://www.github.com/org/repo)"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"URL-like link text should not be flagged, got: {result:?}"
);
}
#[test]
fn test_url_link_text_with_leading_space_not_flagged() {
let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
let content = r#"[ https://github.com/org/repo](https://github.com/org/repo)"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"URL-like link text with leading space should not be flagged, got: {result:?}"
);
}
#[test]
fn test_url_link_text_uppercase_scheme_not_flagged() {
let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
let content = r#"[HTTPS://GITHUB.COM/org/repo](https://github.com/org/repo)"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"URL-like link text with uppercase scheme should not be flagged, got: {result:?}"
);
}
#[test]
fn test_non_url_link_text_still_flagged() {
let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
let content = r#"[github.com/org/repo](https://github.com/org/repo)
[Visit github](https://github.com/org/repo)
[//github.com/org/repo](//github.com/org/repo)
[ftp://github.com/org/repo](ftp://github.com/org/repo)"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 4, "Non-URL link text should be flagged, got: {result:?}");
assert!(result.iter().any(|w| w.line == 1)); assert!(result.iter().any(|w| w.line == 3)); assert!(result.iter().any(|w| w.line == 5)); assert!(result.iter().any(|w| w.line == 7)); }
#[test]
fn test_url_link_text_fix_not_applied() {
let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
let content = "[https://github.com/org/repo](https://github.com/org/repo)\n";
let ctx = create_context(content);
let result = rule.fix(&ctx).unwrap();
assert_eq!(result, content, "Fix should not modify URL-like link text");
}
#[test]
fn test_mixed_url_and_regular_link_text() {
let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
let content = r#"[https://github.com/org/repo](https://github.com/org/repo)
Visit [github documentation](https://github.com/docs) for details.
[www.github.com/pricing](https://www.github.com/pricing)"#;
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Only non-URL link text should be flagged, got: {result:?}"
);
assert_eq!(result[0].line, 3);
}
#[test]
fn test_html_attribute_values_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "# Heading\n\ntest\n\n<img src=\"www.example.test/test_image.png\">\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
let line5_violations: Vec<_> = result.iter().filter(|w| w.line == 5).collect();
assert!(
line5_violations.is_empty(),
"Should not flag anything inside HTML tag attributes: {line5_violations:?}"
);
let line3_violations: Vec<_> = result.iter().filter(|w| w.line == 3).collect();
assert_eq!(line3_violations.len(), 1, "Plain 'test' on line 3 should be flagged");
}
#[test]
fn test_html_text_content_still_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "# Heading\n\n<a href=\"https://example.test/page\">test link</a>\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should flag only 'test' in anchor text, not in href: {result:?}"
);
assert_eq!(result[0].column, 37, "Should flag col 37 ('test link' in anchor text)");
}
#[test]
fn test_html_attribute_various_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = concat!(
"# Heading\n\n",
"<img src=\"test.png\" alt=\"test image\">\n",
"<span class=\"test-class\" data-test=\"value\">test content</span>\n",
);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should flag only 'test content' between tags: {result:?}"
);
assert_eq!(result[0].line, 4);
}
#[test]
fn test_plain_text_underscore_boundary_unchanged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "# Heading\n\ntest_image is here and just_test ends here\n";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
2,
"Should flag 'test' in both 'test_image' and 'just_test': {result:?}"
);
let cols: Vec<usize> = result.iter().map(|w| w.column).collect();
assert!(cols.contains(&1), "Should flag col 1 (test_image): {cols:?}");
assert!(cols.contains(&29), "Should flag col 29 (just_test): {cols:?}");
}
#[test]
fn test_frontmatter_yaml_keys_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntitle: Heading\ntest: Some Test value\n---\n\nTest\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag YAML keys or correctly capitalized values: {result:?}"
);
}
#[test]
fn test_frontmatter_yaml_values_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntitle: Heading\nkey: a test value\n---\n\nTest\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should flag 'test' in YAML value: {result:?}");
assert_eq!(result[0].line, 3);
assert_eq!(result[0].column, 8); }
#[test]
fn test_frontmatter_key_matches_name_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntest: other value\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag YAML key that matches configured name: {result:?}"
);
}
#[test]
fn test_frontmatter_empty_value_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntest:\ntest: \n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag YAML keys with empty values: {result:?}"
);
}
#[test]
fn test_frontmatter_nested_yaml_key_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\nparent:\n test: nested value\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Should not flag nested YAML keys: {result:?}");
}
#[test]
fn test_frontmatter_list_items_checked() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntags:\n - test\n - other\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should flag 'test' in YAML list item: {result:?}");
assert_eq!(result[0].line, 3);
}
#[test]
fn test_frontmatter_value_with_multiple_colons() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntest: description: a test thing\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should flag 'test' in value after first colon: {result:?}"
);
assert_eq!(result[0].line, 2);
assert!(result[0].column > 6, "Violation column should be in value portion");
}
#[test]
fn test_frontmatter_does_not_affect_body() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntitle: Heading\n---\n\ntest should be flagged here\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should flag 'test' in body text: {result:?}");
assert_eq!(result[0].line, 5);
}
#[test]
fn test_frontmatter_fix_corrects_values_preserves_keys() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntest: a test value\n---\n\ntest here\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(fixed, "---\ntest: a Test value\n---\n\nTest here\n");
}
#[test]
fn test_frontmatter_multiword_value_flagged() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
let content = "---\ndescription: Learn javascript and typescript\n---\n\nBody\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should flag both names in YAML value: {result:?}");
assert!(result.iter().all(|w| w.line == 2));
}
#[test]
fn test_frontmatter_yaml_comments_not_checked() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\n# test comment\ntitle: Heading\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Should not flag names in YAML comments: {result:?}");
}
#[test]
fn test_frontmatter_delimiters_not_checked() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntitle: Heading\n---\n\ntest here\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should only flag body text: {result:?}");
assert_eq!(result[0].line, 5);
}
#[test]
fn test_frontmatter_continuation_lines_checked() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ndescription: >\n a test value\n continued here\n---\n\nBody\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should flag 'test' in continuation line: {result:?}");
assert_eq!(result[0].line, 3);
}
#[test]
fn test_frontmatter_quoted_values_checked() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntitle: \"a test title\"\n---\n\nBody\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should flag 'test' in quoted YAML value: {result:?}");
assert_eq!(result[0].line, 2);
}
#[test]
fn test_frontmatter_single_quoted_values_checked() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntitle: 'a test title'\n---\n\nBody\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should flag 'test' in single-quoted YAML value: {result:?}"
);
assert_eq!(result[0].line, 2);
}
#[test]
fn test_frontmatter_fix_multiword_values() {
let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
let content = "---\ndescription: Learn javascript and typescript\n---\n\nBody\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(
fixed,
"---\ndescription: Learn JavaScript and TypeScript\n---\n\nBody\n"
);
}
#[test]
fn test_frontmatter_fix_preserves_yaml_structure() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntags:\n - test\n - other\ntitle: a test doc\n---\n\ntest body\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(
fixed,
"---\ntags:\n - Test\n - other\ntitle: a Test doc\n---\n\nTest body\n"
);
}
#[test]
fn test_frontmatter_toml_delimiters_not_checked() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "+++\ntitle = \"a test title\"\n+++\n\ntest body\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should flag TOML value and body: {result:?}");
let fm_violations: Vec<_> = result.iter().filter(|w| w.line == 2).collect();
assert_eq!(fm_violations.len(), 1, "Should flag 'test' in TOML value: {result:?}");
let body_violations: Vec<_> = result.iter().filter(|w| w.line == 5).collect();
assert_eq!(body_violations.len(), 1, "Should flag body 'test': {result:?}");
}
#[test]
fn test_frontmatter_toml_key_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "+++\ntest = \"other value\"\n+++\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag TOML key that matches configured name: {result:?}"
);
}
#[test]
fn test_frontmatter_toml_fix_preserves_keys() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "+++\ntest = \"a test value\"\n+++\n\ntest here\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(fixed, "+++\ntest = \"a Test value\"\n+++\n\nTest here\n");
}
#[test]
fn test_frontmatter_list_item_mapping_key_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\nitems:\n - test: nested value\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag YAML key in list-item mapping: {result:?}"
);
}
#[test]
fn test_frontmatter_list_item_mapping_value_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\nitems:\n - key: a test value\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should flag 'test' in list-item mapping value: {result:?}"
);
assert_eq!(result[0].line, 3);
}
#[test]
fn test_frontmatter_bare_list_item_still_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\ntags:\n - test\n - other\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should flag 'test' in bare list item: {result:?}");
assert_eq!(result[0].line, 3);
}
#[test]
fn test_frontmatter_flow_mapping_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\nflow_map: {test: value, other: test}\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag names inside flow mappings: {result:?}"
);
}
#[test]
fn test_frontmatter_flow_sequence_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\nitems: [test, other, test]\n---\n\nBody text\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag names inside flow sequences: {result:?}"
);
}
#[test]
fn test_frontmatter_list_item_mapping_fix_preserves_key() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "---\nitems:\n - test: a test value\n---\n\ntest here\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(fixed, "---\nitems:\n - test: a Test value\n---\n\nTest here\n");
}
#[test]
fn test_frontmatter_backtick_code_not_flagged() {
let config = MD044Config {
names: vec!["GoodApplication".to_string()],
code_blocks: false,
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "---\ntitle: \"`goodapplication` CLI\"\n---\n\nIntroductory `goodapplication` CLI text.\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag names inside backticks in frontmatter or body: {result:?}"
);
}
#[test]
fn test_frontmatter_unquoted_backtick_code_not_flagged() {
let config = MD044Config {
names: vec!["GoodApplication".to_string()],
code_blocks: false,
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "---\ntitle: `goodapplication` CLI\n---\n\nIntroductory `goodapplication` CLI text.\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag names inside backticks in unquoted YAML frontmatter: {result:?}"
);
}
#[test]
fn test_frontmatter_bare_name_still_flagged_with_backtick_nearby() {
let config = MD044Config {
names: vec!["GoodApplication".to_string()],
code_blocks: false,
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "---\ntitle: goodapplication `goodapplication` CLI\n---\n\nBody\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should flag bare name but not backtick-wrapped name: {result:?}"
);
assert_eq!(result[0].line, 2);
assert_eq!(result[0].column, 8); }
#[test]
fn test_frontmatter_backtick_code_with_code_blocks_true() {
let config = MD044Config {
names: vec!["GoodApplication".to_string()],
code_blocks: true,
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "---\ntitle: \"`goodapplication` CLI\"\n---\n\nBody\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should flag backtick-wrapped name when code_blocks=true: {result:?}"
);
assert_eq!(result[0].line, 2);
}
#[test]
fn test_frontmatter_fix_preserves_backtick_code() {
let config = MD044Config {
names: vec!["GoodApplication".to_string()],
code_blocks: false,
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "---\ntitle: \"`goodapplication` CLI\"\n---\n\nIntroductory `goodapplication` CLI text.\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(
fixed, content,
"Fix should not modify names inside backticks in frontmatter"
);
}
#[test]
fn test_angle_bracket_url_in_html_comment_not_flagged() {
let config = MD044Config {
names: vec!["Test".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "---\ntitle: Level 1 heading\n---\n\n<https://www.example.test>\n\n<!-- This is a Test https://www.example.test -->\n<!-- This is a Test <https://www.example.test> -->\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
let line8_warnings: Vec<_> = result.iter().filter(|w| w.line == 8).collect();
assert!(
line8_warnings.is_empty(),
"Should not flag names inside angle-bracket URLs in HTML comments: {line8_warnings:?}"
);
}
#[test]
fn test_bare_url_in_html_comment_still_flagged() {
let config = MD044Config {
names: vec!["Test".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "<!-- This is a test https://www.example.test -->\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
!result.is_empty(),
"Should flag 'test' in prose text of HTML comment with bare URL"
);
}
#[test]
fn test_angle_bracket_url_in_regular_markdown_not_flagged() {
let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
let content = "<https://www.example.test>\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag names inside angle-bracket URLs in regular markdown: {result:?}"
);
}
#[test]
fn test_multiple_angle_bracket_urls_in_one_comment() {
let config = MD044Config {
names: vec!["Test".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "<!-- See <https://test.example.com> and <https://www.example.test> for details -->\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag names inside multiple angle-bracket URLs: {result:?}"
);
}
#[test]
fn test_angle_bracket_non_url_still_flagged() {
assert!(
!MD044ProperNames::is_in_angle_bracket_url("<test> which is not a URL.", 1),
"is_in_angle_bracket_url should return false for non-URL angle brackets"
);
}
#[test]
fn test_angle_bracket_mailto_url_not_flagged() {
let config = MD044Config {
names: vec!["Test".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "<!-- Contact <mailto:test@example.com> for help -->\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag names inside angle-bracket mailto URLs: {result:?}"
);
}
#[test]
fn test_angle_bracket_ftp_url_not_flagged() {
let config = MD044Config {
names: vec!["Test".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "<!-- Download from <ftp://test.example.com/file> -->\n";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not flag names inside angle-bracket FTP URLs: {result:?}"
);
}
#[test]
fn test_angle_bracket_url_fix_preserves_url() {
let config = MD044Config {
names: vec!["Test".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "<!-- test text <https://www.example.test> -->\n";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert!(
fixed.contains("<https://www.example.test>"),
"Fix should preserve angle-bracket URLs: {fixed}"
);
assert!(
fixed.contains("Test text"),
"Fix should correct prose 'test' to 'Test': {fixed}"
);
}
#[test]
fn test_is_in_angle_bracket_url_helper() {
let line = "text <https://example.test> more text";
assert!(MD044ProperNames::is_in_angle_bracket_url(line, 5)); assert!(MD044ProperNames::is_in_angle_bracket_url(line, 6)); assert!(MD044ProperNames::is_in_angle_bracket_url(line, 15)); assert!(MD044ProperNames::is_in_angle_bracket_url(line, 26));
assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 0)); assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 4)); assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 27));
assert!(!MD044ProperNames::is_in_angle_bracket_url("<notaurl>", 1));
assert!(MD044ProperNames::is_in_angle_bracket_url(
"<mailto:test@example.com>",
10
));
assert!(MD044ProperNames::is_in_angle_bracket_url(
"<ftp://test.example.com>",
10
));
}
#[test]
fn test_is_in_angle_bracket_url_uppercase_scheme() {
assert!(MD044ProperNames::is_in_angle_bracket_url(
"<HTTPS://test.example.com>",
10
));
assert!(MD044ProperNames::is_in_angle_bracket_url(
"<Http://test.example.com>",
10
));
}
#[test]
fn test_is_in_angle_bracket_url_uncommon_schemes() {
assert!(MD044ProperNames::is_in_angle_bracket_url(
"<ssh://test@example.com>",
10
));
assert!(MD044ProperNames::is_in_angle_bracket_url("<file:///test/path>", 10));
assert!(MD044ProperNames::is_in_angle_bracket_url("<data:text/plain;test>", 10));
}
#[test]
fn test_is_in_angle_bracket_url_unclosed() {
assert!(!MD044ProperNames::is_in_angle_bracket_url(
"<https://test.example.com",
10
));
}
#[test]
fn test_vale_inline_config_comments_not_flagged() {
let config = MD044Config {
names: vec!["Vale".to_string(), "JavaScript".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "\
<!-- vale off -->
Some javascript text here.
<!-- vale on -->
<!-- vale Style.Rule = NO -->
More javascript text.
<!-- vale Style.Rule = YES -->
<!-- vale JavaScript.Grammar = NO -->
";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should only flag body lines, not Vale config comments");
assert_eq!(result[0].line, 2);
assert_eq!(result[1].line, 5);
}
#[test]
fn test_remark_lint_inline_config_comments_not_flagged() {
let config = MD044Config {
names: vec!["JavaScript".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "\
<!-- lint disable remark-lint-some-rule -->
Some javascript text here.
<!-- lint enable remark-lint-some-rule -->
<!-- lint ignore remark-lint-some-rule -->
More javascript text.
";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
2,
"Should only flag body lines, not remark-lint config comments"
);
assert_eq!(result[0].line, 2);
assert_eq!(result[1].line, 5);
}
#[test]
fn test_fix_does_not_modify_vale_remark_lint_comments() {
let config = MD044Config {
names: vec!["JavaScript".to_string(), "Vale".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "\
<!-- vale off -->
Some javascript text.
<!-- vale on -->
<!-- lint disable remark-lint-some-rule -->
More javascript text.
<!-- lint enable remark-lint-some-rule -->
";
let ctx = create_context(content);
let fixed = rule.fix(&ctx).unwrap();
assert!(fixed.contains("<!-- vale off -->"));
assert!(fixed.contains("<!-- vale on -->"));
assert!(fixed.contains("<!-- lint disable remark-lint-some-rule -->"));
assert!(fixed.contains("<!-- lint enable remark-lint-some-rule -->"));
assert!(fixed.contains("Some JavaScript text."));
assert!(fixed.contains("More JavaScript text."));
}
#[test]
fn test_mixed_tool_directives_all_skipped() {
let config = MD044Config {
names: vec!["JavaScript".to_string(), "Vale".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "\
<!-- rumdl-disable MD044 -->
Some javascript text.
<!-- markdownlint-disable -->
More javascript text.
<!-- vale off -->
Even more javascript text.
<!-- lint disable some-rule -->
Final javascript text.
<!-- rumdl-enable MD044 -->
<!-- markdownlint-enable -->
<!-- vale on -->
<!-- lint enable some-rule -->
";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
4,
"Should only flag body lines, not any tool directive comments"
);
assert_eq!(result[0].line, 2);
assert_eq!(result[1].line, 4);
assert_eq!(result[2].line, 6);
assert_eq!(result[3].line, 8);
}
#[test]
fn test_vale_remark_lint_edge_cases_not_matched() {
let config = MD044Config {
names: vec!["JavaScript".to_string(), "Vale".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "\
<!-- vale -->
<!-- vale is a tool for writing -->
<!-- valedictorian javascript -->
<!-- linting javascript tips -->
<!-- vale javascript -->
<!-- lint your javascript code -->
";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
7,
"Should flag proper names in non-directive HTML comments: got {result:?}"
);
assert_eq!(result[0].line, 1); assert_eq!(result[1].line, 2); assert_eq!(result[2].line, 3); assert_eq!(result[3].line, 4); assert_eq!(result[4].line, 5); assert_eq!(result[5].line, 5); assert_eq!(result[6].line, 6); }
#[test]
fn test_vale_style_directives_skipped() {
let config = MD044Config {
names: vec!["JavaScript".to_string(), "Vale".to_string()],
..MD044Config::default()
};
let rule = MD044ProperNames::from_config_struct(config);
let content = "\
<!-- vale style = MyStyle -->
<!-- vale styles = Style1, Style2 -->
<!-- vale MyRule.Name = YES -->
<!-- vale MyRule.Name = NO -->
Some javascript text.
";
let ctx = create_context(content);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only flag body lines, not Vale style/rule directives: got {result:?}"
);
assert_eq!(result[0].line, 5);
}
#[test]
fn test_backtick_code_single_backticks() {
let line = "hello `world` bye";
assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 7));
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 14));
}
#[test]
fn test_backtick_code_double_backticks() {
let line = "a ``code`` b";
assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 4));
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 11));
}
#[test]
fn test_backtick_code_unclosed() {
let line = "a `code b";
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 3));
}
#[test]
fn test_backtick_code_mismatched_count() {
let line = "a `code`` b";
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 3));
}
#[test]
fn test_backtick_code_multiple_spans() {
let line = "`first` and `second`";
assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 1));
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 8));
assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 13));
}
#[test]
fn test_backtick_code_on_backtick_boundary() {
let line = "`code`";
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 5));
assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 1));
assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 4));
}
}