Skip to content

fix(no-undefined-types): liberalize checks to reallow for unknown properties on imports and defined globals #1413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/rules/no-undefined-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -919,5 +919,26 @@ class Foo {
return "bar";
}
}

/* globals SomeGlobal, AnotherGlobal */
import * as Ably from "ably"
import Testing, { another as Another, stillMore as StillMore } from "testing"

export class Code {
/** @type {Ably.Realtime} */
static #client

/** @type {Testing.SomeMethod} */
static #test

/** @type {Another.AnotherMethod} */
static #test2

/** @type {StillMore.AnotherMethod} */
static #test3

/** @type {AnotherGlobal.AnotherMethod} */
static #test4
}
````

37 changes: 35 additions & 2 deletions src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,22 @@ export default iterateJsdoc(({
.filter(Boolean));
}

const comments = sourceCode.getAllComments()
const allComments = sourceCode.getAllComments();
const comments = allComments
.filter((comment) => {
return (/^\*\s/u).test(comment.value);
})
.map((commentNode) => {
return parseComment(commentNode, '');
});

const globals = allComments
.filter((comment) => {
return (/^\s*globals/u).test(comment.value);
}).flatMap((commentNode) => {
return commentNode.value.replace(/^\s*globals/u, '').trim().split(/,\s*/u);
});

const typedefDeclarations = comments
.flatMap((doc) => {
return doc.tags.filter(({
Expand Down Expand Up @@ -242,6 +250,12 @@ export default iterateJsdoc(({
return result;
};

/**
* We treat imports differently as we can't introspect their children.
* @type {string[]}
*/
const imports = [];

const allDefinedTypes = new Set(globalScope.variables.map(({
name,
}) => {
Expand All @@ -256,8 +270,22 @@ export default iterateJsdoc(({
}) => {
return variables;
}).map(({
identifiers,
name,
}) => {
if (
[
'ImportDefaultSpecifier',
'ImportNamespaceSpecifier',
'ImportSpecifier',
].includes(
/** @type {import('estree').Identifier & {parent: {type: string}}} */ (
identifiers?.[0])?.parent?.type,
)
) {
imports.push(name);
}

return name;
/* c8 ignore next */
}) : [],
Expand Down Expand Up @@ -408,7 +436,12 @@ export default iterateJsdoc(({
* _parent?: import('jsdoc-type-pratt-parser').NonRootResult
* }}
*/ (currNode)._parent;
if (currNode && 'right' in currNode && currNode.right?.type === 'JsdocTypeProperty') {
if (
// Avoid appending for imports and globals since we don't want to
// check their properties which may or may not exist
!imports.includes(val) && !globals.includes(val) &&
currNode && 'right' in currNode &&
currNode.right?.type === 'JsdocTypeProperty') {
val = val + '.' + currNode.right.value;
}
} while (currNode?.type === 'JsdocTypeNamePath');
Expand Down
27 changes: 27 additions & 0 deletions test/rules/assertions/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1624,5 +1624,32 @@ export default /** @type {import('../index.js').TestCases} */ ({
ecmaVersion: 2_022,
},
},
{
code: `
/* globals SomeGlobal, AnotherGlobal */
import * as Ably from "ably"
import Testing, { another as Another, stillMore as StillMore } from "testing"

export class Code {
/** @type {Ably.Realtime} */
static #client

/** @type {Testing.SomeMethod} */
static #test

/** @type {Another.AnotherMethod} */
static #test2

/** @type {StillMore.AnotherMethod} */
static #test3

/** @type {AnotherGlobal.AnotherMethod} */
static #test4
}
`,
languageOptions: {
parser: typescriptEslintParser,
},
},
],
});
Loading