From ab5624be6278836241955b2b336bf3b0803ab33d Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Thu, 4 Apr 2024 16:24:00 -0700 Subject: [PATCH] fix(`valid-types`): whitelist pratt parser keywords; fixes #1221 --- docs/rules/check-tag-names.md | 2 +- docs/rules/valid-types.md | 10 ++++++++++ src/rules/validTypes.js | 12 +++++++++++- test/rules/assertions/validTypes.js | 13 +++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/docs/rules/check-tag-names.md b/docs/rules/check-tag-names.md index 5aef2ab34..192404234 100644 --- a/docs/rules/check-tag-names.md +++ b/docs/rules/check-tag-names.md @@ -722,7 +722,7 @@ function quux (foo) {} */ function quux (foo) {} // Settings: {"jsdoc":{"mode":"jsdoc"}} -// Message: Invalid JSDoc tag name "internal". +// Message: Invalid JSDoc tag name "import". /** * @externs diff --git a/docs/rules/valid-types.md b/docs/rules/valid-types.md index 5b0f61cfa..19bcf6c99 100644 --- a/docs/rules/valid-types.md +++ b/docs/rules/valid-types.md @@ -877,5 +877,15 @@ function quux() { /** * An inline {@link text} tag with content. */ + +/** + * @param typeof + * @param readonly + * @param import + * @param is + */ +function quux() { + +} ```` diff --git a/src/rules/validTypes.js b/src/rules/validTypes.js index f8620da42..2930ed1d3 100644 --- a/src/rules/validTypes.js +++ b/src/rules/validTypes.js @@ -10,6 +10,13 @@ const inlineTags = new Set([ 'tutorial', ]); +const jsdocTypePrattKeywords = new Set([ + 'typeof', + 'readonly', + 'import', + 'is', +]); + const asExpression = /as\s+/u; const suppressTypes = new Set([ @@ -107,7 +114,10 @@ export default iterateJsdoc(({ * @returns {boolean} */ const validNamepathParsing = function (namepath, tagName) { - if (tryParsePathIgnoreError(namepath)) { + if ( + tryParsePathIgnoreError(namepath) || + jsdocTypePrattKeywords.has(namepath) + ) { return true; } diff --git a/test/rules/assertions/validTypes.js b/test/rules/assertions/validTypes.js index c201e9486..11bf9a131 100644 --- a/test/rules/assertions/validTypes.js +++ b/test/rules/assertions/validTypes.js @@ -1856,5 +1856,18 @@ export default { */ `, }, + { + code: ` + /** + * @param typeof + * @param readonly + * @param import + * @param is + */ + function quux() { + + } + ` + }, ], };