From 7c0b8c608fa36260224bb856ea5a3ba34bd670ad Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 14 Oct 2024 23:16:25 +0800 Subject: [PATCH] fix(`match-name`): revert to prior correct behavior of ignoring optional and default code surrounding name --- docs/rules/match-name.md | 2 +- src/rules/matchName.js | 13 +++++++------ test/rules/assertions/matchName.js | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/rules/match-name.md b/docs/rules/match-name.md index 48cd10446..8c223f7e0 100644 --- a/docs/rules/match-name.md +++ b/docs/rules/match-name.md @@ -251,6 +251,6 @@ class A { * @typedef {object} Test * @property {T} test */ -// "jsdoc/match-name": ["error"|"warn", {"match":[{"allowName":"/^\\[?[A-Z]{1}(=.*\\])$/","message":"The name should be a single capital letter.","tags":["template"]}]}] +// "jsdoc/match-name": ["error"|"warn", {"match":[{"allowName":"/^[A-Z]{1}$/","message":"The name should be a single capital letter.","tags":["template"]}]}] ```` diff --git a/src/rules/matchName.js b/src/rules/matchName.js index 66d789611..f0371e320 100644 --- a/src/rules/matchName.js +++ b/src/rules/matchName.js @@ -38,8 +38,9 @@ export default iterateJsdoc(({ let reported = false; for (const tag of applicableTags) { - const allowed = !allowNameRegex || allowNameRegex.test(tag.name); - const disallowed = disallowNameRegex && disallowNameRegex.test(tag.name); + const tagName = tag.name.replace(/^\[/u, '').replace(/(=.*)?\]$/u, ''); + const allowed = !allowNameRegex || allowNameRegex.test(tagName); + const disallowed = disallowNameRegex && disallowNameRegex.test(tagName); const hasRegex = allowNameRegex || disallowNameRegex; if (hasRegex && allowed && !disallowed) { continue; @@ -66,10 +67,10 @@ export default iterateJsdoc(({ if (!message) { if (hasRegex) { message = disallowed ? - `Only allowing names not matching \`${disallowNameRegex}\` but found "${tag.name}".` : - `Only allowing names matching \`${allowNameRegex}\` but found "${tag.name}".`; + `Only allowing names not matching \`${disallowNameRegex}\` but found "${tagName}".` : + `Only allowing names matching \`${allowNameRegex}\` but found "${tagName}".`; } else { - message = `Prohibited context for "${tag.name}".`; + message = `Prohibited context for "${tagName}".`; } } @@ -86,7 +87,7 @@ export default iterateJsdoc(({ // Could also supply `context`, `comment`, `tags` allowName, disallowName, - name: tag.name, + name: tagName, }, ); if (!hasRegex) { diff --git a/test/rules/assertions/matchName.js b/test/rules/assertions/matchName.js index eb51508ae..a72f15cc2 100644 --- a/test/rules/assertions/matchName.js +++ b/test/rules/assertions/matchName.js @@ -553,7 +553,7 @@ export default { `, options: [{ match: [{ - allowName: "/^\\[?[A-Z]{1}(=.*\\])$/", + allowName: "/^[A-Z]{1}$/", message: "The name should be a single capital letter.", tags: ["template"], }],