Skip to content
6 changes: 6 additions & 0 deletions rules/expiring-todo-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'node:path';
import {isRegExp} from 'node:util/types';
import semver from 'semver';
import * as ci from 'ci-info';
import parseDirective from './utils/parse-directive.js';
import getBuiltinRule from './utils/get-builtin-rule.js';
import {readPackageJson} from './shared/package-json.js';

Expand Down Expand Up @@ -319,6 +320,11 @@ const create = context => {

// eslint-disable-next-line complexity
function processComment(comment) {
const directive = parseDirective(comment);
if (directive?.isEslintDisableDirective || directive?.isEslintEnableDirective) {
return;
}

if (ignoreRegexes.some(ignore => ignore.test(comment.value))) {
return;
}
Expand Down
17 changes: 4 additions & 13 deletions rules/no-abusive-eslint-disable.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
import {ConfigCommentParser} from '@eslint/plugin-kit';
import parseDirective from './utils/parse-directive.js';

const MESSAGE_ID = 'no-abusive-eslint-disable';
const messages = {
[MESSAGE_ID]: 'Specify the rules you want to disable.',
};

// https://github.com/eslint/eslint/blob/ecd0ede7fd2ccbb4c0daf0e4732e97ea0f49db1b/lib/linter/linter.js#L509-L512
const eslintDisableDirectives = new Set([
'eslint-disable',
'eslint-disable-line',
'eslint-disable-next-line',
]);

let commentParser;
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
context.on('Program', function * (node) {
for (const comment of node.comments) {
commentParser ??= new ConfigCommentParser();
const result = commentParser.parseDirective(comment.value);
const directive = parseDirective(comment);

if (!(
// It's a eslint-disable comment
eslintDisableDirectives.has(result?.label)
directive?.isEslintDisableDirective
// But it did not specify any rules
&& !result?.value
&& !directive?.value
)) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions rules/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export {default as isShorthandImportLocal} from './is-shorthand-import-local.js'
export {default as isShorthandPropertyValue} from './is-shorthand-property-value.js';
export {default as isValueNotUsable} from './is-value-not-usable.js';
export {default as needsSemicolon} from './needs-semicolon.js';
export {default as parseDirective} from './parse-directive.js';
export {checkVueTemplate} from './rule.js';
export {default as shouldAddParenthesesToAwaitExpressionArgument} from './should-add-parentheses-to-await-expression-argument.js';
export {default as shouldAddParenthesesToCallExpressionCallee} from './should-add-parentheses-to-call-expression-callee.js';
Expand Down
42 changes: 42 additions & 0 deletions rules/utils/parse-directive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {ConfigCommentParser} from '@eslint/plugin-kit';

// https://github.com/eslint/eslint/blob/ecd0ede7fd2ccbb4c0daf0e4732e97ea0f49db1b/lib/linter/linter.js#L509-L512
const ESLINT_DISABLE_DIRECTIVES = new Set([
'eslint-disable',
'eslint-disable-line',
'eslint-disable-next-line',
]);

let configCommentParser;

/**
Parse a directive comment value and return directive meta info.

@param {ESTree.Comment} comment
@returns {{
label: string,
value: string,
justification: string,
isEslintDisableDirective: boolean,
isEslintEnableDirective: boolean,
}|undefined}
*/
export default function parseDirective(comment) {
configCommentParser ??= new ConfigCommentParser();

const result = configCommentParser.parseDirective(comment.value);

if (!result) {
return;
}

const {label} = result;
const isEslintDisableDirective = ESLINT_DISABLE_DIRECTIVES.has(label);
const isEslintEnableDirective = label === 'eslint-enable';

return {
...result,
isEslintDisableDirective,
isEslintEnableDirective,
};
}
12 changes: 12 additions & 0 deletions test/expiring-todo-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ test({
code: '// TODO [2001-01-01]: quite old',
options: [{date: '2000-01-01'}],
},
{
code: `// eslint-disable-next-line rule-to-test/expiring-todo-comments
// TODO without a date`,
options: [{allowWarningComments: false}],
},
{
code: `/* eslint-disable rule-to-test/expiring-todo-comments */
// TODO without a date
// fixme [2000-01-01]: too old'
/* eslint-enable rule-to-test/expiring-todo-comments */`,
options: [{allowWarningComments: false}],
},
],
invalid: [
{
Expand Down
Loading