Open History to see the last commit
1import { defineRule, type ESTree } from "@oxlint/plugins";
2
3const TS_FILE_PATTERN = /\.[cm]?tsx?$/u;
4const DIRECTIVE_PATTERN =
5 /^\s*(?:(?:oxlint|eslint)-|@ts-|@effect-diagnostics|prettier-ignore|biome-ignore|c8 |istanbul |#(?:end)?region)/u;
6const DIVIDER_LINE_PATTERN = /^(?:[-=*#~_/+]{3,}.*|.*[-=*#~_]{3,})$/u;
7const TEST_STEP_PATTERN = /^(?:arrange|act|assert|given|when|then)\s*[:.]?$/iu;
8const TASK_MARKER_PATTERN = /\b(?:TODO|FIXME|XXX|HACK)\b/u;
9const TASK_LINK_PATTERN = /#\d+|https?:\/\//u;
10const JSDOC_TYPE_PATTERN = /^@(?:param|returns?|type|typedef)\s+\{/u;
11const EMPTY_JSDOC_TAG_PATTERN = /^@(?:param(?:\s+\[?[\w$.]+\]?)?|returns?)$/u;
12const COMMENTED_CODE_PATTERN =
13 /^(?:(?:(?:const|let|var)\s+[\w${}[\], ]+=.*|import\s.+\sfrom\s+["'].+["']|(?:await\s+|return\s+)?[\w$.]+\(.*\)|[\w$.[\]]+\s*=\s*.+);|(?:if|for|while|switch)\s*\(.*\)\s*\{|\}(?:\s*else\s*\{)?)$/u;
14const DECLARATION_PATTERN =
15 /^\s*(?:export\s+)?(?:default\s+)?(?:declare\s+)?(?:async\s+)?(?:static\s+)?(?:readonly\s+)?(?:(?:const|let|var|function\*?|class|interface|type|enum)\s+)?([A-Za-z_$][\w$]*)\s*[?!]?\s*[:=(<{]/u;
16const FILLER_WORDS = new Set(
17 "a an the of for to in on at by with from and or is are be this that it its as get set return create new current value given whether".split(
18 " ",
19 ),
20);
21
22const stem = (word: string) => (word.length > 3 ? word.replace(/([^s])s$/u, "$1") : word);
23const words = (text: string) =>
24 (text.toLowerCase().match(/[a-z0-9]+/gu) ?? [])
25 .filter((word) => !FILLER_WORDS.has(word))
26 .map(stem)
27 .filter((word) => !FILLER_WORDS.has(word));
28const identifierWords = (name: string) =>
29 words(name.replace(/([a-z0-9])([A-Z])/gu, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/gu, "$1 $2"));
30
31const commentLines = (comment: ESTree.Comment) =>
32 comment.value
33 .split("\n")
34 .map((line) => line.replace(/^\s*\*?\s?/u, "").trim())
35 .filter((line) => line !== "");
36
37export default defineRule({
38 meta: {
39 type: "suggestion",
40 docs: {
41 description: "Reject comments that only repeat the code, divide sections, or narrate tests.",
42 },
43 },
44 create(context) {
45 const { sourceCode } = context;
46 const isTypeScript = TS_FILE_PATTERN.test(context.filename);
47
48 // Consecutive `//` lines at one indent read as one comment.
49 const groups: Array<{ comments: Array<ESTree.Comment>; lines: Array<string> }> = [];
50 for (const comment of sourceCode.getAllComments()) {
51 const previous = groups.at(-1)?.comments.at(-1);
52 const continues =
53 comment.type === "Line" &&
54 previous?.type === "Line" &&
55 previous.loc.end.line === comment.loc.start.line - 1 &&
56 previous.loc.start.column === comment.loc.start.column;
57 if (continues) {
58 groups.at(-1)!.comments.push(comment);
59 groups.at(-1)!.lines.push(...commentLines(comment));
60 } else {
61 groups.push({ comments: [comment], lines: commentLines(comment) });
62 }
63 }
64
65 const restatesNextName = (lastLine: number, lines: ReadonlyArray<string>) => {
66 if (lines.some((line) => line.startsWith("@"))) return false;
67 const name = DECLARATION_PATTERN.exec(sourceCode.lines[lastLine] ?? "")?.[1];
68 if (name === undefined) return false;
69 const content = words(lines.join(" "));
70 if (content.length === 0 || content.length > 6) return false;
71 const nameWords = new Set(identifierWords(name));
72 return content.every((word) => nameWords.has(word));
73 };
74
75 const problem = (comments: ReadonlyArray<ESTree.Comment>, lines: ReadonlyArray<string>) => {
76 const text = comments.map((comment) => comment.value).join("\n");
77 if (lines.length === 0 || comments.some((comment) => DIRECTIVE_PATTERN.test(comment.value))) {
78 return undefined;
79 }
80 const isLine = comments[0]!.type === "Line";
81 if (lines.some((line) => DIVIDER_LINE_PATTERN.test(line))) {
82 return "Drop the section divider; split the file or let the code's structure show the grouping.";
83 }
84 if (isLine && lines.length === 1 && TEST_STEP_PATTERN.test(lines[0]!)) {
85 return "Drop the test step label.";
86 }
87 if (TASK_MARKER_PATTERN.test(text) && !TASK_LINK_PATTERN.test(text)) {
88 return "Link the issue that tracks this, or drop the marker.";
89 }
90 if (isTypeScript && lines.some((line) => JSDOC_TYPE_PATTERN.test(line))) {
91 return "Drop the JSDoc type; TypeScript already has it.";
92 }
93 if (lines.some((line) => EMPTY_JSDOC_TAG_PATTERN.test(line))) {
94 return "Drop the JSDoc tag that has no description.";
95 }
96 if (isLine && lines.every((line) => COMMENTED_CODE_PATTERN.test(line))) {
97 return "Delete commented-out code; git keeps the history.";
98 }
99 if (restatesNextName(comments.at(-1)!.loc.end.line, lines)) {
100 return "Drop the comment; it only repeats the name below it. Say why, or say nothing.";
101 }
102 return undefined;
103 };
104
105 return {
106 Program() {
107 for (const { comments, lines } of groups) {
108 const message = problem(comments, lines);
109 if (message === undefined) continue;
110 context.report({
111 loc: { start: comments[0]!.loc.start, end: comments.at(-1)!.loc.end },
112 message,
113 });
114 }
115 },
116 };
117 },
118});