Skip to content

Commit 25571f9

Browse files
committed
fix(no-undefined-types): allow global prefixes
1 parent 75c8127 commit 25571f9

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

docs/rules/no-undefined-types.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,5 +1092,10 @@ const getValue = () => {};
10921092
*/
10931093
const defineCustomElement = (tagName, component) => {
10941094
};
1095+
1096+
class Storage {
1097+
/** @type {globalThis.localStorage} */
1098+
#storage
1099+
}
10951100
````
10961101

src/rules/noUndefinedTypes.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const extraTypes = [
2020
'Array', 'Object', 'RegExp', 'Date', 'Function', 'Intl',
2121
];
2222

23+
const globalTypes = [
24+
'globalThis', 'global', 'window', 'self',
25+
];
26+
2327
const typescriptGlobals = [
2428
// https://www.typescriptlang.org/docs/handbook/utility-types.html
2529
'Awaited',
@@ -470,6 +474,7 @@ export default iterateJsdoc(({
470474
parsedType,
471475
tag,
472476
} of tagsWithTypes) {
477+
// eslint-disable-next-line complexity -- Refactor
473478
traverse(parsedType, (nde, parentNode) => {
474479
/**
475480
* @type {import('jsdoc-type-pratt-parser').NameResult & {
@@ -501,8 +506,10 @@ export default iterateJsdoc(({
501506
!importTags.includes(val) &&
502507
!extraTypes.includes(val) &&
503508
!typedefDeclarations.includes(val) &&
509+
!globalTypes.includes(val) &&
504510
currNode && 'right' in currNode &&
505-
currNode.right?.type === 'JsdocTypeProperty') {
511+
currNode.right?.type === 'JsdocTypeProperty'
512+
) {
506513
val = val + '.' + currNode.right.value;
507514
}
508515
} while (currNode?.type === 'JsdocTypeNamePath');

test/rules/assertions/noUndefinedTypes.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,5 +1857,18 @@ export default /** @type {import('../index.js').TestCases} */ ({
18571857
};
18581858
`,
18591859
},
1860+
{
1861+
code: `
1862+
class Storage {
1863+
/** @type {globalThis.localStorage} */
1864+
#storage
1865+
}
1866+
`,
1867+
languageOptions: {
1868+
globals: {
1869+
localStorage: 'readonly',
1870+
},
1871+
},
1872+
},
18601873
],
18611874
});

0 commit comments

Comments
 (0)