1use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, Severity};
7use crate::rule_config_serde::RuleConfig;
8use crate::utils::range_utils::calculate_match_range;
9use crate::utils::regex_cache::get_cached_regex;
10use toml;
11
12mod md014_config;
13use md014_config::MD014Config;
14
15const COMMAND_PATTERN: &str = r"^\s*[$>]\s+\S+";
17const SHELL_LANG_PATTERN: &str = r"^(?i)(bash|sh|shell|console|terminal)";
18const DOLLAR_PROMPT_PATTERN: &str = r"^\s*([$>])";
19
20#[derive(Clone, Default)]
21pub struct MD014CommandsShowOutput {
22 config: MD014Config,
23}
24
25impl MD014CommandsShowOutput {
26 pub fn new() -> Self {
27 Self::default()
28 }
29
30 pub fn with_show_output(show_output: bool) -> Self {
31 Self {
32 config: MD014Config { show_output },
33 }
34 }
35
36 pub fn from_config_struct(config: MD014Config) -> Self {
37 Self { config }
38 }
39
40 fn is_command_line(&self, line: &str) -> bool {
41 get_cached_regex(COMMAND_PATTERN)
42 .map(|re| re.is_match(line))
43 .unwrap_or(false)
44 }
45
46 fn is_shell_language(&self, lang: &str) -> bool {
47 get_cached_regex(SHELL_LANG_PATTERN)
48 .map(|re| re.is_match(lang))
49 .unwrap_or(false)
50 }
51
52 fn is_output_line(&self, line: &str) -> bool {
53 let trimmed = line.trim();
54 !trimmed.is_empty() && !trimmed.starts_with('$') && !trimmed.starts_with('>') && !trimmed.starts_with('#')
55 }
56
57 fn is_no_output_command(&self, cmd: &str) -> bool {
58 let cmd = cmd.trim().to_lowercase();
59
60 cmd.starts_with("cd ")
66 || cmd == "cd"
67 || cmd.starts_with("mkdir ")
68 || cmd.starts_with("touch ")
69 || cmd.starts_with("rm ")
70 || cmd.starts_with("mv ")
71 || cmd.starts_with("cp ")
72 || cmd.starts_with("export ")
73 || cmd.starts_with("set ")
74 || cmd.starts_with("alias ")
75 || cmd.starts_with("unset ")
76 || cmd.starts_with("source ")
77 || cmd.starts_with(". ")
78 || cmd == "true"
79 || cmd == "false"
80 || cmd.starts_with("sleep ")
81 || cmd.starts_with("wait ")
82 || cmd.starts_with("pushd ")
83 || cmd.starts_with("popd")
84
85 || cmd.contains(" > ")
87 || cmd.contains(" >> ")
88
89 || cmd.starts_with("git add ")
91 || cmd.starts_with("git checkout ")
92 || cmd.starts_with("git stash")
93 || cmd.starts_with("git reset ")
94 }
95
96 fn is_command_without_output(&self, block: &[&str], lang: &str) -> bool {
97 if !self.config.show_output || !self.is_shell_language(lang) {
98 return false;
99 }
100
101 let has_output = block.iter().any(|line| self.is_output_line(line));
103 if has_output {
104 return false; }
106
107 self.get_first_output_command(block).is_some()
109 }
110
111 fn get_first_output_command(&self, block: &[&str]) -> Option<(usize, String)> {
114 for (i, line) in block.iter().enumerate() {
115 if self.is_command_line(line) {
116 let cmd = line.trim()[1..].trim().to_string();
117 if !self.is_no_output_command(&cmd) {
118 return Some((i, cmd));
119 }
120 }
121 }
122 None }
124
125 fn get_command_from_block(&self, block: &[&str]) -> String {
126 if let Some((_, cmd)) = self.get_first_output_command(block) {
128 return cmd;
129 }
130 for line in block {
132 let trimmed = line.trim();
133 if self.is_command_line(line) {
134 return trimmed[1..].trim().to_string();
135 }
136 }
137 String::new()
138 }
139
140 fn fix_command_block(&self, block: &[&str]) -> String {
141 block
142 .iter()
143 .map(|line| {
144 let trimmed = line.trim_start();
145 if self.is_command_line(line) {
146 let spaces = line.len() - line.trim_start().len();
147 let cmd = trimmed.chars().skip(1).collect::<String>().trim_start().to_string();
148 format!("{}{}", " ".repeat(spaces), cmd)
149 } else {
150 line.to_string()
151 }
152 })
153 .collect::<Vec<_>>()
154 .join("\n")
155 }
156
157 fn get_code_block_language(block_start: &str) -> String {
158 block_start
159 .trim_start()
160 .trim_start_matches("```")
161 .split_whitespace()
162 .next()
163 .unwrap_or("")
164 .to_string()
165 }
166
167 fn find_first_command_line<'a>(&self, block: &[&'a str]) -> Option<(usize, &'a str)> {
170 for (i, line) in block.iter().enumerate() {
171 if self.is_command_line(line) {
172 let cmd = line.trim()[1..].trim();
173 if !self.is_no_output_command(cmd) {
174 return Some((i, line));
175 }
176 }
177 }
178 None
179 }
180}
181
182impl Rule for MD014CommandsShowOutput {
183 fn name(&self) -> &'static str {
184 "MD014"
185 }
186
187 fn description(&self) -> &'static str {
188 "Commands in code blocks should show output"
189 }
190
191 fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
192 let content = ctx.content;
193 let _line_index = &ctx.line_index;
194
195 let mut warnings = Vec::new();
196
197 let mut current_block = Vec::new();
198
199 let mut in_code_block = false;
200
201 let mut block_start_line = 0;
202
203 let mut current_lang = String::new();
204
205 for (line_num, line) in content.lines().enumerate() {
206 if line.trim_start().starts_with("```") {
207 if in_code_block {
208 if self.is_command_without_output(¤t_block, ¤t_lang) {
210 if let Some((cmd_line_idx, cmd_line)) = self.find_first_command_line(¤t_block) {
212 let cmd_line_num = block_start_line + 1 + cmd_line_idx + 1; if let Ok(re) = get_cached_regex(DOLLAR_PROMPT_PATTERN)
216 && let Some(cap) = re.captures(cmd_line)
217 {
218 let match_obj = cap.get(1).unwrap(); let (start_line, start_col, end_line, end_col) =
220 calculate_match_range(cmd_line_num, cmd_line, match_obj.start(), match_obj.len());
221
222 let command = self.get_command_from_block(¤t_block);
224 let message = if command.is_empty() {
225 "Command should show output (add example output or remove $ prompt)".to_string()
226 } else {
227 format!(
228 "Command '{command}' should show output (add example output or remove $ prompt)"
229 )
230 };
231
232 warnings.push(LintWarning {
233 rule_name: Some(self.name().to_string()),
234 line: start_line,
235 column: start_col,
236 end_line,
237 end_column: end_col,
238 message,
239 severity: Severity::Warning,
240 fix: Some(Fix {
241 range: {
242 let content_start_line = block_start_line + 1; let content_end_line = line_num - 1; let start_byte =
248 _line_index.get_line_start_byte(content_start_line + 1).unwrap_or(0); let end_byte = _line_index
250 .get_line_start_byte(content_end_line + 2)
251 .unwrap_or(start_byte); start_byte..end_byte
253 },
254 replacement: format!("{}\n", self.fix_command_block(¤t_block)),
255 }),
256 });
257 }
258 }
259 }
260 current_block.clear();
261 } else {
262 block_start_line = line_num;
264 current_lang = Self::get_code_block_language(line);
265 }
266 in_code_block = !in_code_block;
267 } else if in_code_block {
268 current_block.push(line);
269 }
270 }
271
272 Ok(warnings)
273 }
274
275 fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
276 let content = ctx.content;
277 let _line_index = &ctx.line_index;
278
279 let mut result = String::new();
280
281 let mut current_block = Vec::new();
282
283 let mut in_code_block = false;
284
285 let mut current_lang = String::new();
286
287 for line in content.lines() {
288 if line.trim_start().starts_with("```") {
289 if in_code_block {
290 if self.is_command_without_output(¤t_block, ¤t_lang) {
292 result.push_str(&self.fix_command_block(¤t_block));
293 result.push('\n');
294 } else {
295 for block_line in ¤t_block {
296 result.push_str(block_line);
297 result.push('\n');
298 }
299 }
300 current_block.clear();
301 } else {
302 current_lang = Self::get_code_block_language(line);
303 }
304 in_code_block = !in_code_block;
305 result.push_str(line);
306 result.push('\n');
307 } else if in_code_block {
308 current_block.push(line);
309 } else {
310 result.push_str(line);
311 result.push('\n');
312 }
313 }
314
315 if !content.ends_with('\n') && result.ends_with('\n') {
317 result.pop();
318 }
319
320 Ok(result)
321 }
322
323 fn as_any(&self) -> &dyn std::any::Any {
324 self
325 }
326
327 fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
328 ctx.content.is_empty() || !ctx.likely_has_code()
330 }
331
332 fn default_config_section(&self) -> Option<(String, toml::Value)> {
333 let default_config = MD014Config::default();
334 let json_value = serde_json::to_value(&default_config).ok()?;
335 let toml_value = crate::rule_config_serde::json_to_toml_value(&json_value)?;
336
337 if let toml::Value::Table(table) = toml_value {
338 if !table.is_empty() {
339 Some((MD014Config::RULE_NAME.to_string(), toml::Value::Table(table)))
340 } else {
341 None
342 }
343 } else {
344 None
345 }
346 }
347
348 fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
349 where
350 Self: Sized,
351 {
352 let rule_config = crate::rule_config_serde::load_rule_config::<MD014Config>(config);
353 Box::new(Self::from_config_struct(rule_config))
354 }
355}
356
357#[cfg(test)]
358mod tests {
359 use super::*;
360 use crate::lint_context::LintContext;
361
362 #[test]
363 fn test_is_command_line() {
364 let rule = MD014CommandsShowOutput::new();
365 assert!(rule.is_command_line("$ echo test"));
366 assert!(rule.is_command_line(" $ ls -la"));
367 assert!(rule.is_command_line("> pwd"));
368 assert!(rule.is_command_line(" > cd /home"));
369 assert!(!rule.is_command_line("echo test"));
370 assert!(!rule.is_command_line("# comment"));
371 assert!(!rule.is_command_line("output line"));
372 }
373
374 #[test]
375 fn test_is_shell_language() {
376 let rule = MD014CommandsShowOutput::new();
377 assert!(rule.is_shell_language("bash"));
378 assert!(rule.is_shell_language("BASH"));
379 assert!(rule.is_shell_language("sh"));
380 assert!(rule.is_shell_language("shell"));
381 assert!(rule.is_shell_language("Shell"));
382 assert!(rule.is_shell_language("console"));
383 assert!(rule.is_shell_language("CONSOLE"));
384 assert!(rule.is_shell_language("terminal"));
385 assert!(rule.is_shell_language("Terminal"));
386 assert!(!rule.is_shell_language("python"));
387 assert!(!rule.is_shell_language("javascript"));
388 assert!(!rule.is_shell_language(""));
389 }
390
391 #[test]
392 fn test_is_output_line() {
393 let rule = MD014CommandsShowOutput::new();
394 assert!(rule.is_output_line("output text"));
395 assert!(rule.is_output_line(" some output"));
396 assert!(rule.is_output_line("file1 file2"));
397 assert!(!rule.is_output_line(""));
398 assert!(!rule.is_output_line(" "));
399 assert!(!rule.is_output_line("$ command"));
400 assert!(!rule.is_output_line("> prompt"));
401 assert!(!rule.is_output_line("# comment"));
402 }
403
404 #[test]
405 fn test_is_no_output_command() {
406 let rule = MD014CommandsShowOutput::new();
407
408 assert!(rule.is_no_output_command("cd /home"));
410 assert!(rule.is_no_output_command("cd"));
411 assert!(rule.is_no_output_command("mkdir test"));
412 assert!(rule.is_no_output_command("touch file.txt"));
413 assert!(rule.is_no_output_command("rm -rf dir"));
414 assert!(rule.is_no_output_command("mv old new"));
415 assert!(rule.is_no_output_command("cp src dst"));
416 assert!(rule.is_no_output_command("export VAR=value"));
417 assert!(rule.is_no_output_command("set -e"));
418 assert!(rule.is_no_output_command("source ~/.bashrc"));
419 assert!(rule.is_no_output_command(". ~/.profile"));
420 assert!(rule.is_no_output_command("alias ll='ls -la'"));
421 assert!(rule.is_no_output_command("unset VAR"));
422 assert!(rule.is_no_output_command("true"));
423 assert!(rule.is_no_output_command("false"));
424 assert!(rule.is_no_output_command("sleep 5"));
425 assert!(rule.is_no_output_command("pushd /tmp"));
426 assert!(rule.is_no_output_command("popd"));
427
428 assert!(rule.is_no_output_command("CD /HOME"));
430 assert!(rule.is_no_output_command("MKDIR TEST"));
431
432 assert!(rule.is_no_output_command("echo 'test' > file.txt"));
434 assert!(rule.is_no_output_command("cat input.txt > output.txt"));
435 assert!(rule.is_no_output_command("echo 'append' >> log.txt"));
436
437 assert!(rule.is_no_output_command("git add ."));
439 assert!(rule.is_no_output_command("git checkout main"));
440 assert!(rule.is_no_output_command("git stash"));
441 assert!(rule.is_no_output_command("git reset HEAD~1"));
442
443 assert!(!rule.is_no_output_command("ls -la"));
445 assert!(!rule.is_no_output_command("echo test")); assert!(!rule.is_no_output_command("pwd"));
447 assert!(!rule.is_no_output_command("cat file.txt")); assert!(!rule.is_no_output_command("grep pattern file"));
449
450 assert!(!rule.is_no_output_command("pip install requests"));
452 assert!(!rule.is_no_output_command("npm install express"));
453 assert!(!rule.is_no_output_command("cargo install ripgrep"));
454 assert!(!rule.is_no_output_command("brew install git"));
455
456 assert!(!rule.is_no_output_command("cargo build"));
458 assert!(!rule.is_no_output_command("npm run build"));
459 assert!(!rule.is_no_output_command("make"));
460
461 assert!(!rule.is_no_output_command("docker ps"));
463 assert!(!rule.is_no_output_command("docker compose up"));
464 assert!(!rule.is_no_output_command("docker run myimage"));
465
466 assert!(!rule.is_no_output_command("git status"));
468 assert!(!rule.is_no_output_command("git log"));
469 assert!(!rule.is_no_output_command("git diff"));
470 }
471
472 #[test]
473 fn test_get_command_from_block() {
474 let rule = MD014CommandsShowOutput::new();
475 let block = vec!["$ echo test", "output"];
476 assert_eq!(rule.get_command_from_block(&block), "echo test");
477
478 let block2 = vec![" $ ls -la", "file1 file2"];
479 assert_eq!(rule.get_command_from_block(&block2), "ls -la");
480
481 let block3 = vec!["> pwd", "/home"];
482 assert_eq!(rule.get_command_from_block(&block3), "pwd");
483
484 let empty_block: Vec<&str> = vec![];
485 assert_eq!(rule.get_command_from_block(&empty_block), "");
486 }
487
488 #[test]
489 fn test_fix_command_block() {
490 let rule = MD014CommandsShowOutput::new();
491 let block = vec!["$ echo test", "$ ls -la"];
492 assert_eq!(rule.fix_command_block(&block), "echo test\nls -la");
493
494 let indented = vec![" $ echo test", " $ pwd"];
495 assert_eq!(rule.fix_command_block(&indented), " echo test\n pwd");
496
497 let mixed = vec!["> cd /home", "$ mkdir test"];
498 assert_eq!(rule.fix_command_block(&mixed), "cd /home\nmkdir test");
499 }
500
501 #[test]
502 fn test_get_code_block_language() {
503 assert_eq!(MD014CommandsShowOutput::get_code_block_language("```bash"), "bash");
504 assert_eq!(MD014CommandsShowOutput::get_code_block_language("```shell"), "shell");
505 assert_eq!(
506 MD014CommandsShowOutput::get_code_block_language(" ```console"),
507 "console"
508 );
509 assert_eq!(
510 MD014CommandsShowOutput::get_code_block_language("```bash {.line-numbers}"),
511 "bash"
512 );
513 assert_eq!(MD014CommandsShowOutput::get_code_block_language("```"), "");
514 }
515
516 #[test]
517 fn test_find_first_command_line() {
518 let rule = MD014CommandsShowOutput::new();
519 let block = vec!["# comment", "$ echo test", "output"];
520 let result = rule.find_first_command_line(&block);
521 assert_eq!(result, Some((1, "$ echo test")));
522
523 let no_commands = vec!["output1", "output2"];
524 assert_eq!(rule.find_first_command_line(&no_commands), None);
525 }
526
527 #[test]
528 fn test_is_command_without_output() {
529 let rule = MD014CommandsShowOutput::with_show_output(true);
530
531 let block1 = vec!["$ echo test"];
533 assert!(rule.is_command_without_output(&block1, "bash"));
534
535 let block2 = vec!["$ echo test", "test"];
537 assert!(!rule.is_command_without_output(&block2, "bash"));
538
539 let block3 = vec!["$ cd /home"];
541 assert!(!rule.is_command_without_output(&block3, "bash"));
542
543 let rule_disabled = MD014CommandsShowOutput::with_show_output(false);
545 assert!(!rule_disabled.is_command_without_output(&block1, "bash"));
546
547 assert!(!rule.is_command_without_output(&block1, "python"));
549 }
550
551 #[test]
552 fn test_edge_cases() {
553 let rule = MD014CommandsShowOutput::new();
554 let content = "```bash\n$ \n```";
556 let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
557 let result = rule.check(&ctx).unwrap();
558 assert!(
559 result.is_empty(),
560 "Bare $ with only space doesn't match command pattern"
561 );
562
563 let empty_content = "```bash\n```";
565 let ctx2 = LintContext::new(empty_content, crate::config::MarkdownFlavor::Standard, None);
566 let result2 = rule.check(&ctx2).unwrap();
567 assert!(result2.is_empty(), "Empty code block should not be flagged");
568
569 let minimal = "```bash\n$ a\n```";
571 let ctx3 = LintContext::new(minimal, crate::config::MarkdownFlavor::Standard, None);
572 let result3 = rule.check(&ctx3).unwrap();
573 assert_eq!(result3.len(), 1, "Minimal command should be flagged");
574 }
575
576 #[test]
577 fn test_mixed_silent_and_output_commands() {
578 let rule = MD014CommandsShowOutput::new();
579
580 let silent_only = "```bash\n$ cd /home\n$ mkdir test\n```";
582 let ctx1 = LintContext::new(silent_only, crate::config::MarkdownFlavor::Standard, None);
583 let result1 = rule.check(&ctx1).unwrap();
584 assert!(
585 result1.is_empty(),
586 "Block with only silent commands should not be flagged"
587 );
588
589 let mixed_silent_first = "```bash\n$ cd /home\n$ ls -la\n```";
592 let ctx2 = LintContext::new(mixed_silent_first, crate::config::MarkdownFlavor::Standard, None);
593 let result2 = rule.check(&ctx2).unwrap();
594 assert_eq!(result2.len(), 1, "Mixed block should be flagged once");
595 assert!(
596 result2[0].message.contains("ls -la"),
597 "Message should mention 'ls -la', not 'cd /home'. Got: {}",
598 result2[0].message
599 );
600
601 let mixed_mkdir_cat = "```bash\n$ mkdir test\n$ cat file.txt\n```";
603 let ctx3 = LintContext::new(mixed_mkdir_cat, crate::config::MarkdownFlavor::Standard, None);
604 let result3 = rule.check(&ctx3).unwrap();
605 assert_eq!(result3.len(), 1, "Mixed block should be flagged once");
606 assert!(
607 result3[0].message.contains("cat file.txt"),
608 "Message should mention 'cat file.txt', not 'mkdir'. Got: {}",
609 result3[0].message
610 );
611
612 let mkdir_pip = "```bash\n$ mkdir test\n$ pip install something\n```";
615 let ctx3b = LintContext::new(mkdir_pip, crate::config::MarkdownFlavor::Standard, None);
616 let result3b = rule.check(&ctx3b).unwrap();
617 assert_eq!(result3b.len(), 1, "Block with pip install should be flagged");
618 assert!(
619 result3b[0].message.contains("pip install"),
620 "Message should mention 'pip install'. Got: {}",
621 result3b[0].message
622 );
623
624 let mixed_output_first = "```bash\n$ echo hello\n$ cd /home\n```";
627 let ctx4 = LintContext::new(mixed_output_first, crate::config::MarkdownFlavor::Standard, None);
628 let result4 = rule.check(&ctx4).unwrap();
629 assert_eq!(result4.len(), 1, "Mixed block should be flagged once");
630 assert!(
631 result4[0].message.contains("echo hello"),
632 "Message should mention 'echo hello'. Got: {}",
633 result4[0].message
634 );
635 }
636
637 #[test]
638 fn test_default_config_section() {
639 let rule = MD014CommandsShowOutput::new();
640 let config_section = rule.default_config_section();
641 assert!(config_section.is_some());
642 let (name, _value) = config_section.unwrap();
643 assert_eq!(name, "MD014");
644 }
645}