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

require-description: previous line comment check #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/rules/require-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}, {})
},
}

/**
Expand Down
17 changes: 15 additions & 2 deletions lib/rules/require-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,19 @@ 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 Boolean(commentMapByLine[comment.loc.start.line - 1])
}

return {
Program() {
for (const comment of sourceCode.getAllComments()) {
for (const comment of comments) {
const directiveComment = utils.parseDirectiveComment(
comment
)
Expand All @@ -64,7 +74,10 @@ module.exports = {
if (ignores.has(directiveComment.kind)) {
continue
}
if (!directiveComment.description) {
if (
!directiveComment.description &&
!previousLineHasComment(comment)
) {
context.report({
loc: utils.toForceLocation(comment.loc),
message:
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/rules/require-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add something like

/* 
  * Some multiline comment
  */
// eslint-disable-next-line eqeqeq

... as well, just to make sure it at least is covered by tests. I can see that you look at comment.loc.end.line so it should work.

{
code: "/* eslint */",
options: [{ ignore: ["eslint"] }],
Expand Down