Skip to content

Commit a38b28b

Browse files
committed
fix(newline-after-description): handle carriage returns properly: fixes #431
1 parent d3ca65b commit a38b28b

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4366,6 +4366,16 @@ function quux () {
43664366
*/
43674367
// Options: ["always"]
43684368
// Message: There must be a newline after the description of the JSDoc block.
4369+
4370+
4371+
/**
4372+
* Service for fetching symbols.
4373+
* @param {Object} $http - Injected http helper.
4374+
* @param {Object} $q - Injected Promise api helper.
4375+
* @param {Object} $location - Injected window location object.
4376+
* @param {Object} REPORT_DIALOG_CONSTANTS - Injected handle.
4377+
*/
4378+
// Message: There must be a newline after the description of the JSDoc block.
43694379
````
43704380
43714381
The following patterns are not considered problems:

src/rules/newlineAfterDescription.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ export default iterateJsdoc(({
2121
always = true;
2222
}
2323

24-
// The contents of the jsdoc.source and of jsdoc.description is left trimmed.
25-
// The contents of the jsdoc.description is right trimmed.
26-
// This gets the text following the description.
27-
const descriptionEndsWithANewline = jsdoc.source.slice(jsdoc.description.length).startsWith('\n\n');
24+
const descriptionEndsWithANewline = jsdoc.description.endsWith('\n');
2825

2926
if (always) {
3027
if (!descriptionEndsWithANewline) {
@@ -34,7 +31,9 @@ export default iterateJsdoc(({
3431
});
3532
report('There must be a newline after the description of the JSDoc block.', (fixer) => {
3633
// Add the new line
37-
sourceLines.splice(lastDescriptionLine + 1, 0, `${indent} *`);
34+
const injectedLine = `${indent} *` +
35+
(sourceLines[lastDescriptionLine].endsWith('\r') ? '\r' : '');
36+
sourceLines.splice(lastDescriptionLine + 1, 0, injectedLine);
3837

3938
return fixer.replaceText(jsdocNode, sourceLines.join('\n'));
4039
}, {
@@ -48,11 +47,11 @@ export default iterateJsdoc(({
4847
});
4948
report('There must be no newline after the description of the JSDoc block.', (fixer) => {
5049
// Remove the extra line
51-
sourceLines.splice(lastDescriptionLine + 1, 1);
50+
sourceLines.splice(lastDescriptionLine, 1);
5251

5352
return fixer.replaceText(jsdocNode, sourceLines.join('\n'));
5453
}, {
55-
line: lastDescriptionLine + 1,
54+
line: lastDescriptionLine,
5655
});
5756
}
5857
}, {
@@ -67,4 +66,5 @@ export default iterateJsdoc(({
6766
],
6867
type: 'layout',
6968
},
69+
noTrim: true,
7070
});

test/rules/assertions/newlineAfterDescription.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,32 @@ export default {
149149
*/
150150
`,
151151
},
152+
{
153+
code: `\r
154+
/**\r
155+
* Service for fetching symbols.\r
156+
* @param {Object} $http - Injected http helper.\r
157+
* @param {Object} $q - Injected Promise api helper.\r
158+
* @param {Object} $location - Injected window location object.\r
159+
* @param {Object} REPORT_DIALOG_CONSTANTS - Injected handle.\r
160+
*/\r
161+
`,
162+
errors: [
163+
{
164+
message: 'There must be a newline after the description of the JSDoc block.',
165+
},
166+
],
167+
output: `\r
168+
/**\r
169+
* Service for fetching symbols.\r
170+
*\r
171+
* @param {Object} $http - Injected http helper.\r
172+
* @param {Object} $q - Injected Promise api helper.\r
173+
* @param {Object} $location - Injected window location object.\r
174+
* @param {Object} REPORT_DIALOG_CONSTANTS - Injected handle.\r
175+
*/\r
176+
`,
177+
},
152178
],
153179
valid: [
154180
{

0 commit comments

Comments
 (0)