Skip to content

Commit 16bc33a

Browse files
authored
expiring-todo-comments: Add date option (#1683)
1 parent 3286381 commit 16bc33a

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

docs/rules/expiring-todo-comments.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,36 @@ As an example of this option, if you want this rule to **completely ignore** com
325325
}
326326
]
327327
```
328+
329+
### date
330+
331+
Type: `string` (`date` format)\
332+
Default: `<today>`
333+
334+
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.
335+
336+
The format must match [json-schema's date](https://json-schema.org/understanding-json-schema/reference/string.html#dates-and-times).
337+
338+
### examples
339+
340+
Find tech debt that has grown up and gone to college by triggering the rule only for incredibly old TODOs:
341+
342+
```js
343+
"unicorn/expiring-todo-comments": [
344+
"error",
345+
{
346+
"date": "2000-01-01"
347+
}
348+
]
349+
```
350+
351+
Prepare for the future by triggering the rule on known Y3K bugs:
352+
353+
```js
354+
"unicorn/expiring-todo-comments": [
355+
"error",
356+
{
357+
"date": "3000-01-01"
358+
}
359+
]
360+
```

rules/expiring-todo-comments.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ function parseTodoMessage(todoString) {
186186
return afterArguments;
187187
}
188188

189-
function reachedDate(past) {
190-
const now = new Date().toISOString().slice(0, 10);
189+
function reachedDate(past, now) {
191190
return Date.parse(past) < Date.parse(now);
192191
}
193192

@@ -253,6 +252,7 @@ const create = context => {
253252
ignore: [],
254253
ignoreDatesOnPullRequests: true,
255254
allowWarningComments: true,
255+
date: new Date().toISOString().slice(0, 10),
256256
...context.options[0],
257257
};
258258

@@ -329,15 +329,15 @@ const create = context => {
329329
});
330330
} else if (dates.length === 1) {
331331
uses++;
332-
const [date] = dates;
332+
const [expirationDate] = dates;
333333

334334
const shouldIgnore = options.ignoreDatesOnPullRequests && ci.isPR;
335-
if (!shouldIgnore && reachedDate(date)) {
335+
if (!shouldIgnore && reachedDate(expirationDate, options.date)) {
336336
context.report({
337337
loc: comment.loc,
338338
messageId: MESSAGE_ID_EXPIRED_TODO,
339339
data: {
340-
expirationDate: date,
340+
expirationDate,
341341
message: parseTodoMessage(comment.value),
342342
},
343343
});
@@ -536,6 +536,10 @@ const schema = [
536536
type: 'boolean',
537537
default: false,
538538
},
539+
date: {
540+
type: 'string',
541+
format: 'date',
542+
},
539543
},
540544
},
541545
];

test/expiring-todo-comments.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ test({
110110
code: '// TODO [Issue-123] fix later',
111111
options: [{allowWarningComments: false, ignore: [/issue-\d+/i]}],
112112
},
113+
{
114+
code: '// TODO [2001-01-01]: quite old',
115+
options: [{date: '2000-01-01'}],
116+
},
113117
],
114118
invalid: [
115119
{
@@ -387,5 +391,10 @@ test({
387391
noWarningCommentError('TODO Invalid'),
388392
],
389393
},
394+
{
395+
code: '// TODO [2999-12-01]: Y3K bug',
396+
options: [{date: '3000-01-01', ignoreDatesOnPullRequests: false}],
397+
errors: [expiredTodoError('2999-12-01', 'Y3K bug')],
398+
},
390399
],
391400
});

0 commit comments

Comments
 (0)