Skip to content

Commit ffa435b

Browse files
committed
chore(lint): an empty comment above a suppression is not its reason
An empty `//` or `/* */` directly above a directive counted as its explanation. Skip empty comment lines the way stacked directives are skipped, so the reason must be real text, and a blank comment line between the reason and the directive still works.
1 parent d469f4f commit ffa435b

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎oxlint-plugin-t3code/rules/require-suppression-reason.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ describe("t3code/require-suppression-reason", () => {
8787
`,
8888
);
8989

90+
rule.valid(
91+
"allows an empty comment line between the reason and the directive",
92+
`
93+
// Positions are stable for a given string.
94+
//
95+
// oxlint-disable-next-line react/no-array-index-key
96+
const items = ["a"].map((item, index) => <li key={index}>{item}</li>);
97+
`,
98+
);
99+
100+
rule.invalid(
101+
"does not count an empty comment above",
102+
`
103+
//
104+
/* */
105+
// oxlint-disable-next-line react/no-array-index-key
106+
const items = ["a"].map((item, index) => <li key={index}>{item}</li>);
107+
`,
108+
);
109+
90110
rule.invalid(
91111
"does not count a trailing comment on the code line above",
92112
`

‎oxlint-plugin-t3code/rules/require-suppression-reason.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const DIRECTIVE_PATTERN =
55
/^\s*\*?\s*(?:(?:oxlint|eslint)-(?:disable|enable)|@ts-(?:expect-error|ignore|nocheck)|@effect-diagnostics)/u;
66
// TypeScript's own directives; whatever follows one is its reason.
77
const TS_DIRECTIVE_PATTERN = /^\s*\*?\s*@ts-(?:expect-error|ignore|nocheck)(?![\w-])(.*)$/su;
8+
const EMPTY_COMMENT_PATTERN = /^[\s*]*$/u;
89

910
/**
1011
* Reports a directive that disables a lint rule or the type checker without saying why. The reason
@@ -30,10 +31,14 @@ export default defineRule({
3031
}
3132
}
3233

33-
// A comment directly above explains a directive, even across other stacked directives.
34+
// A comment directly above explains a directive, even across stacked directives and empty
35+
// comment lines.
3436
const explainedAbove = (directive: ESTree.Comment) => {
3537
let above = ownLineCommentsByEndLine.get(directive.loc.start.line - 1);
36-
while (above && DIRECTIVE_PATTERN.test(above.value)) {
38+
while (
39+
above &&
40+
(DIRECTIVE_PATTERN.test(above.value) || EMPTY_COMMENT_PATTERN.test(above.value))
41+
) {
3742
above = ownLineCommentsByEndLine.get(above.loc.start.line - 1);
3843
}
3944
return above !== undefined;

0 commit comments

Comments
 (0)