|
| 1 | +import type { Rule } from 'eslint'; |
| 2 | +import type { SourceLocation } from 'estree'; |
| 3 | + |
| 4 | +const RE = /^\s*#(region|endregion)(?:$|\s+(.*)$)/; |
| 5 | + |
| 6 | +interface CommentInfo { |
| 7 | + type: 'start' | 'end' |
| 8 | + regionName?: string |
| 9 | + loc: SourceLocation |
| 10 | +} |
| 11 | + |
| 12 | +const regionComment = { |
| 13 | + meta: { |
| 14 | + type: 'problem', |
| 15 | + docs: { |
| 16 | + description: 'Ensures that every region comment has a corresponding endregion comment' |
| 17 | + } |
| 18 | + }, |
| 19 | + create: context => ({ |
| 20 | + Program() { |
| 21 | + const lineComments = context.sourceCode |
| 22 | + .getAllComments() |
| 23 | + .reduce<CommentInfo[]>((res, comment) => { |
| 24 | + if (comment.type !== 'Line') return res; |
| 25 | + const match = RE.exec(comment.value); |
| 26 | + if (match == null) return res; |
| 27 | + |
| 28 | + const [, marker, name] = match; |
| 29 | + |
| 30 | + return [ |
| 31 | + ...res, |
| 32 | + { |
| 33 | + loc: comment.loc!, |
| 34 | + regionName: name?.trim(), |
| 35 | + type: marker === 'region' ? 'start' : 'end', |
| 36 | + } |
| 37 | + ]; |
| 38 | + }, []); |
| 39 | + |
| 40 | + const missingRegionNames = lineComments.filter(({ regionName }) => regionName === undefined); |
| 41 | + if (missingRegionNames.length > 0) { |
| 42 | + for (const comment of missingRegionNames) { |
| 43 | + context.report({ |
| 44 | + message: `Expected region name for #${comment.type === 'start' ? '' : 'end'}region comment`, |
| 45 | + loc: comment.loc |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + function searchForClosing(parentComment: CommentInfo): number | null { |
| 51 | + let level = 1; |
| 52 | + for (let i = 0; i < lineComments.length; i++) { |
| 53 | + const commentInfo = lineComments[i]; |
| 54 | + if ( |
| 55 | + commentInfo.regionName !== undefined && |
| 56 | + commentInfo.regionName === parentComment.regionName |
| 57 | + ) { |
| 58 | + if (commentInfo.type === 'start') level++; |
| 59 | + else level--; |
| 60 | + } |
| 61 | + |
| 62 | + if (level === 0) return i; |
| 63 | + } |
| 64 | + |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + while (lineComments.length > 0) { |
| 69 | + const comment = lineComments.shift()!; |
| 70 | + if (comment.regionName === undefined) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if (comment.type === 'end') { |
| 75 | + context.report({ |
| 76 | + message: '#endregion comment missing #region', |
| 77 | + loc: comment.loc |
| 78 | + }); |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + const endIndex = searchForClosing(comment); |
| 83 | + if (endIndex === null) { |
| 84 | + context.report({ |
| 85 | + message: 'Missing #endregion for #region', |
| 86 | + loc: comment.loc |
| 87 | + }); |
| 88 | + } else { |
| 89 | + lineComments.splice(endIndex, 1); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }) |
| 94 | +} satisfies Rule.RuleModule; |
| 95 | + |
| 96 | +export default regionComment; |
0 commit comments