Skip to content

Commit 7fb138a

Browse files
authored
Add commentPattern option to override the whole comment pattern (#2)
A new configuration to override the main comment pattern as a more flexible way of making this rule work with individual project requirements. For instance, the [Atlassian for VS Code](https://marketplace.visualstudio.com/items?itemName=Atlassian.atlascode) extension resolves issues in comments with the following format: `TODO: [PROJ-123]`. Also, some VS Code extensions for highlighting `TODO`s require a colon after the term (e.g. `TODO: some text`).
1 parent 8436ff1 commit 7fb138a

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

docs/rules/ticket-ref.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Require a ticket reference in the TODO comment (ticket-ref)
22

3-
Adding a `TODO` comment that will be addressed in the future should have a corresponding ticket (AKA issue) in the project backlog so the team doesn't lose track of the pending work.
3+
Adding a `TODO` comment that will be addressed in the future should have a corresponding ticket (AKA issue) in the project backlog, so the team doesn't lose track of the pending work.
44

55
## Fail 🛑
66

@@ -26,6 +26,20 @@ Examples of **correct** code for this rule:
2626

2727
## Options
2828

29+
### commentPattern
30+
31+
This option overrides the overall comment pattern that matches both term and ticket. When used, `term` and `pattern` options are ignored. Expects a regex string.
32+
33+
For example, let's say your IDE or tooling expects a different comment pattern such as `TODO: [PROJ-123]`, you would configure this rule like:
34+
35+
```json
36+
{
37+
"rules": {
38+
"todo-plz/ticket-ref": ["error", { "commentPattern": "TODO:\\s\\[(PROJ-[0-9]+[,\\s]*)+\\]" }]
39+
}
40+
}
41+
```
42+
2943
### pattern
3044

3145
This option controls what the ticket pattern is to match against. Expects a regex string.

lib/rules/ticket-ref.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
const messages = {
88
missingTicket:
99
"{{ term }} comment doesn't reference a ticket number. Ticket pattern: {{ pattern }}",
10+
missingTicketWithCommentPattern:
11+
"{{ term }} comment doesn't reference a ticket number. Comment pattern: {{ commentPattern }}",
1012
};
1113

1214
const schema = [
1315
{
1416
type: "object",
1517
properties: {
18+
commentPattern: {
19+
type: "string",
20+
},
1621
pattern: {
1722
type: "string",
1823
},
@@ -27,7 +32,7 @@ const schema = [
2732
];
2833

2934
function create(context) {
30-
const { pattern, terms } = {
35+
const { commentPattern, pattern, terms } = {
3136
terms: ["TODO"],
3237
...context.options[0],
3338
};
@@ -37,7 +42,7 @@ function create(context) {
3742

3843
terms.forEach((term) => {
3944
termSearchPatterns[term] = new RegExp(
40-
`${term}\\s?\\((${pattern}[,\\s]*)+\\)`,
45+
commentPattern || `${term}\\s?\\((${pattern}[,\\s]*)+\\)`,
4146
"i"
4247
);
4348
});
@@ -59,8 +64,8 @@ function create(context) {
5964

6065
context.report({
6166
loc: comment.loc,
62-
messageId: "missingTicket",
63-
data: { pattern, term },
67+
messageId: commentPattern ? "missingTicketWithCommentPattern" : "missingTicket",
68+
data: { commentPattern, pattern, term },
6469
});
6570
});
6671
}

tests/integration/.eslintrc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@
22
"plugins": ["todo-plz"],
33
"rules": {
44
"todo-plz/ticket-ref": ["error", { "pattern": "PROJ-[0-9]+" }]
5-
}
5+
},
6+
"overrides": [
7+
{
8+
"files": ["commentPattern.js"],
9+
"rules": {
10+
"todo-plz/ticket-ref": ["error", { "commentPattern": "TODO:\\s\\[(PROJ-[0-9]+[,\\s]*)+\\]" }]
11+
}
12+
}
13+
]
614
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// TODO: [PROJ-1] Good
2+
console.log("[Rule] ticket-ref: This is fine.");
3+
4+
// TODO (PROJ-1): Bad
5+
console.log("[Rule] ticket-ref: This is bad.");

tests/ticket-ref.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const ruleTester = new RuleTester();
55
const messages = {
66
missingTodoTicket:
77
"TODO comment doesn't reference a ticket number. Ticket pattern: PROJ-[0-9]+",
8+
missingTodoTicketWithCommentPattern:
9+
"TODO comment doesn't reference a ticket number. Comment pattern: TODO:\\s\\[(PROJ-[0-9]+[,\\s]*)+\\]",
810
missingFixmeTicket:
911
"FIXME comment doesn't reference a ticket number. Ticket pattern: PROJ-[0-9]+",
1012
};
@@ -13,6 +15,8 @@ const options = {
1315
jira: { pattern: "PROJ-[0-9]+" },
1416
};
1517

18+
const commentPattern = "TODO:\\s\\[(PROJ-[0-9]+[,\\s]*)+\\]"
19+
1620
ruleTester.run("ticket-ref", rule, {
1721
valid: [
1822
{
@@ -66,6 +70,18 @@ ruleTester.run("ticket-ref", rule, {
6670
code: "// FIXME (PROJ-2): Connect to the API",
6771
options: [{ pattern: "PROJ-[0-9]+", terms: ["FIXME"] }],
6872
},
73+
{
74+
code: "// TODO: [PROJ-2] Connect to the API",
75+
options: [{ commentPattern }],
76+
},
77+
{
78+
code: `/**
79+
* Description
80+
* TODO: [PROJ-123] Connect to the API
81+
* @returns {string}
82+
*/`,
83+
options: [{ commentPattern }],
84+
},
6985
],
7086

7187
invalid: [
@@ -105,5 +121,10 @@ ruleTester.run("ticket-ref", rule, {
105121
errors: [{ message: messages.missingFixmeTicket }],
106122
options: [{ pattern: "PROJ-[0-9]+", terms: ["FIXME"] }],
107123
},
124+
{
125+
code: "// TODO (PROJ-123) Connect to the API",
126+
errors: [{ message: messages.missingTodoTicketWithCommentPattern }],
127+
options: [{ commentPattern }],
128+
},
108129
],
109130
});

0 commit comments

Comments
 (0)