From 75b3d86447b9498b5b87d9db4b59825c66a501a8 Mon Sep 17 00:00:00 2001 From: Jason More Date: Mon, 8 Feb 2021 15:44:38 -0600 Subject: [PATCH 1/3] multi line comment check --- lib/internal/utils.js | 12 ++++++++++++ lib/rules/require-description.js | 11 +++++++++-- tests/lib/rules/require-description.js | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/internal/utils.js b/lib/internal/utils.js index 849bb04..f4edad5 100644 --- a/lib/internal/utils.js +++ b/lib/internal/utils.js @@ -135,6 +135,18 @@ module.exports = { description, } }, + + /** + * Creates an object hashmap + * @param {Object[]} values + * @param {Function} getKey + */ + keyBy(values, getKey){ + return values.reduce((accumulator, value) => { + accumulator[getKey(value)] = value; + return accumulator; + }, {}); + } } /** diff --git a/lib/rules/require-description.js b/lib/rules/require-description.js index 3a03c6e..b6fffca 100644 --- a/lib/rules/require-description.js +++ b/lib/rules/require-description.js @@ -52,9 +52,16 @@ module.exports = { (context.options[0] && context.options[0].ignore) || [] ) + const comments = sourceCode.getAllComments() + const commentMapByLine = utils.keyBy(comments, comment => comment.loc.end.line) + + function previousLineHasComment(comment){ + return !!commentMapByLine[comment.loc.start.line -1] + } + return { Program() { - for (const comment of sourceCode.getAllComments()) { + for (const comment of comments) { const directiveComment = utils.parseDirectiveComment( comment ) @@ -64,7 +71,7 @@ module.exports = { if (ignores.has(directiveComment.kind)) { continue } - if (!directiveComment.description) { + if (!directiveComment.description && !previousLineHasComment(comment)) { context.report({ loc: utils.toForceLocation(comment.loc), message: diff --git a/tests/lib/rules/require-description.js b/tests/lib/rules/require-description.js index 5813dc9..3bbbac7 100644 --- a/tests/lib/rules/require-description.js +++ b/tests/lib/rules/require-description.js @@ -31,6 +31,8 @@ tester.run("require-description", rule, { "/* eslint-disable-next-line -- description */", "// eslint-disable-line eqeqeq -- description", "// eslint-disable-next-line eqeqeq -- description", + `// some comment above +// eslint-disable-next-line eqeqeq`, { code: "/* eslint */", options: [{ ignore: ["eslint"] }], From d7d868d1f5049e9af283994d221c3151050d27b6 Mon Sep 17 00:00:00 2001 From: Jason More Date: Mon, 8 Feb 2021 15:46:45 -0600 Subject: [PATCH 2/3] docs --- docs/rules/require-description.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/rules/require-description.md b/docs/rules/require-description.md index 68ca472..257ddb4 100644 --- a/docs/rules/require-description.md +++ b/docs/rules/require-description.md @@ -33,6 +33,9 @@ Examples of :+1: **correct** code for this rule: /* eslint-env browser -- This script works in browser. */ // eslint-disable-next-line -- Temporarily avoids the lint error problem. See issue XXX. /* global $ -- This script using jQuery. */ + +// Temporarily avoids the lint error problem. See issue XXX. +// eslint-disable-next-line " /> ## Options From 761eae1912336d40d8702f2a9287b7da600ada08 Mon Sep 17 00:00:00 2001 From: Jason More Date: Mon, 15 Feb 2021 12:19:58 -0600 Subject: [PATCH 3/3] lint --- lib/internal/utils.js | 14 +++++++------- lib/rules/require-description.js | 14 ++++++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/internal/utils.js b/lib/internal/utils.js index f4edad5..c095114 100644 --- a/lib/internal/utils.js +++ b/lib/internal/utils.js @@ -138,15 +138,15 @@ module.exports = { /** * Creates an object hashmap - * @param {Object[]} values - * @param {Function} getKey + * @param {Object[]} values + * @param {Function} getKey */ - keyBy(values, getKey){ + keyBy(values, getKey) { return values.reduce((accumulator, value) => { - accumulator[getKey(value)] = value; - return accumulator; - }, {}); - } + accumulator[getKey(value)] = value + return accumulator + }, {}) + }, } /** diff --git a/lib/rules/require-description.js b/lib/rules/require-description.js index b6fffca..179fbc8 100644 --- a/lib/rules/require-description.js +++ b/lib/rules/require-description.js @@ -53,10 +53,13 @@ module.exports = { ) const comments = sourceCode.getAllComments() - const commentMapByLine = utils.keyBy(comments, comment => comment.loc.end.line) + const commentMapByLine = utils.keyBy( + comments, + comment => comment.loc.end.line + ) - function previousLineHasComment(comment){ - return !!commentMapByLine[comment.loc.start.line -1] + function previousLineHasComment(comment) { + return Boolean(commentMapByLine[comment.loc.start.line - 1]) } return { @@ -71,7 +74,10 @@ module.exports = { if (ignores.has(directiveComment.kind)) { continue } - if (!directiveComment.description && !previousLineHasComment(comment)) { + if ( + !directiveComment.description && + !previousLineHasComment(comment) + ) { context.report({ loc: utils.toForceLocation(comment.loc), message: