From a38b28b0ecc336e05237d410e110566f2833ea89 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 19 Nov 2019 09:21:11 +0800 Subject: [PATCH] fix(`newline-after-description`): handle carriage returns properly: fixes #431 --- README.md | 10 +++++++ src/rules/newlineAfterDescription.js | 14 +++++----- .../assertions/newlineAfterDescription.js | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c02552d6e..f706c04f4 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/rules/newlineAfterDescription.js b/src/rules/newlineAfterDescription.js index 3cbb486e5..3ed36822a 100644 --- a/src/rules/newlineAfterDescription.js +++ b/src/rules/newlineAfterDescription.js @@ -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) { @@ -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')); }, { @@ -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, }); } }, { @@ -67,4 +66,5 @@ export default iterateJsdoc(({ ], type: 'layout', }, + noTrim: true, }); diff --git a/test/rules/assertions/newlineAfterDescription.js b/test/rules/assertions/newlineAfterDescription.js index d65856d3c..f3afe6a7c 100644 --- a/test/rules/assertions/newlineAfterDescription.js +++ b/test/rules/assertions/newlineAfterDescription.js @@ -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: [ {