diff --git a/README.md b/README.md index fb1050efe..a422dd540 100644 --- a/README.md +++ b/README.md @@ -7171,6 +7171,13 @@ function quux () { } // "jsdoc/match-description": ["error"|"warn", {"matchDescription":"^\\S[\\s\\S]*\\S$"}] // Message: JSDoc description does not satisfy the regex pattern. + +/** + * + * @param + */ +// "jsdoc/match-description": ["error"|"warn", {"contexts":["any"],"matchDescription":"^\\S[\\s\\S]*\\S$"}] +// Message: JSDoc description does not satisfy the regex pattern. ```` The following patterns are not considered problems: diff --git a/src/iterateJsdoc.js b/src/iterateJsdoc.js index 4830ecced..f732c08ad 100644 --- a/src/iterateJsdoc.js +++ b/src/iterateJsdoc.js @@ -182,7 +182,7 @@ const getUtils = ( } if (idx || description) { - descriptions.push(description); + descriptions.push(description || (descriptions.length ? '' : '\n')); } return false; diff --git a/src/rules/matchDescription.js b/src/rules/matchDescription.js index 8010f161c..9633cea97 100644 --- a/src/rules/matchDescription.js +++ b/src/rules/matchDescription.js @@ -23,7 +23,7 @@ export default iterateJsdoc(({ tags, } = context.options[0] || {}; - const validateDescription = (description, tag) => { + const validateDescription = (desc, tag) => { let mainDescriptionMatch = mainDescription; let errorMessage = message; if (typeof mainDescription === 'object') { @@ -52,7 +52,7 @@ export default iterateJsdoc(({ stringOrDefault(tagValue, matchDescription), ); - if (!regex.test(description)) { + if (!regex.test(desc)) { report( errorMessage || 'JSDoc description does not satisfy the regex pattern.', null, @@ -64,10 +64,10 @@ export default iterateJsdoc(({ } }; - if (jsdoc.description) { - const { - description, - } = utils.getDescription(); + const { + description, + } = utils.getDescription(); + if (description) { validateDescription(description); } @@ -80,9 +80,9 @@ export default iterateJsdoc(({ }; utils.forEachPreferredTag('description', (matchingJsdocTag, targetTagName) => { - const description = (matchingJsdocTag.name + ' ' + utils.getTagDescription(matchingJsdocTag)).trim(); + const desc = (matchingJsdocTag.name + ' ' + utils.getTagDescription(matchingJsdocTag)).trim(); if (hasOptionTag(targetTagName)) { - validateDescription(description, matchingJsdocTag); + validateDescription(desc, matchingJsdocTag); } }, true); @@ -97,16 +97,16 @@ export default iterateJsdoc(({ } = utils.getTagsByType(whitelistedTags); tagsWithNames.some((tag) => { - const description = utils.getTagDescription(tag).replace(/^[- ]*/u, '') + const desc = utils.getTagDescription(tag).replace(/^[- ]*/u, '') .trim(); - return validateDescription(description, tag); + return validateDescription(desc, tag); }); tagsWithoutNames.some((tag) => { - const description = (tag.name + ' ' + utils.getTagDescription(tag)).trim(); + const desc = (tag.name + ' ' + utils.getTagDescription(tag)).trim(); - return validateDescription(description, tag); + return validateDescription(desc, tag); }); }, { contextDefaults: true, diff --git a/test/rules/assertions/matchDescription.js b/test/rules/assertions/matchDescription.js index 86f1df055..568bdc05a 100644 --- a/test/rules/assertions/matchDescription.js +++ b/test/rules/assertions/matchDescription.js @@ -956,6 +956,28 @@ export default { }, ], }, + { + code: ` + /** + * + * @param + */ + `, + errors: [ + { + line: 3, + message: 'JSDoc description does not satisfy the regex pattern.', + }, + ], + options: [ + { + contexts: [ + 'any', + ], + matchDescription: '^\\S[\\s\\S]*\\S$', + }, + ], + }, ], valid: [ {