|
| 1 | +import { addError, filterTokens } from 'markdownlint-rule-helpers' |
| 2 | + |
| 3 | +export const codeAnnotationCommentSpacing = { |
| 4 | + names: ['GHD045', 'code-annotation-comment-spacing'], |
| 5 | + description: |
| 6 | + 'Code comments in annotation blocks must have exactly one space after the comment character(s)', |
| 7 | + tags: ['code', 'comments', 'annotate', 'spacing'], |
| 8 | + parser: 'markdownit', |
| 9 | + function: (params, onError) => { |
| 10 | + filterTokens(params, 'fence', (token) => { |
| 11 | + if (!token.info.includes('annotate')) return |
| 12 | + |
| 13 | + const lines = token.content.split('\n') |
| 14 | + |
| 15 | + lines.forEach((line, index) => { |
| 16 | + const trimmedLine = line.trim() |
| 17 | + if (!trimmedLine) return |
| 18 | + |
| 19 | + // Define a map of comment patterns |
| 20 | + const commentPatterns = { |
| 21 | + '//': /^(\/\/)(.*)/, // JavaScript/TypeScript/Java/C# style comments |
| 22 | + '#': /^(#)(.*)/, // Python/Ruby/Shell/YAML style comments |
| 23 | + '--': /^(--)(.*)/, // SQL/Lua style comments |
| 24 | + } |
| 25 | + |
| 26 | + // Check for different comment patterns |
| 27 | + let commentMatch = null |
| 28 | + let commentChar = null |
| 29 | + let restOfLine = null |
| 30 | + |
| 31 | + // Iterate over the map to find a matching comment style |
| 32 | + for (const [char, pattern] of Object.entries(commentPatterns)) { |
| 33 | + if (trimmedLine.startsWith(char)) { |
| 34 | + commentMatch = trimmedLine.match(pattern) |
| 35 | + commentChar = char |
| 36 | + restOfLine = commentMatch ? commentMatch[2] : '' |
| 37 | + break |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (commentMatch && restOfLine !== null) { |
| 42 | + // Skip shebang lines (#!/...) |
| 43 | + if (trimmedLine.startsWith('#!')) { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + // Allow empty comments or comments with exactly one space |
| 48 | + if (restOfLine === '' || restOfLine.startsWith(' ')) { |
| 49 | + // If it starts with a space, make sure it's exactly one space |
| 50 | + if (restOfLine.startsWith(' ') && restOfLine.length > 1 && restOfLine[1] === ' ') { |
| 51 | + // Multiple spaces - this is an error |
| 52 | + const lineNumber = token.lineNumber + index + 1 |
| 53 | + const fixedLine = line.replace( |
| 54 | + new RegExp(`^(\\s*${commentChar.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})\\s+`), |
| 55 | + `$1 `, |
| 56 | + ) |
| 57 | + |
| 58 | + addError( |
| 59 | + onError, |
| 60 | + lineNumber, |
| 61 | + `Comment must have exactly one space after '${commentChar}', found multiple spaces`, |
| 62 | + line, |
| 63 | + [1, line.length], |
| 64 | + { |
| 65 | + lineNumber, |
| 66 | + editColumn: 1, |
| 67 | + deleteCount: line.length, |
| 68 | + insertText: fixedLine, |
| 69 | + }, |
| 70 | + ) |
| 71 | + } |
| 72 | + // Single space or empty - this is correct |
| 73 | + return |
| 74 | + } else { |
| 75 | + // No space after comment character - this is an error |
| 76 | + const lineNumber = token.lineNumber + index + 1 |
| 77 | + const leadingWhitespace = line.match(/^\s*/)[0] |
| 78 | + const fixedLine = leadingWhitespace + commentChar + ' ' + restOfLine |
| 79 | + |
| 80 | + addError( |
| 81 | + onError, |
| 82 | + lineNumber, |
| 83 | + `Comment must have exactly one space after '${commentChar}'`, |
| 84 | + line, |
| 85 | + [1, line.length], |
| 86 | + { |
| 87 | + lineNumber, |
| 88 | + editColumn: 1, |
| 89 | + deleteCount: line.length, |
| 90 | + insertText: fixedLine, |
| 91 | + }, |
| 92 | + ) |
| 93 | + } |
| 94 | + } |
| 95 | + }) |
| 96 | + }) |
| 97 | + }, |
| 98 | +} |
0 commit comments