From a23168d8ff38887609b81715e67bb2682dc2c534 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sat, 12 Mar 2022 08:24:16 +0800 Subject: [PATCH] fix(`match-name`): perform replacements for names appearing after multiline types --- README.md | 8 ++++++++ src/rules/matchName.js | 11 +++++++--- test/rules/assertions/matchName.js | 32 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1e2d135a2..fb1050efe 100644 --- a/README.md +++ b/README.md @@ -7685,6 +7685,14 @@ function quux () { function quux () {} // "jsdoc/match-name": ["error"|"warn", ] // Message: Rule `no-restricted-syntax` is missing a `match` option. + +/** + * @param { + * someType + * } opt_a + */ +// "jsdoc/match-name": ["error"|"warn", {"match":[{"disallowName":"/^opt_/i","replacement":""}]}] +// Message: Only allowing names not matching `/^opt_/i` but found "opt_a". ```` The following patterns are not considered problems: diff --git a/src/rules/matchName.js b/src/rules/matchName.js index 2ffdf3bbc..1a5f9fab0 100644 --- a/src/rules/matchName.js +++ b/src/rules/matchName.js @@ -50,9 +50,14 @@ export default iterateJsdoc(({ } const fixer = () => { - tag.source[0].tokens.name = tag.source[0].tokens.name.replace( - disallowNameRegex, replacement, - ); + for (const src of tag.source) { + if (src.tokens.name) { + src.tokens.name = src.tokens.name.replace( + disallowNameRegex, replacement, + ); + break; + } + } }; let { diff --git a/test/rules/assertions/matchName.js b/test/rules/assertions/matchName.js index 04198bdf2..32b6104c9 100644 --- a/test/rules/assertions/matchName.js +++ b/test/rules/assertions/matchName.js @@ -325,6 +325,38 @@ export default { ], options: [], }, + { + code: ` + /** + * @param { + * someType + * } opt_a + */ + `, + errors: [ + { + line: 3, + message: 'Only allowing names not matching `/^opt_/i` but found "opt_a".', + }, + ], + options: [ + { + match: [ + { + disallowName: '/^opt_/i', + replacement: '', + }, + ], + }, + ], + output: ` + /** + * @param { + * someType + * } a + */ + `, + }, ], valid: [ {