Skip to content

Commit

Permalink
fix(newline-afer-description): when finding last description line, …
Browse files Browse the repository at this point in the history
…don't look beyond the length of the description.

The previous check for the last description line was redundant and problematic.
The search for the last line was mistakenly checking for the last match within the full comment, so if the last line of the
description were duplicated later, that position was used, and since that position couldn't be find within the
`comment-parser` description, there would be an error. It was also mistakenly stripping carriage returns in part
of the comparison when there could be carriage returns in the other part.
  • Loading branch information
brettz9 committed Nov 25, 2019
1 parent 2fd8ecc commit 242202f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
25 changes: 25 additions & 0 deletions README.md
Expand Up @@ -4326,6 +4326,18 @@ function quux () {
// Options: ["always"]
// Message: There must be a newline after the description of the JSDoc block.
/**
* Foo.
* @foo
*
* Foo.
*/
function quux () {
}
// Options: ["always"]
// Message: There must be a newline after the description of the JSDoc block.
/**
* Foo.
*
Expand All @@ -4346,6 +4358,19 @@ function quux () {
*/
function quux () {
}
// Options: ["never"]
// Message: There must be no newline after the description of the JSDoc block.
/**
* Bar.
*
* @bar
*
* Bar.
*/
function quux () {
}
// Options: ["never"]
// Message: There must be no newline after the description of the JSDoc block.
Expand Down
10 changes: 4 additions & 6 deletions src/rules/newlineAfterDescription.js
Expand Up @@ -26,9 +26,8 @@ export default iterateJsdoc(({
if (always) {
if (!descriptionEndsWithANewline) {
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
const lastDescriptionLine = _.findLastIndex(sourceLines, (line) => {
return line.replace(/^\s*\*\s*/u, '') === _.last(jsdoc.description.split('\n'));
});
const splitDesc = jsdoc.description.split('\n');
const lastDescriptionLine = splitDesc.length - 1;
report('There must be a newline after the description of the JSDoc block.', (fixer) => {
// Add the new line
const injectedLine = `${indent} *` +
Expand All @@ -42,9 +41,8 @@ export default iterateJsdoc(({
}
} else if (descriptionEndsWithANewline) {
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
const lastDescriptionLine = _.findLastIndex(sourceLines, (line) => {
return line.replace(/^\s*\*\s*/u, '') === _.last(jsdoc.description.split('\n'));
});
const splitDesc = jsdoc.description.split('\n');
const lastDescriptionLine = splitDesc.length - 1;
report('There must be no newline after the description of the JSDoc block.', (fixer) => {
// Remove the extra line
sourceLines.splice(lastDescriptionLine, 1);
Expand Down
68 changes: 68 additions & 0 deletions test/rules/assertions/newlineAfterDescription.js
Expand Up @@ -34,6 +34,40 @@ export default {
}
`,
},
{
code: `
/**
* Foo.
* @foo
*
* Foo.
*/
function quux () {
}
`,
errors: [
{
line: 3,
message: 'There must be a newline after the description of the JSDoc block.',
},
],
options: [
'always',
],
output: `
/**
* Foo.
*
* @foo
*
* Foo.
*/
function quux () {
}
`,
},
{
code: `
/**
Expand Down Expand Up @@ -99,6 +133,40 @@ export default {
}
`,
},
{
code: `
/**
* Bar.
*
* @bar
*
* Bar.
*/
function quux () {
}
`,
errors: [
{
line: 4,
message: 'There must be no newline after the description of the JSDoc block.',
},
],
options: [
'never',
],
output: `
/**
* Bar.
* @bar
*
* Bar.
*/
function quux () {
}
`,
},
{
code: `\r
/**\r
Expand Down

0 comments on commit 242202f

Please sign in to comment.