Skip to content

Commit

Permalink
expiring-todo-comments: Add date option (#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Jan 13, 2022
1 parent 3286381 commit 16bc33a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
33 changes: 33 additions & 0 deletions docs/rules/expiring-todo-comments.md
Expand Up @@ -325,3 +325,36 @@ As an example of this option, if you want this rule to **completely ignore** com
}
]
```

### date

Type: `string` (`date` format)\
Default: `<today>`

For TODOs with date deadlines, this option makes them trigger only if the deadline is later than the specified date. You could set this to a date in the future to find TODOs that expire soon, or set it far in the past if you want to ignore recently-expired TODOs.

The format must match [json-schema's date](https://json-schema.org/understanding-json-schema/reference/string.html#dates-and-times).

### examples

Find tech debt that has grown up and gone to college by triggering the rule only for incredibly old TODOs:

```js
"unicorn/expiring-todo-comments": [
"error",
{
"date": "2000-01-01"
}
]
```

Prepare for the future by triggering the rule on known Y3K bugs:

```js
"unicorn/expiring-todo-comments": [
"error",
{
"date": "3000-01-01"
}
]
```
14 changes: 9 additions & 5 deletions rules/expiring-todo-comments.js
Expand Up @@ -186,8 +186,7 @@ function parseTodoMessage(todoString) {
return afterArguments;
}

function reachedDate(past) {
const now = new Date().toISOString().slice(0, 10);
function reachedDate(past, now) {
return Date.parse(past) < Date.parse(now);
}

Expand Down Expand Up @@ -253,6 +252,7 @@ const create = context => {
ignore: [],
ignoreDatesOnPullRequests: true,
allowWarningComments: true,
date: new Date().toISOString().slice(0, 10),
...context.options[0],
};

Expand Down Expand Up @@ -329,15 +329,15 @@ const create = context => {
});
} else if (dates.length === 1) {
uses++;
const [date] = dates;
const [expirationDate] = dates;

const shouldIgnore = options.ignoreDatesOnPullRequests && ci.isPR;
if (!shouldIgnore && reachedDate(date)) {
if (!shouldIgnore && reachedDate(expirationDate, options.date)) {
context.report({
loc: comment.loc,
messageId: MESSAGE_ID_EXPIRED_TODO,
data: {
expirationDate: date,
expirationDate,
message: parseTodoMessage(comment.value),
},
});
Expand Down Expand Up @@ -536,6 +536,10 @@ const schema = [
type: 'boolean',
default: false,
},
date: {
type: 'string',
format: 'date',
},
},
},
];
Expand Down
9 changes: 9 additions & 0 deletions test/expiring-todo-comments.mjs
Expand Up @@ -110,6 +110,10 @@ test({
code: '// TODO [Issue-123] fix later',
options: [{allowWarningComments: false, ignore: [/issue-\d+/i]}],
},
{
code: '// TODO [2001-01-01]: quite old',
options: [{date: '2000-01-01'}],
},
],
invalid: [
{
Expand Down Expand Up @@ -387,5 +391,10 @@ test({
noWarningCommentError('TODO Invalid'),
],
},
{
code: '// TODO [2999-12-01]: Y3K bug',
options: [{date: '3000-01-01', ignoreDatesOnPullRequests: false}],
errors: [expiredTodoError('2999-12-01', 'Y3K bug')],
},
],
});

0 comments on commit 16bc33a

Please sign in to comment.