Skip to content

Commit

Permalink
Update: added the comment in no-warning-comments (fixes eslint#12327)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed Jul 22, 2020
1 parent f3a19d2 commit dbaeef1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 38 deletions.
14 changes: 10 additions & 4 deletions lib/rules/no-warning-comments.js
Expand Up @@ -42,7 +42,7 @@ module.exports = {
],

messages: {
unexpectedComment: "Unexpected '{{matchedTerm}}' comment."
unexpectedComment: "Unexpected '{{type}}' comment: {{comment}}."
}
},

Expand Down Expand Up @@ -122,7 +122,7 @@ module.exports = {

warningRegExps.forEach((regex, index) => {
if (regex.test(comment)) {
matches.push(warningTerms[index]);
matches.push({ type: warningTerms[index], comment });
}
});

Expand All @@ -141,12 +141,18 @@ module.exports = {

const matches = commentContainsWarningTerm(node.value);

matches.forEach(matchedTerm => {
matches.forEach(({ type, comment }) => {
let commentToDisplay = comment.trim();

if (commentToDisplay.length >= 30) {
commentToDisplay = `${commentToDisplay.slice(0, 30)}..`;
}
context.report({
node,
messageId: "unexpectedComment",
data: {
matchedTerm
type,
comment: `${commentToDisplay}`
}
});
});
Expand Down
82 changes: 48 additions & 34 deletions tests/lib/rules/no-warning-comments.js
Expand Up @@ -43,165 +43,179 @@ ruleTester.run("no-warning-comments", rule, {
{
code: "// fixme",
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "fixme" } }
]
},
{
code: "// any fixme",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any fixme" } }
]
},
{
code: "// any fixme",
options: [{ terms: ["fixme"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any fixme" } }
]
},
{
code: "// any FIXME",
options: [{ terms: ["fixme"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any FIXME" } }
]
},
{
code: "// any fIxMe",
options: [{ terms: ["fixme"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any fIxMe" } }
]
},
{
code: "/* any fixme */",
options: [{ terms: ["FIXME"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "FIXME" } }
{ messageId: "unexpectedComment", data: { type: "FIXME", comment: "any fixme" } }
]
},
{
code: "/* any FIXME */",
options: [{ terms: ["FIXME"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "FIXME" } }
{ messageId: "unexpectedComment", data: { type: "FIXME", comment: "any FIXME" } }
]
},
{
code: "/* any fIxMe */",
options: [{ terms: ["FIXME"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "FIXME" } }
{ messageId: "unexpectedComment", data: { type: "FIXME", comment: "any fIxMe" } }
]
},
{
code: "// any fixme or todo",
options: [{ terms: ["fixme", "todo"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "todo" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any fixme or todo" } },
{ messageId: "unexpectedComment", data: { type: "todo", comment: "any fixme or todo" } }
]
},
{
code: "/* any fixme or todo */",
options: [{ terms: ["fixme", "todo"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "todo" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any fixme or todo" } },
{ messageId: "unexpectedComment", data: { type: "todo", comment: "any fixme or todo" } }
]
},
{
code: "/* any fixme or todo */",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "todo" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "todo", comment: "any fixme or todo" } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any fixme or todo" } }
]
},
{
code: "/* fixme and todo */",
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "fixme and todo" } }
]
},
{
code: "/* fixme and todo */",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "todo" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "todo", comment: "fixme and todo" } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "fixme and todo" } }
]
},
{
code: "/* any fixme */",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any fixme" } }
]
},
{
code: "/* fixme! */",
options: [{ terms: ["fixme"] }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } }
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "fixme!" } }
]
},
{
code: "// regex [litera|$]",
options: [{ terms: ["[litera|$]"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "[litera|$]" } }
{ messageId: "unexpectedComment", data: { type: "[litera|$]", comment: "regex [litera|$]" } }
]
},
{
code: "/* eslint one-var: 2 */",
options: [{ terms: ["eslint"] }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "eslint" } }
{ messageId: "unexpectedComment", data: { type: "eslint", comment: "eslint one-var: 2" } }
]
},
{
code: "/* eslint one-var: 2 */",
options: [{ terms: ["one"], location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "one" } }
{ messageId: "unexpectedComment", data: { type: "one", comment: "eslint one-var: 2" } }
]
},
{
code: "/* any block comment with TODO, FIXME or XXX */",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "todo" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "xxx" } }
{ messageId: "unexpectedComment", data: { type: "todo", comment: "any block comment with TODO, F.." } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any block comment with TODO, F.." } },
{ messageId: "unexpectedComment", data: { type: "xxx", comment: "any block comment with TODO, F.." } }
]
},
{
code: "/* any block comment with (TODO, FIXME's or XXX!) */",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "todo" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "xxx" } }
{ messageId: "unexpectedComment", data: { type: "todo", comment: "any block comment with (TODO, .." } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any block comment with (TODO, .." } },
{ messageId: "unexpectedComment", data: { type: "xxx", comment: "any block comment with (TODO, .." } }
]
},
{
code: "/** \n *any block comment \n*with (TODO, FIXME's or XXX!) **/",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "todo" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "xxx" } }
{ messageId: "unexpectedComment", data: { type: "todo", comment: "* \n *any block comment \n*with .." } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "* \n *any block comment \n*with .." } },
{ messageId: "unexpectedComment", data: { type: "xxx", comment: "* \n *any block comment \n*with .." } }
]
},
{
code: "// any comment with TODO, FIXME or XXX",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { matchedTerm: "todo" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "fixme" } },
{ messageId: "unexpectedComment", data: { matchedTerm: "xxx" } }
{ messageId: "unexpectedComment", data: { type: "todo", comment: "any comment with TODO, FIXME o.." } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any comment with TODO, FIXME o.." } },
{ messageId: "unexpectedComment", data: { type: "xxx", comment: "any comment with TODO, FIXME o.." } }
]
},
{
code: "// TODO: something small",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { type: "todo", comment: "TODO: something small" } }
]
},
{
code: "// TODO: something really longer than 30 characters",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { type: "todo", comment: "TODO: something really longer .." } }
]
}
]
Expand Down

0 comments on commit dbaeef1

Please sign in to comment.