Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expiring-todo-comments: Add date option #1683

Merged
merged 4 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/rules/expiring-todo-comments.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -249,6 +248,7 @@ const create = context => {
ignore: [],
ignoreDatesOnPullRequests: true,
allowWarningComments: true,
date: new Date().toISOString().slice(0, 10),
...context.options[0],
};

Expand Down Expand Up @@ -325,15 +325,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 @@ -531,6 +531,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
Original file line number Diff line number Diff line change
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')],
},
],
});