From 456cff3f3d4e0403f80870a2120db3c2eadcf563 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 12 Oct 2025 07:51:50 -0700 Subject: [PATCH] fix(`no-undefined-types`): allow global prefixes; fixes #1566 --- docs/rules/no-undefined-types.md | 5 +++++ src/rules/noUndefinedTypes.js | 9 ++++++++- test/rules/assertions/noUndefinedTypes.js | 13 +++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/docs/rules/no-undefined-types.md b/docs/rules/no-undefined-types.md index b90322be3..0b9eaa7ac 100644 --- a/docs/rules/no-undefined-types.md +++ b/docs/rules/no-undefined-types.md @@ -1092,5 +1092,10 @@ const getValue = () => {}; */ const defineCustomElement = (tagName, component) => { }; + +class Storage { + /** @type {globalThis.localStorage} */ + #storage +} ```` diff --git a/src/rules/noUndefinedTypes.js b/src/rules/noUndefinedTypes.js index 3bb9eb9a1..af80beff6 100644 --- a/src/rules/noUndefinedTypes.js +++ b/src/rules/noUndefinedTypes.js @@ -20,6 +20,10 @@ const extraTypes = [ 'Array', 'Object', 'RegExp', 'Date', 'Function', 'Intl', ]; +const globalTypes = [ + 'globalThis', 'global', 'window', 'self', +]; + const typescriptGlobals = [ // https://www.typescriptlang.org/docs/handbook/utility-types.html 'Awaited', @@ -470,6 +474,7 @@ export default iterateJsdoc(({ parsedType, tag, } of tagsWithTypes) { + // eslint-disable-next-line complexity -- Refactor traverse(parsedType, (nde, parentNode) => { /** * @type {import('jsdoc-type-pratt-parser').NameResult & { @@ -501,8 +506,10 @@ export default iterateJsdoc(({ !importTags.includes(val) && !extraTypes.includes(val) && !typedefDeclarations.includes(val) && + !globalTypes.includes(val) && currNode && 'right' in currNode && - currNode.right?.type === 'JsdocTypeProperty') { + currNode.right?.type === 'JsdocTypeProperty' + ) { val = val + '.' + currNode.right.value; } } while (currNode?.type === 'JsdocTypeNamePath'); diff --git a/test/rules/assertions/noUndefinedTypes.js b/test/rules/assertions/noUndefinedTypes.js index 451a96d9a..c4ec1636e 100644 --- a/test/rules/assertions/noUndefinedTypes.js +++ b/test/rules/assertions/noUndefinedTypes.js @@ -1857,5 +1857,18 @@ export default /** @type {import('../index.js').TestCases} */ ({ }; `, }, + { + code: ` + class Storage { + /** @type {globalThis.localStorage} */ + #storage + } + `, + languageOptions: { + globals: { + localStorage: 'readonly', + }, + }, + }, ], });