Skip to content

Commit

Permalink
fix(newline-after-description): handle carriage returns properly: f…
Browse files Browse the repository at this point in the history
…ixes #431
  • Loading branch information
brettz9 committed Nov 19, 2019
1 parent d3ca65b commit a38b28b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -4366,6 +4366,16 @@ function quux () {
*/
// Options: ["always"]
// Message: There must be a newline after the description of the JSDoc block.
/**
* Service for fetching symbols.
* @param {Object} $http - Injected http helper.
* @param {Object} $q - Injected Promise api helper.
* @param {Object} $location - Injected window location object.
* @param {Object} REPORT_DIALOG_CONSTANTS - Injected handle.
*/
// Message: There must be a newline after the description of the JSDoc block.
````
The following patterns are not considered problems:
Expand Down
14 changes: 7 additions & 7 deletions src/rules/newlineAfterDescription.js
Expand Up @@ -21,10 +21,7 @@ export default iterateJsdoc(({
always = true;
}

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

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

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

return fixer.replaceText(jsdocNode, sourceLines.join('\n'));
}, {
line: lastDescriptionLine + 1,
line: lastDescriptionLine,
});
}
}, {
Expand All @@ -67,4 +66,5 @@ export default iterateJsdoc(({
],
type: 'layout',
},
noTrim: true,
});
26 changes: 26 additions & 0 deletions test/rules/assertions/newlineAfterDescription.js
Expand Up @@ -149,6 +149,32 @@ export default {
*/
`,
},
{
code: `\r
/**\r
* Service for fetching symbols.\r
* @param {Object} $http - Injected http helper.\r
* @param {Object} $q - Injected Promise api helper.\r
* @param {Object} $location - Injected window location object.\r
* @param {Object} REPORT_DIALOG_CONSTANTS - Injected handle.\r
*/\r
`,
errors: [
{
message: 'There must be a newline after the description of the JSDoc block.',
},
],
output: `\r
/**\r
* Service for fetching symbols.\r
*\r
* @param {Object} $http - Injected http helper.\r
* @param {Object} $q - Injected Promise api helper.\r
* @param {Object} $location - Injected window location object.\r
* @param {Object} REPORT_DIALOG_CONSTANTS - Injected handle.\r
*/\r
`,
},
],
valid: [
{
Expand Down

0 comments on commit a38b28b

Please sign in to comment.