1use crate::utils::fast_hash;
2use crate::utils::regex_cache::{escape_regex, get_cached_fancy_regex};
3
4use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, Severity};
5use std::collections::{HashMap, HashSet};
6use std::sync::{Arc, Mutex};
7
8mod md044_config;
9pub use md044_config::MD044Config;
10
11type WarningPosition = (usize, usize, String); fn is_inline_config_comment(trimmed: &str) -> bool {
70 trimmed.starts_with("<!-- rumdl-")
71 || trimmed.starts_with("<!-- markdownlint-")
72 || trimmed.starts_with("<!-- vale off")
73 || trimmed.starts_with("<!-- vale on")
74 || (trimmed.starts_with("<!-- vale ") && trimmed.contains(" = "))
75 || trimmed.starts_with("<!-- vale style")
76 || trimmed.starts_with("<!-- lint disable ")
77 || trimmed.starts_with("<!-- lint enable ")
78 || trimmed.starts_with("<!-- lint ignore ")
79}
80
81#[derive(Clone)]
82pub struct MD044ProperNames {
83 config: MD044Config,
84 combined_pattern: Option<String>,
86 name_variants: Vec<String>,
88 content_cache: Arc<Mutex<HashMap<u64, Vec<WarningPosition>>>>,
90}
91
92impl MD044ProperNames {
93 pub fn new(names: Vec<String>, code_blocks: bool) -> Self {
94 let config = MD044Config {
95 names,
96 code_blocks,
97 html_elements: true, html_comments: true, };
100 let combined_pattern = Self::create_combined_pattern(&config);
101 let name_variants = Self::build_name_variants(&config);
102 Self {
103 config,
104 combined_pattern,
105 name_variants,
106 content_cache: Arc::new(Mutex::new(HashMap::new())),
107 }
108 }
109
110 fn ascii_normalize(s: &str) -> String {
112 s.replace(['é', 'è', 'ê', 'ë'], "e")
113 .replace(['à', 'á', 'â', 'ä', 'ã', 'å'], "a")
114 .replace(['ï', 'î', 'í', 'ì'], "i")
115 .replace(['ü', 'ú', 'ù', 'û'], "u")
116 .replace(['ö', 'ó', 'ò', 'ô', 'õ'], "o")
117 .replace('ñ', "n")
118 .replace('ç', "c")
119 }
120
121 pub fn from_config_struct(config: MD044Config) -> Self {
122 let combined_pattern = Self::create_combined_pattern(&config);
123 let name_variants = Self::build_name_variants(&config);
124 Self {
125 config,
126 combined_pattern,
127 name_variants,
128 content_cache: Arc::new(Mutex::new(HashMap::new())),
129 }
130 }
131
132 fn create_combined_pattern(config: &MD044Config) -> Option<String> {
134 if config.names.is_empty() {
135 return None;
136 }
137
138 let mut patterns: Vec<String> = config
140 .names
141 .iter()
142 .flat_map(|name| {
143 let mut variations = vec![];
144 let lower_name = name.to_lowercase();
145
146 variations.push(escape_regex(&lower_name));
148
149 let lower_name_no_dots = lower_name.replace('.', "");
151 if lower_name != lower_name_no_dots {
152 variations.push(escape_regex(&lower_name_no_dots));
153 }
154
155 let ascii_normalized = Self::ascii_normalize(&lower_name);
157
158 if ascii_normalized != lower_name {
159 variations.push(escape_regex(&ascii_normalized));
160
161 let ascii_no_dots = ascii_normalized.replace('.', "");
163 if ascii_normalized != ascii_no_dots {
164 variations.push(escape_regex(&ascii_no_dots));
165 }
166 }
167
168 variations
169 })
170 .collect();
171
172 patterns.sort_by_key(|b| std::cmp::Reverse(b.len()));
174
175 Some(format!(r"(?i)({})", patterns.join("|")))
178 }
179
180 fn build_name_variants(config: &MD044Config) -> Vec<String> {
181 let mut variants = HashSet::new();
182 for name in &config.names {
183 let lower_name = name.to_lowercase();
184 variants.insert(lower_name.clone());
185
186 let lower_no_dots = lower_name.replace('.', "");
187 if lower_name != lower_no_dots {
188 variants.insert(lower_no_dots);
189 }
190
191 let ascii_normalized = Self::ascii_normalize(&lower_name);
192 if ascii_normalized != lower_name {
193 variants.insert(ascii_normalized.clone());
194
195 let ascii_no_dots = ascii_normalized.replace('.', "");
196 if ascii_normalized != ascii_no_dots {
197 variants.insert(ascii_no_dots);
198 }
199 }
200 }
201
202 variants.into_iter().collect()
203 }
204
205 fn find_name_violations(
208 &self,
209 content: &str,
210 ctx: &crate::lint_context::LintContext,
211 content_lower: &str,
212 ) -> Vec<WarningPosition> {
213 if self.config.names.is_empty() || content.is_empty() || self.combined_pattern.is_none() {
215 return Vec::new();
216 }
217
218 let has_potential_matches = self.name_variants.iter().any(|name| content_lower.contains(name));
220
221 if !has_potential_matches {
222 return Vec::new();
223 }
224
225 let hash = fast_hash(content);
227 {
228 if let Ok(cache) = self.content_cache.lock()
230 && let Some(cached) = cache.get(&hash)
231 {
232 return cached.clone();
233 }
234 }
235
236 let mut violations = Vec::new();
237
238 let combined_regex = match &self.combined_pattern {
240 Some(pattern) => match get_cached_fancy_regex(pattern) {
241 Ok(regex) => regex,
242 Err(_) => return Vec::new(),
243 },
244 None => return Vec::new(),
245 };
246
247 for (line_idx, line_info) in ctx.lines.iter().enumerate() {
249 let line_num = line_idx + 1;
250 let line = line_info.content(ctx.content);
251
252 let trimmed = line.trim_start();
254 if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
255 continue;
256 }
257
258 if !self.config.code_blocks && line_info.in_code_block {
260 continue;
261 }
262
263 if !self.config.html_elements && line_info.in_html_block {
265 continue;
266 }
267
268 if !self.config.html_comments && line_info.in_html_comment {
270 continue;
271 }
272
273 if line_info.in_jsx_expression || line_info.in_mdx_comment {
275 continue;
276 }
277
278 if line_info.in_obsidian_comment {
280 continue;
281 }
282
283 let fm_value_offset = if line_info.in_front_matter {
286 Self::frontmatter_value_offset(line)
287 } else {
288 0
289 };
290 if fm_value_offset == usize::MAX {
291 continue;
292 }
293
294 if is_inline_config_comment(trimmed) {
296 continue;
297 }
298
299 let line_lower = line.to_lowercase();
301 let has_line_matches = self.name_variants.iter().any(|name| line_lower.contains(name));
302
303 if !has_line_matches {
304 continue;
305 }
306
307 for cap_result in combined_regex.find_iter(line) {
309 match cap_result {
310 Ok(cap) => {
311 let found_name = &line[cap.start()..cap.end()];
312
313 let start_pos = cap.start();
315 let end_pos = cap.end();
316
317 if start_pos < fm_value_offset {
319 continue;
320 }
321
322 let byte_pos = line_info.byte_offset + start_pos;
324 if ctx.is_in_html_tag(byte_pos) {
325 continue;
326 }
327
328 if !Self::is_at_word_boundary(line, start_pos, true)
329 || !Self::is_at_word_boundary(line, end_pos, false)
330 {
331 continue; }
333
334 if !self.config.code_blocks {
336 if ctx.is_in_code_block_or_span(byte_pos) {
337 continue;
338 }
339 if (line_info.in_html_comment || line_info.in_html_block)
343 && Self::is_in_backtick_code_in_line(line, start_pos)
344 {
345 continue;
346 }
347 }
348
349 if Self::is_in_link(ctx, byte_pos) {
351 continue;
352 }
353
354 if Self::is_in_angle_bracket_url(line, start_pos) {
358 continue;
359 }
360
361 if let Some(proper_name) = self.get_proper_name_for(found_name) {
363 if found_name != proper_name {
365 violations.push((line_num, cap.start() + 1, found_name.to_string()));
366 }
367 }
368 }
369 Err(e) => {
370 eprintln!("Regex execution error on line {line_num}: {e}");
371 }
372 }
373 }
374 }
375
376 if let Ok(mut cache) = self.content_cache.lock() {
378 cache.insert(hash, violations.clone());
379 }
380 violations
381 }
382
383 fn is_in_link(ctx: &crate::lint_context::LintContext, byte_pos: usize) -> bool {
390 use pulldown_cmark::LinkType;
391
392 let link_idx = ctx.links.partition_point(|link| link.byte_offset <= byte_pos);
394 if link_idx > 0 {
395 let link = &ctx.links[link_idx - 1];
396 if byte_pos < link.byte_end {
397 let text_start = if matches!(link.link_type, LinkType::WikiLink { .. }) {
399 link.byte_offset + 2
400 } else {
401 link.byte_offset + 1
402 };
403 let text_end = text_start + link.text.len();
404
405 if byte_pos >= text_start && byte_pos < text_end {
407 return Self::link_text_is_url(&link.text);
408 }
409 return true;
411 }
412 }
413
414 let image_idx = ctx.images.partition_point(|img| img.byte_offset <= byte_pos);
416 if image_idx > 0 {
417 let image = &ctx.images[image_idx - 1];
418 if byte_pos < image.byte_end {
419 let alt_start = image.byte_offset + 2;
421 let alt_end = alt_start + image.alt_text.len();
422
423 if byte_pos >= alt_start && byte_pos < alt_end {
425 return false;
426 }
427 return true;
429 }
430 }
431
432 ctx.is_in_reference_def(byte_pos)
434 }
435
436 fn link_text_is_url(text: &str) -> bool {
439 let lower = text.trim().to_ascii_lowercase();
440 lower.starts_with("http://") || lower.starts_with("https://") || lower.starts_with("www.")
441 }
442
443 fn is_in_angle_bracket_url(line: &str, pos: usize) -> bool {
449 let bytes = line.as_bytes();
450 let len = bytes.len();
451 let mut i = 0;
452 while i < len {
453 if bytes[i] == b'<' {
454 let after_open = i + 1;
455 if after_open < len && bytes[after_open].is_ascii_alphabetic() {
459 let mut s = after_open + 1;
460 let scheme_max = (after_open + 32).min(len);
461 while s < scheme_max
462 && (bytes[s].is_ascii_alphanumeric()
463 || bytes[s] == b'+'
464 || bytes[s] == b'-'
465 || bytes[s] == b'.')
466 {
467 s += 1;
468 }
469 if s < len && bytes[s] == b':' {
470 let mut j = s + 1;
472 let mut found_close = false;
473 while j < len {
474 match bytes[j] {
475 b'>' => {
476 found_close = true;
477 break;
478 }
479 b' ' | b'<' => break,
480 _ => j += 1,
481 }
482 }
483 if found_close && pos >= i && pos <= j {
484 return true;
485 }
486 if found_close {
487 i = j + 1;
488 continue;
489 }
490 }
491 }
492 }
493 i += 1;
494 }
495 false
496 }
497
498 fn is_in_backtick_code_in_line(line: &str, pos: usize) -> bool {
506 let bytes = line.as_bytes();
507 let len = bytes.len();
508 let mut i = 0;
509 while i < len {
510 if bytes[i] == b'`' {
511 let open_start = i;
513 while i < len && bytes[i] == b'`' {
514 i += 1;
515 }
516 let tick_len = i - open_start;
517
518 while i < len {
520 if bytes[i] == b'`' {
521 let close_start = i;
522 while i < len && bytes[i] == b'`' {
523 i += 1;
524 }
525 if i - close_start == tick_len {
526 let content_start = open_start + tick_len;
530 let content_end = close_start;
531 if pos >= content_start && pos < content_end {
532 return true;
533 }
534 break;
536 }
537 } else {
539 i += 1;
540 }
541 }
542 } else {
543 i += 1;
544 }
545 }
546 false
547 }
548
549 fn is_word_boundary_char(c: char) -> bool {
551 !c.is_alphanumeric()
552 }
553
554 fn is_at_word_boundary(content: &str, pos: usize, is_start: bool) -> bool {
556 if is_start {
557 if pos == 0 {
558 return true;
559 }
560 match content[..pos].chars().next_back() {
561 None => true,
562 Some(c) => Self::is_word_boundary_char(c),
563 }
564 } else {
565 if pos >= content.len() {
566 return true;
567 }
568 match content[pos..].chars().next() {
569 None => true,
570 Some(c) => Self::is_word_boundary_char(c),
571 }
572 }
573 }
574
575 fn frontmatter_value_offset(line: &str) -> usize {
579 let trimmed = line.trim();
580
581 if trimmed == "---" || trimmed == "+++" || trimmed.is_empty() {
583 return usize::MAX;
584 }
585
586 if trimmed.starts_with('#') {
588 return usize::MAX;
589 }
590
591 let stripped = line.trim_start();
593 if let Some(after_dash) = stripped.strip_prefix("- ") {
594 let leading = line.len() - stripped.len();
595 if let Some(result) = Self::kv_value_offset(line, after_dash, leading + 2) {
597 return result;
598 }
599 return leading + 2;
601 }
602 if stripped == "-" {
603 return usize::MAX;
604 }
605
606 if let Some(result) = Self::kv_value_offset(line, stripped, line.len() - stripped.len()) {
608 return result;
609 }
610
611 if let Some(eq_pos) = line.find('=') {
613 let after_eq = eq_pos + 1;
614 if after_eq < line.len() && line.as_bytes()[after_eq] == b' ' {
615 let value_start = after_eq + 1;
616 let value_slice = &line[value_start..];
617 let value_trimmed = value_slice.trim();
618 if value_trimmed.is_empty() {
619 return usize::MAX;
620 }
621 if (value_trimmed.starts_with('"') && value_trimmed.ends_with('"'))
623 || (value_trimmed.starts_with('\'') && value_trimmed.ends_with('\''))
624 {
625 let quote_offset = value_slice.find(['"', '\'']).unwrap_or(0);
626 return value_start + quote_offset + 1;
627 }
628 return value_start;
629 }
630 return usize::MAX;
632 }
633
634 0
636 }
637
638 fn kv_value_offset(line: &str, content: &str, base_offset: usize) -> Option<usize> {
642 let colon_pos = content.find(':')?;
643 let abs_colon = base_offset + colon_pos;
644 let after_colon = abs_colon + 1;
645 if after_colon < line.len() && line.as_bytes()[after_colon] == b' ' {
646 let value_start = after_colon + 1;
647 let value_slice = &line[value_start..];
648 let value_trimmed = value_slice.trim();
649 if value_trimmed.is_empty() {
650 return Some(usize::MAX);
651 }
652 if value_trimmed.starts_with('{') || value_trimmed.starts_with('[') {
654 return Some(usize::MAX);
655 }
656 if (value_trimmed.starts_with('"') && value_trimmed.ends_with('"'))
658 || (value_trimmed.starts_with('\'') && value_trimmed.ends_with('\''))
659 {
660 let quote_offset = value_slice.find(['"', '\'']).unwrap_or(0);
661 return Some(value_start + quote_offset + 1);
662 }
663 return Some(value_start);
664 }
665 Some(usize::MAX)
667 }
668
669 fn get_proper_name_for(&self, found_name: &str) -> Option<String> {
671 let found_lower = found_name.to_lowercase();
672
673 for name in &self.config.names {
675 let lower_name = name.to_lowercase();
676 let lower_name_no_dots = lower_name.replace('.', "");
677
678 if found_lower == lower_name || found_lower == lower_name_no_dots {
680 return Some(name.clone());
681 }
682
683 let ascii_normalized = Self::ascii_normalize(&lower_name);
685
686 let ascii_no_dots = ascii_normalized.replace('.', "");
687
688 if found_lower == ascii_normalized || found_lower == ascii_no_dots {
689 return Some(name.clone());
690 }
691 }
692 None
693 }
694}
695
696impl Rule for MD044ProperNames {
697 fn name(&self) -> &'static str {
698 "MD044"
699 }
700
701 fn description(&self) -> &'static str {
702 "Proper names should have the correct capitalization"
703 }
704
705 fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
706 if self.config.names.is_empty() {
707 return true;
708 }
709 let content_lower = if ctx.content.is_ascii() {
711 ctx.content.to_ascii_lowercase()
712 } else {
713 ctx.content.to_lowercase()
714 };
715 !self.name_variants.iter().any(|name| content_lower.contains(name))
716 }
717
718 fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
719 let content = ctx.content;
720 if content.is_empty() || self.config.names.is_empty() || self.combined_pattern.is_none() {
721 return Ok(Vec::new());
722 }
723
724 let content_lower = if content.is_ascii() {
726 content.to_ascii_lowercase()
727 } else {
728 content.to_lowercase()
729 };
730
731 let has_potential_matches = self.name_variants.iter().any(|name| content_lower.contains(name));
733
734 if !has_potential_matches {
735 return Ok(Vec::new());
736 }
737
738 let line_index = &ctx.line_index;
739 let violations = self.find_name_violations(content, ctx, &content_lower);
740
741 let warnings = violations
742 .into_iter()
743 .filter_map(|(line, column, found_name)| {
744 self.get_proper_name_for(&found_name).map(|proper_name| LintWarning {
745 rule_name: Some(self.name().to_string()),
746 line,
747 column,
748 end_line: line,
749 end_column: column + found_name.len(),
750 message: format!("Proper name '{found_name}' should be '{proper_name}'"),
751 severity: Severity::Warning,
752 fix: Some(Fix {
753 range: line_index.line_col_to_byte_range(line, column),
754 replacement: proper_name,
755 }),
756 })
757 })
758 .collect();
759
760 Ok(warnings)
761 }
762
763 fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
764 let content = ctx.content;
765 if content.is_empty() || self.config.names.is_empty() {
766 return Ok(content.to_string());
767 }
768
769 let content_lower = if content.is_ascii() {
770 content.to_ascii_lowercase()
771 } else {
772 content.to_lowercase()
773 };
774 let violations = self.find_name_violations(content, ctx, &content_lower);
775 if violations.is_empty() {
776 return Ok(content.to_string());
777 }
778
779 let mut fixed_lines = Vec::new();
781
782 let mut violations_by_line: HashMap<usize, Vec<(usize, String)>> = HashMap::new();
784 for (line_num, col_num, found_name) in violations {
785 violations_by_line
786 .entry(line_num)
787 .or_default()
788 .push((col_num, found_name));
789 }
790
791 for violations in violations_by_line.values_mut() {
793 violations.sort_by_key(|b| std::cmp::Reverse(b.0));
794 }
795
796 for (line_idx, line_info) in ctx.lines.iter().enumerate() {
798 let line_num = line_idx + 1;
799
800 if let Some(line_violations) = violations_by_line.get(&line_num) {
801 let mut fixed_line = line_info.content(ctx.content).to_string();
803
804 for (col_num, found_name) in line_violations {
805 if let Some(proper_name) = self.get_proper_name_for(found_name) {
806 let start_col = col_num - 1; let end_col = start_col + found_name.len();
808
809 if end_col <= fixed_line.len()
810 && fixed_line.is_char_boundary(start_col)
811 && fixed_line.is_char_boundary(end_col)
812 {
813 fixed_line.replace_range(start_col..end_col, &proper_name);
814 }
815 }
816 }
817
818 fixed_lines.push(fixed_line);
819 } else {
820 fixed_lines.push(line_info.content(ctx.content).to_string());
822 }
823 }
824
825 let mut result = fixed_lines.join("\n");
827 if content.ends_with('\n') && !result.ends_with('\n') {
828 result.push('\n');
829 }
830 Ok(result)
831 }
832
833 fn as_any(&self) -> &dyn std::any::Any {
834 self
835 }
836
837 fn default_config_section(&self) -> Option<(String, toml::Value)> {
838 let json_value = serde_json::to_value(&self.config).ok()?;
839 Some((
840 self.name().to_string(),
841 crate::rule_config_serde::json_to_toml_value(&json_value)?,
842 ))
843 }
844
845 fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
846 where
847 Self: Sized,
848 {
849 let rule_config = crate::rule_config_serde::load_rule_config::<MD044Config>(config);
850 Box::new(Self::from_config_struct(rule_config))
851 }
852}
853
854#[cfg(test)]
855mod tests {
856 use super::*;
857 use crate::lint_context::LintContext;
858
859 fn create_context(content: &str) -> LintContext<'_> {
860 LintContext::new(content, crate::config::MarkdownFlavor::Standard, None)
861 }
862
863 #[test]
864 fn test_correctly_capitalized_names() {
865 let rule = MD044ProperNames::new(
866 vec![
867 "JavaScript".to_string(),
868 "TypeScript".to_string(),
869 "Node.js".to_string(),
870 ],
871 true,
872 );
873
874 let content = "This document uses JavaScript, TypeScript, and Node.js correctly.";
875 let ctx = create_context(content);
876 let result = rule.check(&ctx).unwrap();
877 assert!(result.is_empty(), "Should not flag correctly capitalized names");
878 }
879
880 #[test]
881 fn test_incorrectly_capitalized_names() {
882 let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
883
884 let content = "This document uses javascript and typescript incorrectly.";
885 let ctx = create_context(content);
886 let result = rule.check(&ctx).unwrap();
887
888 assert_eq!(result.len(), 2, "Should flag two incorrect capitalizations");
889 assert_eq!(result[0].message, "Proper name 'javascript' should be 'JavaScript'");
890 assert_eq!(result[0].line, 1);
891 assert_eq!(result[0].column, 20);
892 assert_eq!(result[1].message, "Proper name 'typescript' should be 'TypeScript'");
893 assert_eq!(result[1].line, 1);
894 assert_eq!(result[1].column, 35);
895 }
896
897 #[test]
898 fn test_names_at_beginning_of_sentences() {
899 let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "Python".to_string()], true);
900
901 let content = "javascript is a great language. python is also popular.";
902 let ctx = create_context(content);
903 let result = rule.check(&ctx).unwrap();
904
905 assert_eq!(result.len(), 2, "Should flag names at beginning of sentences");
906 assert_eq!(result[0].line, 1);
907 assert_eq!(result[0].column, 1);
908 assert_eq!(result[1].line, 1);
909 assert_eq!(result[1].column, 33);
910 }
911
912 #[test]
913 fn test_names_in_code_blocks_checked_by_default() {
914 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
915
916 let content = r#"Here is some text with JavaScript.
917
918```javascript
919// This javascript should be checked
920const lang = "javascript";
921```
922
923But this javascript should be flagged."#;
924
925 let ctx = create_context(content);
926 let result = rule.check(&ctx).unwrap();
927
928 assert_eq!(result.len(), 3, "Should flag javascript inside and outside code blocks");
929 assert_eq!(result[0].line, 4);
930 assert_eq!(result[1].line, 5);
931 assert_eq!(result[2].line, 8);
932 }
933
934 #[test]
935 fn test_names_in_code_blocks_ignored_when_disabled() {
936 let rule = MD044ProperNames::new(
937 vec!["JavaScript".to_string()],
938 false, );
940
941 let content = r#"```
942javascript in code block
943```"#;
944
945 let ctx = create_context(content);
946 let result = rule.check(&ctx).unwrap();
947
948 assert_eq!(
949 result.len(),
950 0,
951 "Should not flag javascript in code blocks when code_blocks is false"
952 );
953 }
954
955 #[test]
956 fn test_names_in_inline_code_checked_by_default() {
957 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
958
959 let content = "This is `javascript` in inline code and javascript outside.";
960 let ctx = create_context(content);
961 let result = rule.check(&ctx).unwrap();
962
963 assert_eq!(result.len(), 2, "Should flag javascript inside and outside inline code");
965 assert_eq!(result[0].column, 10); assert_eq!(result[1].column, 41); }
968
969 #[test]
970 fn test_multiple_names_in_same_line() {
971 let rule = MD044ProperNames::new(
972 vec!["JavaScript".to_string(), "TypeScript".to_string(), "React".to_string()],
973 true,
974 );
975
976 let content = "I use javascript, typescript, and react in my projects.";
977 let ctx = create_context(content);
978 let result = rule.check(&ctx).unwrap();
979
980 assert_eq!(result.len(), 3, "Should flag all three incorrect names");
981 assert_eq!(result[0].message, "Proper name 'javascript' should be 'JavaScript'");
982 assert_eq!(result[1].message, "Proper name 'typescript' should be 'TypeScript'");
983 assert_eq!(result[2].message, "Proper name 'react' should be 'React'");
984 }
985
986 #[test]
987 fn test_case_sensitivity() {
988 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
989
990 let content = "JAVASCRIPT, Javascript, javascript, and JavaScript variations.";
991 let ctx = create_context(content);
992 let result = rule.check(&ctx).unwrap();
993
994 assert_eq!(result.len(), 3, "Should flag all incorrect case variations");
995 assert!(result.iter().all(|w| w.message.contains("should be 'JavaScript'")));
997 }
998
999 #[test]
1000 fn test_configuration_with_custom_name_list() {
1001 let config = MD044Config {
1002 names: vec!["GitHub".to_string(), "GitLab".to_string(), "DevOps".to_string()],
1003 code_blocks: true,
1004 html_elements: true,
1005 html_comments: true,
1006 };
1007 let rule = MD044ProperNames::from_config_struct(config);
1008
1009 let content = "We use github, gitlab, and devops for our workflow.";
1010 let ctx = create_context(content);
1011 let result = rule.check(&ctx).unwrap();
1012
1013 assert_eq!(result.len(), 3, "Should flag all custom names");
1014 assert_eq!(result[0].message, "Proper name 'github' should be 'GitHub'");
1015 assert_eq!(result[1].message, "Proper name 'gitlab' should be 'GitLab'");
1016 assert_eq!(result[2].message, "Proper name 'devops' should be 'DevOps'");
1017 }
1018
1019 #[test]
1020 fn test_empty_configuration() {
1021 let rule = MD044ProperNames::new(vec![], true);
1022
1023 let content = "This has javascript and typescript but no configured names.";
1024 let ctx = create_context(content);
1025 let result = rule.check(&ctx).unwrap();
1026
1027 assert!(result.is_empty(), "Should not flag anything with empty configuration");
1028 }
1029
1030 #[test]
1031 fn test_names_with_special_characters() {
1032 let rule = MD044ProperNames::new(
1033 vec!["Node.js".to_string(), "ASP.NET".to_string(), "C++".to_string()],
1034 true,
1035 );
1036
1037 let content = "We use nodejs, asp.net, ASP.NET, and c++ in our stack.";
1038 let ctx = create_context(content);
1039 let result = rule.check(&ctx).unwrap();
1040
1041 assert_eq!(result.len(), 3, "Should handle special characters correctly");
1046
1047 let messages: Vec<&str> = result.iter().map(|w| w.message.as_str()).collect();
1048 assert!(messages.contains(&"Proper name 'nodejs' should be 'Node.js'"));
1049 assert!(messages.contains(&"Proper name 'asp.net' should be 'ASP.NET'"));
1050 assert!(messages.contains(&"Proper name 'c++' should be 'C++'"));
1051 }
1052
1053 #[test]
1054 fn test_word_boundaries() {
1055 let rule = MD044ProperNames::new(vec!["Java".to_string(), "Script".to_string()], true);
1056
1057 let content = "JavaScript is not java or script, but Java and Script are separate.";
1058 let ctx = create_context(content);
1059 let result = rule.check(&ctx).unwrap();
1060
1061 assert_eq!(result.len(), 2, "Should respect word boundaries");
1063 assert!(result.iter().any(|w| w.column == 19)); assert!(result.iter().any(|w| w.column == 27)); }
1066
1067 #[test]
1068 fn test_fix_method() {
1069 let rule = MD044ProperNames::new(
1070 vec![
1071 "JavaScript".to_string(),
1072 "TypeScript".to_string(),
1073 "Node.js".to_string(),
1074 ],
1075 true,
1076 );
1077
1078 let content = "I love javascript, typescript, and nodejs!";
1079 let ctx = create_context(content);
1080 let fixed = rule.fix(&ctx).unwrap();
1081
1082 assert_eq!(fixed, "I love JavaScript, TypeScript, and Node.js!");
1083 }
1084
1085 #[test]
1086 fn test_fix_multiple_occurrences() {
1087 let rule = MD044ProperNames::new(vec!["Python".to_string()], true);
1088
1089 let content = "python is great. I use python daily. PYTHON is powerful.";
1090 let ctx = create_context(content);
1091 let fixed = rule.fix(&ctx).unwrap();
1092
1093 assert_eq!(fixed, "Python is great. I use Python daily. Python is powerful.");
1094 }
1095
1096 #[test]
1097 fn test_fix_checks_code_blocks_by_default() {
1098 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1099
1100 let content = r#"I love javascript.
1101
1102```
1103const lang = "javascript";
1104```
1105
1106More javascript here."#;
1107
1108 let ctx = create_context(content);
1109 let fixed = rule.fix(&ctx).unwrap();
1110
1111 let expected = r#"I love JavaScript.
1112
1113```
1114const lang = "JavaScript";
1115```
1116
1117More JavaScript here."#;
1118
1119 assert_eq!(fixed, expected);
1120 }
1121
1122 #[test]
1123 fn test_multiline_content() {
1124 let rule = MD044ProperNames::new(vec!["Rust".to_string(), "Python".to_string()], true);
1125
1126 let content = r#"First line with rust.
1127Second line with python.
1128Third line with RUST and PYTHON."#;
1129
1130 let ctx = create_context(content);
1131 let result = rule.check(&ctx).unwrap();
1132
1133 assert_eq!(result.len(), 4, "Should flag all incorrect occurrences");
1134 assert_eq!(result[0].line, 1);
1135 assert_eq!(result[1].line, 2);
1136 assert_eq!(result[2].line, 3);
1137 assert_eq!(result[3].line, 3);
1138 }
1139
1140 #[test]
1141 fn test_default_config() {
1142 let config = MD044Config::default();
1143 assert!(config.names.is_empty());
1144 assert!(!config.code_blocks);
1145 assert!(config.html_elements);
1146 assert!(config.html_comments);
1147 }
1148
1149 #[test]
1150 fn test_default_config_checks_html_comments() {
1151 let config = MD044Config {
1152 names: vec!["JavaScript".to_string()],
1153 ..MD044Config::default()
1154 };
1155 let rule = MD044ProperNames::from_config_struct(config);
1156
1157 let content = "# Guide\n\n<!-- javascript mentioned here -->\n";
1158 let ctx = create_context(content);
1159 let result = rule.check(&ctx).unwrap();
1160
1161 assert_eq!(result.len(), 1, "Default config should check HTML comments");
1162 assert_eq!(result[0].line, 3);
1163 }
1164
1165 #[test]
1166 fn test_default_config_skips_code_blocks() {
1167 let config = MD044Config {
1168 names: vec!["JavaScript".to_string()],
1169 ..MD044Config::default()
1170 };
1171 let rule = MD044ProperNames::from_config_struct(config);
1172
1173 let content = "# Guide\n\n```\njavascript in code\n```\n";
1174 let ctx = create_context(content);
1175 let result = rule.check(&ctx).unwrap();
1176
1177 assert_eq!(result.len(), 0, "Default config should skip code blocks");
1178 }
1179
1180 #[test]
1181 fn test_standalone_html_comment_checked() {
1182 let config = MD044Config {
1183 names: vec!["Test".to_string()],
1184 ..MD044Config::default()
1185 };
1186 let rule = MD044ProperNames::from_config_struct(config);
1187
1188 let content = "# Heading\n\n<!-- this is a test example -->\n";
1189 let ctx = create_context(content);
1190 let result = rule.check(&ctx).unwrap();
1191
1192 assert_eq!(result.len(), 1, "Should flag proper name in standalone HTML comment");
1193 assert_eq!(result[0].line, 3);
1194 }
1195
1196 #[test]
1197 fn test_inline_config_comments_not_flagged() {
1198 let config = MD044Config {
1199 names: vec!["RUMDL".to_string()],
1200 ..MD044Config::default()
1201 };
1202 let rule = MD044ProperNames::from_config_struct(config);
1203
1204 let content = "<!-- rumdl-disable MD044 -->\nSome rumdl text here.\n<!-- rumdl-enable MD044 -->\n<!-- markdownlint-disable -->\nMore rumdl text.\n<!-- markdownlint-enable -->\n";
1208 let ctx = create_context(content);
1209 let result = rule.check(&ctx).unwrap();
1210
1211 assert_eq!(result.len(), 2, "Should only flag body lines, not config comments");
1212 assert_eq!(result[0].line, 2);
1213 assert_eq!(result[1].line, 5);
1214 }
1215
1216 #[test]
1217 fn test_html_comment_skipped_when_disabled() {
1218 let config = MD044Config {
1219 names: vec!["Test".to_string()],
1220 code_blocks: true,
1221 html_elements: true,
1222 html_comments: false,
1223 };
1224 let rule = MD044ProperNames::from_config_struct(config);
1225
1226 let content = "# Heading\n\n<!-- this is a test example -->\n\nRegular test here.\n";
1227 let ctx = create_context(content);
1228 let result = rule.check(&ctx).unwrap();
1229
1230 assert_eq!(
1231 result.len(),
1232 1,
1233 "Should only flag 'test' outside HTML comment when html_comments=false"
1234 );
1235 assert_eq!(result[0].line, 5);
1236 }
1237
1238 #[test]
1239 fn test_fix_corrects_html_comment_content() {
1240 let config = MD044Config {
1241 names: vec!["JavaScript".to_string()],
1242 ..MD044Config::default()
1243 };
1244 let rule = MD044ProperNames::from_config_struct(config);
1245
1246 let content = "# Guide\n\n<!-- javascript mentioned here -->\n";
1247 let ctx = create_context(content);
1248 let fixed = rule.fix(&ctx).unwrap();
1249
1250 assert_eq!(fixed, "# Guide\n\n<!-- JavaScript mentioned here -->\n");
1251 }
1252
1253 #[test]
1254 fn test_fix_does_not_modify_inline_config_comments() {
1255 let config = MD044Config {
1256 names: vec!["RUMDL".to_string()],
1257 ..MD044Config::default()
1258 };
1259 let rule = MD044ProperNames::from_config_struct(config);
1260
1261 let content = "<!-- rumdl-disable -->\nSome rumdl text.\n<!-- rumdl-enable -->\n";
1262 let ctx = create_context(content);
1263 let fixed = rule.fix(&ctx).unwrap();
1264
1265 assert!(fixed.contains("<!-- rumdl-disable -->"));
1267 assert!(fixed.contains("<!-- rumdl-enable -->"));
1268 assert!(fixed.contains("Some RUMDL text."));
1269 }
1270
1271 #[test]
1272 fn test_performance_with_many_names() {
1273 let mut names = vec![];
1274 for i in 0..50 {
1275 names.push(format!("ProperName{i}"));
1276 }
1277
1278 let rule = MD044ProperNames::new(names, true);
1279
1280 let content = "This has propername0, propername25, and propername49 incorrectly.";
1281 let ctx = create_context(content);
1282 let result = rule.check(&ctx).unwrap();
1283
1284 assert_eq!(result.len(), 3, "Should handle many configured names efficiently");
1285 }
1286
1287 #[test]
1288 fn test_large_name_count_performance() {
1289 let names = (0..1000).map(|i| format!("ProperName{i}")).collect::<Vec<_>>();
1292
1293 let rule = MD044ProperNames::new(names, true);
1294
1295 assert!(rule.combined_pattern.is_some());
1297
1298 let content = "This has propername0 and propername999 in it.";
1300 let ctx = create_context(content);
1301 let result = rule.check(&ctx).unwrap();
1302
1303 assert_eq!(result.len(), 2, "Should handle 1000 names without issues");
1305 }
1306
1307 #[test]
1308 fn test_cache_behavior() {
1309 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1310
1311 let content = "Using javascript here.";
1312 let ctx = create_context(content);
1313
1314 let result1 = rule.check(&ctx).unwrap();
1316 assert_eq!(result1.len(), 1);
1317
1318 let result2 = rule.check(&ctx).unwrap();
1320 assert_eq!(result2.len(), 1);
1321
1322 assert_eq!(result1[0].line, result2[0].line);
1324 assert_eq!(result1[0].column, result2[0].column);
1325 }
1326
1327 #[test]
1328 fn test_html_comments_not_checked_when_disabled() {
1329 let config = MD044Config {
1330 names: vec!["JavaScript".to_string()],
1331 code_blocks: true, html_elements: true, html_comments: false, };
1335 let rule = MD044ProperNames::from_config_struct(config);
1336
1337 let content = r#"Regular javascript here.
1338<!-- This javascript in HTML comment should be ignored -->
1339More javascript outside."#;
1340
1341 let ctx = create_context(content);
1342 let result = rule.check(&ctx).unwrap();
1343
1344 assert_eq!(result.len(), 2, "Should only flag javascript outside HTML comments");
1345 assert_eq!(result[0].line, 1);
1346 assert_eq!(result[1].line, 3);
1347 }
1348
1349 #[test]
1350 fn test_html_comments_checked_when_enabled() {
1351 let config = MD044Config {
1352 names: vec!["JavaScript".to_string()],
1353 code_blocks: true, html_elements: true, html_comments: true, };
1357 let rule = MD044ProperNames::from_config_struct(config);
1358
1359 let content = r#"Regular javascript here.
1360<!-- This javascript in HTML comment should be checked -->
1361More javascript outside."#;
1362
1363 let ctx = create_context(content);
1364 let result = rule.check(&ctx).unwrap();
1365
1366 assert_eq!(
1367 result.len(),
1368 3,
1369 "Should flag all javascript occurrences including in HTML comments"
1370 );
1371 }
1372
1373 #[test]
1374 fn test_multiline_html_comments() {
1375 let config = MD044Config {
1376 names: vec!["Python".to_string(), "JavaScript".to_string()],
1377 code_blocks: true, html_elements: true, html_comments: false, };
1381 let rule = MD044ProperNames::from_config_struct(config);
1382
1383 let content = r#"Regular python here.
1384<!--
1385This is a multiline comment
1386with javascript and python
1387that should be ignored
1388-->
1389More javascript outside."#;
1390
1391 let ctx = create_context(content);
1392 let result = rule.check(&ctx).unwrap();
1393
1394 assert_eq!(result.len(), 2, "Should only flag names outside HTML comments");
1395 assert_eq!(result[0].line, 1); assert_eq!(result[1].line, 7); }
1398
1399 #[test]
1400 fn test_fix_preserves_html_comments_when_disabled() {
1401 let config = MD044Config {
1402 names: vec!["JavaScript".to_string()],
1403 code_blocks: true, html_elements: true, html_comments: false, };
1407 let rule = MD044ProperNames::from_config_struct(config);
1408
1409 let content = r#"javascript here.
1410<!-- javascript in comment -->
1411More javascript."#;
1412
1413 let ctx = create_context(content);
1414 let fixed = rule.fix(&ctx).unwrap();
1415
1416 let expected = r#"JavaScript here.
1417<!-- javascript in comment -->
1418More JavaScript."#;
1419
1420 assert_eq!(
1421 fixed, expected,
1422 "Should not fix names inside HTML comments when disabled"
1423 );
1424 }
1425
1426 #[test]
1427 fn test_proper_names_in_link_text_are_flagged() {
1428 let rule = MD044ProperNames::new(
1429 vec!["JavaScript".to_string(), "Node.js".to_string(), "Python".to_string()],
1430 true,
1431 );
1432
1433 let content = r#"Check this [javascript documentation](https://javascript.info) for info.
1434
1435Visit [node.js homepage](https://nodejs.org) and [python tutorial](https://python.org).
1436
1437Real javascript should be flagged.
1438
1439Also see the [typescript guide][ts-ref] for more.
1440
1441Real python should be flagged too.
1442
1443[ts-ref]: https://typescript.org/handbook"#;
1444
1445 let ctx = create_context(content);
1446 let result = rule.check(&ctx).unwrap();
1447
1448 assert_eq!(result.len(), 5, "Expected 5 warnings: 3 in link text + 2 standalone");
1455
1456 let line_1_warnings: Vec<_> = result.iter().filter(|w| w.line == 1).collect();
1458 assert_eq!(line_1_warnings.len(), 1);
1459 assert!(
1460 line_1_warnings[0]
1461 .message
1462 .contains("'javascript' should be 'JavaScript'")
1463 );
1464
1465 let line_3_warnings: Vec<_> = result.iter().filter(|w| w.line == 3).collect();
1466 assert_eq!(line_3_warnings.len(), 2); assert!(result.iter().any(|w| w.line == 5 && w.message.contains("'javascript'")));
1470 assert!(result.iter().any(|w| w.line == 9 && w.message.contains("'python'")));
1471 }
1472
1473 #[test]
1474 fn test_link_urls_not_flagged() {
1475 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1476
1477 let content = r#"[Link Text](https://javascript.info/guide)"#;
1479
1480 let ctx = create_context(content);
1481 let result = rule.check(&ctx).unwrap();
1482
1483 assert!(result.is_empty(), "URLs should not be checked for proper names");
1485 }
1486
1487 #[test]
1488 fn test_proper_names_in_image_alt_text_are_flagged() {
1489 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1490
1491 let content = r#"Here is a  image.
1492
1493Real javascript should be flagged."#;
1494
1495 let ctx = create_context(content);
1496 let result = rule.check(&ctx).unwrap();
1497
1498 assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in alt text + 1 standalone");
1502 assert!(result[0].message.contains("'javascript' should be 'JavaScript'"));
1503 assert!(result[0].line == 1); assert!(result[1].message.contains("'javascript' should be 'JavaScript'"));
1505 assert!(result[1].line == 3); }
1507
1508 #[test]
1509 fn test_image_urls_not_flagged() {
1510 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1511
1512 let content = r#""#;
1514
1515 let ctx = create_context(content);
1516 let result = rule.check(&ctx).unwrap();
1517
1518 assert!(result.is_empty(), "Image URLs should not be checked for proper names");
1520 }
1521
1522 #[test]
1523 fn test_reference_link_text_flagged_but_definition_not() {
1524 let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
1525
1526 let content = r#"Check the [javascript guide][js-ref] for details.
1527
1528Real javascript should be flagged.
1529
1530[js-ref]: https://javascript.info/typescript/guide"#;
1531
1532 let ctx = create_context(content);
1533 let result = rule.check(&ctx).unwrap();
1534
1535 assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in link text + 1 standalone");
1540 assert!(result.iter().any(|w| w.line == 1 && w.message.contains("'javascript'")));
1541 assert!(result.iter().any(|w| w.line == 3 && w.message.contains("'javascript'")));
1542 }
1543
1544 #[test]
1545 fn test_reference_definitions_not_flagged() {
1546 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1547
1548 let content = r#"[js-ref]: https://javascript.info/guide"#;
1550
1551 let ctx = create_context(content);
1552 let result = rule.check(&ctx).unwrap();
1553
1554 assert!(result.is_empty(), "Reference definitions should not be checked");
1556 }
1557
1558 #[test]
1559 fn test_wikilinks_text_is_flagged() {
1560 let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1561
1562 let content = r#"[[javascript]]
1564
1565Regular javascript here.
1566
1567[[JavaScript|display text]]"#;
1568
1569 let ctx = create_context(content);
1570 let result = rule.check(&ctx).unwrap();
1571
1572 assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in WikiLink + 1 standalone");
1576 assert!(
1577 result
1578 .iter()
1579 .any(|w| w.line == 1 && w.column == 3 && w.message.contains("'javascript'"))
1580 );
1581 assert!(result.iter().any(|w| w.line == 3 && w.message.contains("'javascript'")));
1582 }
1583
1584 #[test]
1585 fn test_url_link_text_not_flagged() {
1586 let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1587
1588 let content = r#"[https://github.com/org/repo](https://github.com/org/repo)
1590
1591[http://github.com/org/repo](http://github.com/org/repo)
1592
1593[www.github.com/org/repo](https://www.github.com/org/repo)"#;
1594
1595 let ctx = create_context(content);
1596 let result = rule.check(&ctx).unwrap();
1597
1598 assert!(
1599 result.is_empty(),
1600 "URL-like link text should not be flagged, got: {result:?}"
1601 );
1602 }
1603
1604 #[test]
1605 fn test_url_link_text_with_leading_space_not_flagged() {
1606 let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1607
1608 let content = r#"[ https://github.com/org/repo](https://github.com/org/repo)"#;
1610
1611 let ctx = create_context(content);
1612 let result = rule.check(&ctx).unwrap();
1613
1614 assert!(
1615 result.is_empty(),
1616 "URL-like link text with leading space should not be flagged, got: {result:?}"
1617 );
1618 }
1619
1620 #[test]
1621 fn test_url_link_text_uppercase_scheme_not_flagged() {
1622 let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1623
1624 let content = r#"[HTTPS://GITHUB.COM/org/repo](https://github.com/org/repo)"#;
1625
1626 let ctx = create_context(content);
1627 let result = rule.check(&ctx).unwrap();
1628
1629 assert!(
1630 result.is_empty(),
1631 "URL-like link text with uppercase scheme should not be flagged, got: {result:?}"
1632 );
1633 }
1634
1635 #[test]
1636 fn test_non_url_link_text_still_flagged() {
1637 let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1638
1639 let content = r#"[github.com/org/repo](https://github.com/org/repo)
1641
1642[Visit github](https://github.com/org/repo)
1643
1644[//github.com/org/repo](//github.com/org/repo)
1645
1646[ftp://github.com/org/repo](ftp://github.com/org/repo)"#;
1647
1648 let ctx = create_context(content);
1649 let result = rule.check(&ctx).unwrap();
1650
1651 assert_eq!(result.len(), 4, "Non-URL link text should be flagged, got: {result:?}");
1652 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)); }
1657
1658 #[test]
1659 fn test_url_link_text_fix_not_applied() {
1660 let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1661
1662 let content = "[https://github.com/org/repo](https://github.com/org/repo)\n";
1663
1664 let ctx = create_context(content);
1665 let result = rule.fix(&ctx).unwrap();
1666
1667 assert_eq!(result, content, "Fix should not modify URL-like link text");
1668 }
1669
1670 #[test]
1671 fn test_mixed_url_and_regular_link_text() {
1672 let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1673
1674 let content = r#"[https://github.com/org/repo](https://github.com/org/repo)
1676
1677Visit [github documentation](https://github.com/docs) for details.
1678
1679[www.github.com/pricing](https://www.github.com/pricing)"#;
1680
1681 let ctx = create_context(content);
1682 let result = rule.check(&ctx).unwrap();
1683
1684 assert_eq!(
1686 result.len(),
1687 1,
1688 "Only non-URL link text should be flagged, got: {result:?}"
1689 );
1690 assert_eq!(result[0].line, 3);
1691 }
1692
1693 #[test]
1694 fn test_html_attribute_values_not_flagged() {
1695 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1698 let content = "# Heading\n\ntest\n\n<img src=\"www.example.test/test_image.png\">\n";
1699 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1700 let result = rule.check(&ctx).unwrap();
1701
1702 let line5_violations: Vec<_> = result.iter().filter(|w| w.line == 5).collect();
1704 assert!(
1705 line5_violations.is_empty(),
1706 "Should not flag anything inside HTML tag attributes: {line5_violations:?}"
1707 );
1708
1709 let line3_violations: Vec<_> = result.iter().filter(|w| w.line == 3).collect();
1711 assert_eq!(line3_violations.len(), 1, "Plain 'test' on line 3 should be flagged");
1712 }
1713
1714 #[test]
1715 fn test_html_text_content_still_flagged() {
1716 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1718 let content = "# Heading\n\n<a href=\"https://example.test/page\">test link</a>\n";
1719 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1720 let result = rule.check(&ctx).unwrap();
1721
1722 assert_eq!(
1725 result.len(),
1726 1,
1727 "Should flag only 'test' in anchor text, not in href: {result:?}"
1728 );
1729 assert_eq!(result[0].column, 37, "Should flag col 37 ('test link' in anchor text)");
1730 }
1731
1732 #[test]
1733 fn test_html_attribute_various_not_flagged() {
1734 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1736 let content = concat!(
1737 "# Heading\n\n",
1738 "<img src=\"test.png\" alt=\"test image\">\n",
1739 "<span class=\"test-class\" data-test=\"value\">test content</span>\n",
1740 );
1741 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1742 let result = rule.check(&ctx).unwrap();
1743
1744 assert_eq!(
1746 result.len(),
1747 1,
1748 "Should flag only 'test content' between tags: {result:?}"
1749 );
1750 assert_eq!(result[0].line, 4);
1751 }
1752
1753 #[test]
1754 fn test_plain_text_underscore_boundary_unchanged() {
1755 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1758 let content = "# Heading\n\ntest_image is here and just_test ends here\n";
1759 let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1760 let result = rule.check(&ctx).unwrap();
1761
1762 assert_eq!(
1765 result.len(),
1766 2,
1767 "Should flag 'test' in both 'test_image' and 'just_test': {result:?}"
1768 );
1769 let cols: Vec<usize> = result.iter().map(|w| w.column).collect();
1770 assert!(cols.contains(&1), "Should flag col 1 (test_image): {cols:?}");
1771 assert!(cols.contains(&29), "Should flag col 29 (just_test): {cols:?}");
1772 }
1773
1774 #[test]
1775 fn test_frontmatter_yaml_keys_not_flagged() {
1776 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1779
1780 let content = "---\ntitle: Heading\ntest: Some Test value\n---\n\nTest\n";
1781 let ctx = create_context(content);
1782 let result = rule.check(&ctx).unwrap();
1783
1784 assert!(
1788 result.is_empty(),
1789 "Should not flag YAML keys or correctly capitalized values: {result:?}"
1790 );
1791 }
1792
1793 #[test]
1794 fn test_frontmatter_yaml_values_flagged() {
1795 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1797
1798 let content = "---\ntitle: Heading\nkey: a test value\n---\n\nTest\n";
1799 let ctx = create_context(content);
1800 let result = rule.check(&ctx).unwrap();
1801
1802 assert_eq!(result.len(), 1, "Should flag 'test' in YAML value: {result:?}");
1804 assert_eq!(result[0].line, 3);
1805 assert_eq!(result[0].column, 8); }
1807
1808 #[test]
1809 fn test_frontmatter_key_matches_name_not_flagged() {
1810 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1812
1813 let content = "---\ntest: other value\n---\n\nBody text\n";
1814 let ctx = create_context(content);
1815 let result = rule.check(&ctx).unwrap();
1816
1817 assert!(
1818 result.is_empty(),
1819 "Should not flag YAML key that matches configured name: {result:?}"
1820 );
1821 }
1822
1823 #[test]
1824 fn test_frontmatter_empty_value_not_flagged() {
1825 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1827
1828 let content = "---\ntest:\ntest: \n---\n\nBody text\n";
1829 let ctx = create_context(content);
1830 let result = rule.check(&ctx).unwrap();
1831
1832 assert!(
1833 result.is_empty(),
1834 "Should not flag YAML keys with empty values: {result:?}"
1835 );
1836 }
1837
1838 #[test]
1839 fn test_frontmatter_nested_yaml_key_not_flagged() {
1840 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1842
1843 let content = "---\nparent:\n test: nested value\n---\n\nBody text\n";
1844 let ctx = create_context(content);
1845 let result = rule.check(&ctx).unwrap();
1846
1847 assert!(result.is_empty(), "Should not flag nested YAML keys: {result:?}");
1849 }
1850
1851 #[test]
1852 fn test_frontmatter_list_items_checked() {
1853 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1855
1856 let content = "---\ntags:\n - test\n - other\n---\n\nBody text\n";
1857 let ctx = create_context(content);
1858 let result = rule.check(&ctx).unwrap();
1859
1860 assert_eq!(result.len(), 1, "Should flag 'test' in YAML list item: {result:?}");
1862 assert_eq!(result[0].line, 3);
1863 }
1864
1865 #[test]
1866 fn test_frontmatter_value_with_multiple_colons() {
1867 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1869
1870 let content = "---\ntest: description: a test thing\n---\n\nBody text\n";
1871 let ctx = create_context(content);
1872 let result = rule.check(&ctx).unwrap();
1873
1874 assert_eq!(
1877 result.len(),
1878 1,
1879 "Should flag 'test' in value after first colon: {result:?}"
1880 );
1881 assert_eq!(result[0].line, 2);
1882 assert!(result[0].column > 6, "Violation column should be in value portion");
1883 }
1884
1885 #[test]
1886 fn test_frontmatter_does_not_affect_body() {
1887 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1889
1890 let content = "---\ntitle: Heading\n---\n\ntest should be flagged here\n";
1891 let ctx = create_context(content);
1892 let result = rule.check(&ctx).unwrap();
1893
1894 assert_eq!(result.len(), 1, "Should flag 'test' in body text: {result:?}");
1895 assert_eq!(result[0].line, 5);
1896 }
1897
1898 #[test]
1899 fn test_frontmatter_fix_corrects_values_preserves_keys() {
1900 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1902
1903 let content = "---\ntest: a test value\n---\n\ntest here\n";
1904 let ctx = create_context(content);
1905 let fixed = rule.fix(&ctx).unwrap();
1906
1907 assert_eq!(fixed, "---\ntest: a Test value\n---\n\nTest here\n");
1909 }
1910
1911 #[test]
1912 fn test_frontmatter_multiword_value_flagged() {
1913 let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
1915
1916 let content = "---\ndescription: Learn javascript and typescript\n---\n\nBody\n";
1917 let ctx = create_context(content);
1918 let result = rule.check(&ctx).unwrap();
1919
1920 assert_eq!(result.len(), 2, "Should flag both names in YAML value: {result:?}");
1921 assert!(result.iter().all(|w| w.line == 2));
1922 }
1923
1924 #[test]
1925 fn test_frontmatter_yaml_comments_not_checked() {
1926 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1928
1929 let content = "---\n# test comment\ntitle: Heading\n---\n\nBody text\n";
1930 let ctx = create_context(content);
1931 let result = rule.check(&ctx).unwrap();
1932
1933 assert!(result.is_empty(), "Should not flag names in YAML comments: {result:?}");
1934 }
1935
1936 #[test]
1937 fn test_frontmatter_delimiters_not_checked() {
1938 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1940
1941 let content = "---\ntitle: Heading\n---\n\ntest here\n";
1942 let ctx = create_context(content);
1943 let result = rule.check(&ctx).unwrap();
1944
1945 assert_eq!(result.len(), 1, "Should only flag body text: {result:?}");
1947 assert_eq!(result[0].line, 5);
1948 }
1949
1950 #[test]
1951 fn test_frontmatter_continuation_lines_checked() {
1952 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1954
1955 let content = "---\ndescription: >\n a test value\n continued here\n---\n\nBody\n";
1956 let ctx = create_context(content);
1957 let result = rule.check(&ctx).unwrap();
1958
1959 assert_eq!(result.len(), 1, "Should flag 'test' in continuation line: {result:?}");
1961 assert_eq!(result[0].line, 3);
1962 }
1963
1964 #[test]
1965 fn test_frontmatter_quoted_values_checked() {
1966 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1968
1969 let content = "---\ntitle: \"a test title\"\n---\n\nBody\n";
1970 let ctx = create_context(content);
1971 let result = rule.check(&ctx).unwrap();
1972
1973 assert_eq!(result.len(), 1, "Should flag 'test' in quoted YAML value: {result:?}");
1974 assert_eq!(result[0].line, 2);
1975 }
1976
1977 #[test]
1978 fn test_frontmatter_single_quoted_values_checked() {
1979 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1981
1982 let content = "---\ntitle: 'a test title'\n---\n\nBody\n";
1983 let ctx = create_context(content);
1984 let result = rule.check(&ctx).unwrap();
1985
1986 assert_eq!(
1987 result.len(),
1988 1,
1989 "Should flag 'test' in single-quoted YAML value: {result:?}"
1990 );
1991 assert_eq!(result[0].line, 2);
1992 }
1993
1994 #[test]
1995 fn test_frontmatter_fix_multiword_values() {
1996 let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
1998
1999 let content = "---\ndescription: Learn javascript and typescript\n---\n\nBody\n";
2000 let ctx = create_context(content);
2001 let fixed = rule.fix(&ctx).unwrap();
2002
2003 assert_eq!(
2004 fixed,
2005 "---\ndescription: Learn JavaScript and TypeScript\n---\n\nBody\n"
2006 );
2007 }
2008
2009 #[test]
2010 fn test_frontmatter_fix_preserves_yaml_structure() {
2011 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2013
2014 let content = "---\ntags:\n - test\n - other\ntitle: a test doc\n---\n\ntest body\n";
2015 let ctx = create_context(content);
2016 let fixed = rule.fix(&ctx).unwrap();
2017
2018 assert_eq!(
2019 fixed,
2020 "---\ntags:\n - Test\n - other\ntitle: a Test doc\n---\n\nTest body\n"
2021 );
2022 }
2023
2024 #[test]
2025 fn test_frontmatter_toml_delimiters_not_checked() {
2026 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2028
2029 let content = "+++\ntitle = \"a test title\"\n+++\n\ntest body\n";
2030 let ctx = create_context(content);
2031 let result = rule.check(&ctx).unwrap();
2032
2033 assert_eq!(result.len(), 2, "Should flag TOML value and body: {result:?}");
2037 let fm_violations: Vec<_> = result.iter().filter(|w| w.line == 2).collect();
2038 assert_eq!(fm_violations.len(), 1, "Should flag 'test' in TOML value: {result:?}");
2039 let body_violations: Vec<_> = result.iter().filter(|w| w.line == 5).collect();
2040 assert_eq!(body_violations.len(), 1, "Should flag body 'test': {result:?}");
2041 }
2042
2043 #[test]
2044 fn test_frontmatter_toml_key_not_flagged() {
2045 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2047
2048 let content = "+++\ntest = \"other value\"\n+++\n\nBody text\n";
2049 let ctx = create_context(content);
2050 let result = rule.check(&ctx).unwrap();
2051
2052 assert!(
2053 result.is_empty(),
2054 "Should not flag TOML key that matches configured name: {result:?}"
2055 );
2056 }
2057
2058 #[test]
2059 fn test_frontmatter_toml_fix_preserves_keys() {
2060 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2062
2063 let content = "+++\ntest = \"a test value\"\n+++\n\ntest here\n";
2064 let ctx = create_context(content);
2065 let fixed = rule.fix(&ctx).unwrap();
2066
2067 assert_eq!(fixed, "+++\ntest = \"a Test value\"\n+++\n\nTest here\n");
2069 }
2070
2071 #[test]
2072 fn test_frontmatter_list_item_mapping_key_not_flagged() {
2073 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2076
2077 let content = "---\nitems:\n - test: nested value\n---\n\nBody text\n";
2078 let ctx = create_context(content);
2079 let result = rule.check(&ctx).unwrap();
2080
2081 assert!(
2082 result.is_empty(),
2083 "Should not flag YAML key in list-item mapping: {result:?}"
2084 );
2085 }
2086
2087 #[test]
2088 fn test_frontmatter_list_item_mapping_value_flagged() {
2089 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2091
2092 let content = "---\nitems:\n - key: a test value\n---\n\nBody text\n";
2093 let ctx = create_context(content);
2094 let result = rule.check(&ctx).unwrap();
2095
2096 assert_eq!(
2097 result.len(),
2098 1,
2099 "Should flag 'test' in list-item mapping value: {result:?}"
2100 );
2101 assert_eq!(result[0].line, 3);
2102 }
2103
2104 #[test]
2105 fn test_frontmatter_bare_list_item_still_flagged() {
2106 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2108
2109 let content = "---\ntags:\n - test\n - other\n---\n\nBody text\n";
2110 let ctx = create_context(content);
2111 let result = rule.check(&ctx).unwrap();
2112
2113 assert_eq!(result.len(), 1, "Should flag 'test' in bare list item: {result:?}");
2114 assert_eq!(result[0].line, 3);
2115 }
2116
2117 #[test]
2118 fn test_frontmatter_flow_mapping_not_flagged() {
2119 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2122
2123 let content = "---\nflow_map: {test: value, other: test}\n---\n\nBody text\n";
2124 let ctx = create_context(content);
2125 let result = rule.check(&ctx).unwrap();
2126
2127 assert!(
2128 result.is_empty(),
2129 "Should not flag names inside flow mappings: {result:?}"
2130 );
2131 }
2132
2133 #[test]
2134 fn test_frontmatter_flow_sequence_not_flagged() {
2135 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2137
2138 let content = "---\nitems: [test, other, test]\n---\n\nBody text\n";
2139 let ctx = create_context(content);
2140 let result = rule.check(&ctx).unwrap();
2141
2142 assert!(
2143 result.is_empty(),
2144 "Should not flag names inside flow sequences: {result:?}"
2145 );
2146 }
2147
2148 #[test]
2149 fn test_frontmatter_list_item_mapping_fix_preserves_key() {
2150 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2152
2153 let content = "---\nitems:\n - test: a test value\n---\n\ntest here\n";
2154 let ctx = create_context(content);
2155 let fixed = rule.fix(&ctx).unwrap();
2156
2157 assert_eq!(fixed, "---\nitems:\n - test: a Test value\n---\n\nTest here\n");
2160 }
2161
2162 #[test]
2165 fn test_angle_bracket_url_in_html_comment_not_flagged() {
2166 let config = MD044Config {
2168 names: vec!["Test".to_string()],
2169 ..MD044Config::default()
2170 };
2171 let rule = MD044ProperNames::from_config_struct(config);
2172
2173 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";
2174 let ctx = create_context(content);
2175 let result = rule.check(&ctx).unwrap();
2176
2177 let line8_warnings: Vec<_> = result.iter().filter(|w| w.line == 8).collect();
2185 assert!(
2186 line8_warnings.is_empty(),
2187 "Should not flag names inside angle-bracket URLs in HTML comments: {line8_warnings:?}"
2188 );
2189 }
2190
2191 #[test]
2192 fn test_bare_url_in_html_comment_still_flagged() {
2193 let config = MD044Config {
2195 names: vec!["Test".to_string()],
2196 ..MD044Config::default()
2197 };
2198 let rule = MD044ProperNames::from_config_struct(config);
2199
2200 let content = "<!-- This is a test https://www.example.test -->\n";
2201 let ctx = create_context(content);
2202 let result = rule.check(&ctx).unwrap();
2203
2204 assert!(
2207 !result.is_empty(),
2208 "Should flag 'test' in prose text of HTML comment with bare URL"
2209 );
2210 }
2211
2212 #[test]
2213 fn test_angle_bracket_url_in_regular_markdown_not_flagged() {
2214 let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2217
2218 let content = "<https://www.example.test>\n";
2219 let ctx = create_context(content);
2220 let result = rule.check(&ctx).unwrap();
2221
2222 assert!(
2223 result.is_empty(),
2224 "Should not flag names inside angle-bracket URLs in regular markdown: {result:?}"
2225 );
2226 }
2227
2228 #[test]
2229 fn test_multiple_angle_bracket_urls_in_one_comment() {
2230 let config = MD044Config {
2231 names: vec!["Test".to_string()],
2232 ..MD044Config::default()
2233 };
2234 let rule = MD044ProperNames::from_config_struct(config);
2235
2236 let content = "<!-- See <https://test.example.com> and <https://www.example.test> for details -->\n";
2237 let ctx = create_context(content);
2238 let result = rule.check(&ctx).unwrap();
2239
2240 assert!(
2242 result.is_empty(),
2243 "Should not flag names inside multiple angle-bracket URLs: {result:?}"
2244 );
2245 }
2246
2247 #[test]
2248 fn test_angle_bracket_non_url_still_flagged() {
2249 assert!(
2252 !MD044ProperNames::is_in_angle_bracket_url("<test> which is not a URL.", 1),
2253 "is_in_angle_bracket_url should return false for non-URL angle brackets"
2254 );
2255 }
2256
2257 #[test]
2258 fn test_angle_bracket_mailto_url_not_flagged() {
2259 let config = MD044Config {
2260 names: vec!["Test".to_string()],
2261 ..MD044Config::default()
2262 };
2263 let rule = MD044ProperNames::from_config_struct(config);
2264
2265 let content = "<!-- Contact <mailto:test@example.com> for help -->\n";
2266 let ctx = create_context(content);
2267 let result = rule.check(&ctx).unwrap();
2268
2269 assert!(
2270 result.is_empty(),
2271 "Should not flag names inside angle-bracket mailto URLs: {result:?}"
2272 );
2273 }
2274
2275 #[test]
2276 fn test_angle_bracket_ftp_url_not_flagged() {
2277 let config = MD044Config {
2278 names: vec!["Test".to_string()],
2279 ..MD044Config::default()
2280 };
2281 let rule = MD044ProperNames::from_config_struct(config);
2282
2283 let content = "<!-- Download from <ftp://test.example.com/file> -->\n";
2284 let ctx = create_context(content);
2285 let result = rule.check(&ctx).unwrap();
2286
2287 assert!(
2288 result.is_empty(),
2289 "Should not flag names inside angle-bracket FTP URLs: {result:?}"
2290 );
2291 }
2292
2293 #[test]
2294 fn test_angle_bracket_url_fix_preserves_url() {
2295 let config = MD044Config {
2297 names: vec!["Test".to_string()],
2298 ..MD044Config::default()
2299 };
2300 let rule = MD044ProperNames::from_config_struct(config);
2301
2302 let content = "<!-- test text <https://www.example.test> -->\n";
2303 let ctx = create_context(content);
2304 let fixed = rule.fix(&ctx).unwrap();
2305
2306 assert!(
2308 fixed.contains("<https://www.example.test>"),
2309 "Fix should preserve angle-bracket URLs: {fixed}"
2310 );
2311 assert!(
2312 fixed.contains("Test text"),
2313 "Fix should correct prose 'test' to 'Test': {fixed}"
2314 );
2315 }
2316
2317 #[test]
2318 fn test_is_in_angle_bracket_url_helper() {
2319 let line = "text <https://example.test> more text";
2321
2322 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));
2335
2336 assert!(MD044ProperNames::is_in_angle_bracket_url(
2338 "<mailto:test@example.com>",
2339 10
2340 ));
2341
2342 assert!(MD044ProperNames::is_in_angle_bracket_url(
2344 "<ftp://test.example.com>",
2345 10
2346 ));
2347 }
2348
2349 #[test]
2350 fn test_is_in_angle_bracket_url_uppercase_scheme() {
2351 assert!(MD044ProperNames::is_in_angle_bracket_url(
2353 "<HTTPS://test.example.com>",
2354 10
2355 ));
2356 assert!(MD044ProperNames::is_in_angle_bracket_url(
2357 "<Http://test.example.com>",
2358 10
2359 ));
2360 }
2361
2362 #[test]
2363 fn test_is_in_angle_bracket_url_uncommon_schemes() {
2364 assert!(MD044ProperNames::is_in_angle_bracket_url(
2366 "<ssh://test@example.com>",
2367 10
2368 ));
2369 assert!(MD044ProperNames::is_in_angle_bracket_url("<file:///test/path>", 10));
2371 assert!(MD044ProperNames::is_in_angle_bracket_url("<data:text/plain;test>", 10));
2373 }
2374
2375 #[test]
2376 fn test_is_in_angle_bracket_url_unclosed() {
2377 assert!(!MD044ProperNames::is_in_angle_bracket_url(
2379 "<https://test.example.com",
2380 10
2381 ));
2382 }
2383
2384 #[test]
2385 fn test_vale_inline_config_comments_not_flagged() {
2386 let config = MD044Config {
2387 names: vec!["Vale".to_string(), "JavaScript".to_string()],
2388 ..MD044Config::default()
2389 };
2390 let rule = MD044ProperNames::from_config_struct(config);
2391
2392 let content = "\
2393<!-- vale off -->
2394Some javascript text here.
2395<!-- vale on -->
2396<!-- vale Style.Rule = NO -->
2397More javascript text.
2398<!-- vale Style.Rule = YES -->
2399<!-- vale JavaScript.Grammar = NO -->
2400";
2401 let ctx = create_context(content);
2402 let result = rule.check(&ctx).unwrap();
2403
2404 assert_eq!(result.len(), 2, "Should only flag body lines, not Vale config comments");
2406 assert_eq!(result[0].line, 2);
2407 assert_eq!(result[1].line, 5);
2408 }
2409
2410 #[test]
2411 fn test_remark_lint_inline_config_comments_not_flagged() {
2412 let config = MD044Config {
2413 names: vec!["JavaScript".to_string()],
2414 ..MD044Config::default()
2415 };
2416 let rule = MD044ProperNames::from_config_struct(config);
2417
2418 let content = "\
2419<!-- lint disable remark-lint-some-rule -->
2420Some javascript text here.
2421<!-- lint enable remark-lint-some-rule -->
2422<!-- lint ignore remark-lint-some-rule -->
2423More javascript text.
2424";
2425 let ctx = create_context(content);
2426 let result = rule.check(&ctx).unwrap();
2427
2428 assert_eq!(
2429 result.len(),
2430 2,
2431 "Should only flag body lines, not remark-lint config comments"
2432 );
2433 assert_eq!(result[0].line, 2);
2434 assert_eq!(result[1].line, 5);
2435 }
2436
2437 #[test]
2438 fn test_fix_does_not_modify_vale_remark_lint_comments() {
2439 let config = MD044Config {
2440 names: vec!["JavaScript".to_string(), "Vale".to_string()],
2441 ..MD044Config::default()
2442 };
2443 let rule = MD044ProperNames::from_config_struct(config);
2444
2445 let content = "\
2446<!-- vale off -->
2447Some javascript text.
2448<!-- vale on -->
2449<!-- lint disable remark-lint-some-rule -->
2450More javascript text.
2451<!-- lint enable remark-lint-some-rule -->
2452";
2453 let ctx = create_context(content);
2454 let fixed = rule.fix(&ctx).unwrap();
2455
2456 assert!(fixed.contains("<!-- vale off -->"));
2458 assert!(fixed.contains("<!-- vale on -->"));
2459 assert!(fixed.contains("<!-- lint disable remark-lint-some-rule -->"));
2460 assert!(fixed.contains("<!-- lint enable remark-lint-some-rule -->"));
2461 assert!(fixed.contains("Some JavaScript text."));
2463 assert!(fixed.contains("More JavaScript text."));
2464 }
2465
2466 #[test]
2467 fn test_mixed_tool_directives_all_skipped() {
2468 let config = MD044Config {
2469 names: vec!["JavaScript".to_string(), "Vale".to_string()],
2470 ..MD044Config::default()
2471 };
2472 let rule = MD044ProperNames::from_config_struct(config);
2473
2474 let content = "\
2475<!-- rumdl-disable MD044 -->
2476Some javascript text.
2477<!-- markdownlint-disable -->
2478More javascript text.
2479<!-- vale off -->
2480Even more javascript text.
2481<!-- lint disable some-rule -->
2482Final javascript text.
2483<!-- rumdl-enable MD044 -->
2484<!-- markdownlint-enable -->
2485<!-- vale on -->
2486<!-- lint enable some-rule -->
2487";
2488 let ctx = create_context(content);
2489 let result = rule.check(&ctx).unwrap();
2490
2491 assert_eq!(
2493 result.len(),
2494 4,
2495 "Should only flag body lines, not any tool directive comments"
2496 );
2497 assert_eq!(result[0].line, 2);
2498 assert_eq!(result[1].line, 4);
2499 assert_eq!(result[2].line, 6);
2500 assert_eq!(result[3].line, 8);
2501 }
2502
2503 #[test]
2504 fn test_vale_remark_lint_edge_cases_not_matched() {
2505 let config = MD044Config {
2506 names: vec!["JavaScript".to_string(), "Vale".to_string()],
2507 ..MD044Config::default()
2508 };
2509 let rule = MD044ProperNames::from_config_struct(config);
2510
2511 let content = "\
2519<!-- vale -->
2520<!-- vale is a tool for writing -->
2521<!-- valedictorian javascript -->
2522<!-- linting javascript tips -->
2523<!-- vale javascript -->
2524<!-- lint your javascript code -->
2525";
2526 let ctx = create_context(content);
2527 let result = rule.check(&ctx).unwrap();
2528
2529 assert_eq!(
2536 result.len(),
2537 7,
2538 "Should flag proper names in non-directive HTML comments: got {result:?}"
2539 );
2540 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); }
2548
2549 #[test]
2550 fn test_vale_style_directives_skipped() {
2551 let config = MD044Config {
2552 names: vec!["JavaScript".to_string(), "Vale".to_string()],
2553 ..MD044Config::default()
2554 };
2555 let rule = MD044ProperNames::from_config_struct(config);
2556
2557 let content = "\
2559<!-- vale style = MyStyle -->
2560<!-- vale styles = Style1, Style2 -->
2561<!-- vale MyRule.Name = YES -->
2562<!-- vale MyRule.Name = NO -->
2563Some javascript text.
2564";
2565 let ctx = create_context(content);
2566 let result = rule.check(&ctx).unwrap();
2567
2568 assert_eq!(
2570 result.len(),
2571 1,
2572 "Should only flag body lines, not Vale style/rule directives: got {result:?}"
2573 );
2574 assert_eq!(result[0].line, 5);
2575 }
2576
2577 #[test]
2580 fn test_backtick_code_single_backticks() {
2581 let line = "hello `world` bye";
2582 assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 7));
2584 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
2586 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 14));
2588 }
2589
2590 #[test]
2591 fn test_backtick_code_double_backticks() {
2592 let line = "a ``code`` b";
2593 assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 4));
2595 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
2597 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 11));
2599 }
2600
2601 #[test]
2602 fn test_backtick_code_unclosed() {
2603 let line = "a `code b";
2604 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 3));
2606 }
2607
2608 #[test]
2609 fn test_backtick_code_mismatched_count() {
2610 let line = "a `code`` b";
2612 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 3));
2615 }
2616
2617 #[test]
2618 fn test_backtick_code_multiple_spans() {
2619 let line = "`first` and `second`";
2620 assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 1));
2622 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 8));
2624 assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 13));
2626 }
2627
2628 #[test]
2629 fn test_backtick_code_on_backtick_boundary() {
2630 let line = "`code`";
2631 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
2633 assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 5));
2635 assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 1));
2637 assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 4));
2638 }
2639}