From e247d6777fff34e73193db8ccc61beb9de988002 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 23 Oct 2022 08:56:10 -0700 Subject: [PATCH] fix(`valid-types`): report problems with name parsing --- README.md | 33 ++++++++++++ src/rules/validTypes.js | 10 ++++ test/rules/assertions/validTypes.js | 78 +++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) diff --git a/README.md b/README.md index 11f808b9a..a8fc5d7bd 100644 --- a/README.md +++ b/README.md @@ -22332,6 +22332,39 @@ function quux () {} function quux () {} // Settings: {"jsdoc":{"mode":"closure"}} // Message: Syntax error in supresss type: blah + +/** + * @param {Object[]} employees + * @param {string} employees[.name - The name of an employee. + */ +function quux () {} +// Message: Invalid name: unpaired brackets + +/** + * @param {Object[]} employees + * @param {string} [] - The name of an employee. + */ +function quux () {} +// Message: Invalid name: empty name + +/** + * @param {Object[]} employees + * @param {string} [] - The name of an employee. + */ +function quux () {} +// Message: Invalid name: empty name + +/** + * @param {string} [name=] - The name of an employee. + */ +function quux () {} +// Message: Invalid name: empty default value + +/** + * @param {string} [name==] - The name of an employee. + */ +function quux () {} +// Message: Invalid name: invalid default value syntax ```` The following patterns are not considered problems: diff --git a/src/rules/validTypes.js b/src/rules/validTypes.js index b3c77889c..e4569991b 100644 --- a/src/rules/validTypes.js +++ b/src/rules/validTypes.js @@ -158,6 +158,16 @@ export default iterateJsdoc(({ return true; }; + if (tag.problems.length) { + const msg = tag.problems.reduce((str, { + message, + }) => { + return str + '; ' + message; + }, '').slice(2); + report(`Invalid name: ${msg}`, null, tag); + continue; + } + if (tag.tag === 'borrows') { const thisNamepath = utils.getTagDescription(tag).replace(asExpression, '') .trim(); diff --git a/test/rules/assertions/validTypes.js b/test/rules/assertions/validTypes.js index 47ed88b46..5feb20b4e 100644 --- a/test/rules/assertions/validTypes.js +++ b/test/rules/assertions/validTypes.js @@ -974,6 +974,84 @@ export default { ], ignoreReadme: true, }, + { + code: ` + /** + * @param {Object[]} employees + * @param {string} employees[.name - The name of an employee. + */ + function quux () {} + + `, + errors: [ + { + line: 4, + message: 'Invalid name: unpaired brackets', + }, + ], + }, + { + code: ` + /** + * @param {Object[]} employees + * @param {string} [] - The name of an employee. + */ + function quux () {} + + `, + errors: [ + { + line: 4, + message: 'Invalid name: empty name', + }, + ], + }, + { + code: ` + /** + * @param {Object[]} employees + * @param {string} [] - The name of an employee. + */ + function quux () {} + + `, + errors: [ + { + line: 4, + message: 'Invalid name: empty name', + }, + ], + }, + { + code: ` + /** + * @param {string} [name=] - The name of an employee. + */ + function quux () {} + + `, + errors: [ + { + line: 3, + message: 'Invalid name: empty default value', + }, + ], + }, + { + code: ` + /** + * @param {string} [name==] - The name of an employee. + */ + function quux () {} + + `, + errors: [ + { + line: 3, + message: 'Invalid name: invalid default value syntax', + }, + ], + }, ], valid: [ {