Skip to content

Commit 242202f

Browse files
committed
fix(newline-afer-description): when finding last description line, 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.
1 parent 2fd8ecc commit 242202f

File tree

3 files changed

+97
-6
lines changed

3 files changed

+97
-6
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,6 +4326,18 @@ function quux () {
43264326
// Options: ["always"]
43274327
// Message: There must be a newline after the description of the JSDoc block.
43284328
4329+
/**
4330+
* Foo.
4331+
* @foo
4332+
*
4333+
* Foo.
4334+
*/
4335+
function quux () {
4336+
4337+
}
4338+
// Options: ["always"]
4339+
// Message: There must be a newline after the description of the JSDoc block.
4340+
43294341
/**
43304342
* Foo.
43314343
*
@@ -4346,6 +4358,19 @@ function quux () {
43464358
*/
43474359
function quux () {
43484360
4361+
}
4362+
// Options: ["never"]
4363+
// Message: There must be no newline after the description of the JSDoc block.
4364+
4365+
/**
4366+
* Bar.
4367+
*
4368+
* @bar
4369+
*
4370+
* Bar.
4371+
*/
4372+
function quux () {
4373+
43494374
}
43504375
// Options: ["never"]
43514376
// Message: There must be no newline after the description of the JSDoc block.

src/rules/newlineAfterDescription.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ export default iterateJsdoc(({
2626
if (always) {
2727
if (!descriptionEndsWithANewline) {
2828
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
29-
const lastDescriptionLine = _.findLastIndex(sourceLines, (line) => {
30-
return line.replace(/^\s*\*\s*/u, '') === _.last(jsdoc.description.split('\n'));
31-
});
29+
const splitDesc = jsdoc.description.split('\n');
30+
const lastDescriptionLine = splitDesc.length - 1;
3231
report('There must be a newline after the description of the JSDoc block.', (fixer) => {
3332
// Add the new line
3433
const injectedLine = `${indent} *` +
@@ -42,9 +41,8 @@ export default iterateJsdoc(({
4241
}
4342
} else if (descriptionEndsWithANewline) {
4443
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
45-
const lastDescriptionLine = _.findLastIndex(sourceLines, (line) => {
46-
return line.replace(/^\s*\*\s*/u, '') === _.last(jsdoc.description.split('\n'));
47-
});
44+
const splitDesc = jsdoc.description.split('\n');
45+
const lastDescriptionLine = splitDesc.length - 1;
4846
report('There must be no newline after the description of the JSDoc block.', (fixer) => {
4947
// Remove the extra line
5048
sourceLines.splice(lastDescriptionLine, 1);

test/rules/assertions/newlineAfterDescription.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,40 @@ export default {
3434
}
3535
`,
3636
},
37+
{
38+
code: `
39+
/**
40+
* Foo.
41+
* @foo
42+
*
43+
* Foo.
44+
*/
45+
function quux () {
46+
47+
}
48+
`,
49+
errors: [
50+
{
51+
line: 3,
52+
message: 'There must be a newline after the description of the JSDoc block.',
53+
},
54+
],
55+
options: [
56+
'always',
57+
],
58+
output: `
59+
/**
60+
* Foo.
61+
*
62+
* @foo
63+
*
64+
* Foo.
65+
*/
66+
function quux () {
67+
68+
}
69+
`,
70+
},
3771
{
3872
code: `
3973
/**
@@ -99,6 +133,40 @@ export default {
99133
}
100134
`,
101135
},
136+
{
137+
code: `
138+
/**
139+
* Bar.
140+
*
141+
* @bar
142+
*
143+
* Bar.
144+
*/
145+
function quux () {
146+
147+
}
148+
`,
149+
errors: [
150+
{
151+
line: 4,
152+
message: 'There must be no newline after the description of the JSDoc block.',
153+
},
154+
],
155+
options: [
156+
'never',
157+
],
158+
output: `
159+
/**
160+
* Bar.
161+
* @bar
162+
*
163+
* Bar.
164+
*/
165+
function quux () {
166+
167+
}
168+
`,
169+
},
102170
{
103171
code: `\r
104172
/**\r

0 commit comments

Comments
 (0)