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

Update: add comment to message in no-warning-comments (fixes #12327) #13522

Merged
merged 8 commits into from Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 23 additions & 4 deletions lib/rules/no-warning-comments.js
Expand Up @@ -8,6 +8,8 @@
const { escapeRegExp } = require("lodash");
const astUtils = require("./utils/ast-utils");

const CHAR_LIMIT = 40;

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -42,7 +44,7 @@ module.exports = {
],

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

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

warningRegExps.forEach((regex, index) => {
if (regex.test(comment)) {
matches.push(warningTerms[index]);
matches.push({ type: warningTerms[index], comment });
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
}
});

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

const matches = commentContainsWarningTerm(node.value);

matches.forEach(matchedTerm => {
matches.forEach(({ type, comment }) => {
let len = 0;
const commentsToDisplay = [];
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
let truncated = false;

for (const c of comment.split(/\s+/u)) {
if (c.length + len <= CHAR_LIMIT) {
commentsToDisplay.push(c);
} else {
truncated = true;
break;
}

len += c.length;
}


context.report({
node,
messageId: "unexpectedComment",
data: {
matchedTerm
type,
comment: `${commentsToDisplay.join(" ").trim()}${truncated ? "..." : ""}`
}
});
});
Expand Down
109 changes: 75 additions & 34 deletions tests/lib/rules/no-warning-comments.js
Expand Up @@ -43,165 +43,206 @@ 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, FIXME or XXX" } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any block comment with TODO, FIXME or XXX" } },
{ messageId: "unexpectedComment", data: { type: "xxx", comment: "any block comment with TODO, FIXME or XXX" } }
]
},
{
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, FIXME's or XXX!)" } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any block comment with (TODO, FIXME's or XXX!)" } },
{ messageId: "unexpectedComment", data: { type: "xxx", comment: "any block comment with (TODO, FIXME's or XXX!)" } }
]
},
{
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: "* *any block comment *with (TODO, FIXME's or..." } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "* *any block comment *with (TODO, FIXME's or..." } },
{ messageId: "unexpectedComment", data: { type: "xxx", comment: "* *any block comment *with (TODO, FIXME's or..." } }
]
},
{
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 or XXX" } },
{ messageId: "unexpectedComment", data: { type: "fixme", comment: "any comment with TODO, FIXME or XXX" } },
{ messageId: "unexpectedComment", data: { type: "xxx", comment: "any comment with TODO, FIXME or XXX" } }
]
},
{
code: "// TODO: something small",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { type: "todo", comment: "TODO: something small" } }
]
},
{
code: "// TODO: something really longer than 40 characters",
options: [{ location: "anywhere" }],
errors: [
{ messageId: "unexpectedComment", data: { type: "todo", comment: "TODO: something really longer than 40..." } }
]
},
{
code:
"/* TODO: something \n really longer than 40 characters \n and also a new line */",
options: [{ location: "anywhere" }],
errors: [
{
messageId: "unexpectedComment",
data: {
type: "todo",
comment: "TODO: something really longer than 40..."
}
}
]
},
{
code: "// TODO: small",
options: [{ location: "anywhere" }],
errors: [
{
messageId: "unexpectedComment",
data: {
type: "todo",
comment: "TODO: small"
}
}
]
}
]
Expand Down