Skip to content

fix(no-undefined-types): mark sister method and property names as defined #1404

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 20, 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
39 changes: 39 additions & 0 deletions docs/rules/no-undefined-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,5 +865,44 @@ quux();
/**
* @type {Linters}
*/

class Filler {
static methodOne () {
return 'Method One';
}

nonStaticMethodTwo (param) {
return `Method Two received: ${param}`;
}

/**
* {@link methodOne} shouldn't report eslint error
* {@link nonStaticMethodTwo} also shouldn't report eslint error
* @returns {number} A number representing the answer to everything.
*/
static methodThree () {
return 42;
}

/**
* {@link Filler.methodOne} doesn't report eslint error
* {@link Filler.nonStaticMethodTwo} also doesn't report eslint error
* @returns {string} A string indicating the method's purpose.
*/
methodFour() {
return 'Method Four';
}
}

class Foo {
foo = "foo";
/**
* Something related to {@link foo}
* @returns {string} Something awesome
*/
bar() {
return "bar";
}
}
````

25 changes: 25 additions & 0 deletions src/rules/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,31 @@ export default iterateJsdoc(({
.concat(importTags)
.concat(definedTypes)
.concat(/** @type {string[]} */ (definedPreferredTypes))
.concat((() => {
// Other methods are not in scope, but we need them, and we grab them here
if (node?.type === 'MethodDefinition') {
return /** @type {import('estree').ClassBody} */ (node.parent).body.map((methodOrProp) => {
if (methodOrProp.type === 'MethodDefinition') {
// eslint-disable-next-line unicorn/no-lonely-if -- Pattern
if (methodOrProp.key.type === 'Identifier') {
return methodOrProp.key.name;
}
}

if (methodOrProp.type === 'PropertyDefinition') {
// eslint-disable-next-line unicorn/no-lonely-if -- Pattern
if (methodOrProp.key.type === 'Identifier') {
return methodOrProp.key.name;
}
}
/* c8 ignore next 2 -- Not yet built */

return '';
}).filter(Boolean);
}

return [];
})())
.concat(...getValidRuntimeIdentifiers(node && (
(sourceCode.getScope &&
/* c8 ignore next 2 */
Expand Down
48 changes: 48 additions & 0 deletions test/rules/assertions/noUndefinedTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1549,5 +1549,53 @@ export default /** @type {import('../index.js').TestCases} */ ({
*/
`,
},
{
code: `
class Filler {
static methodOne () {
return 'Method One';
}

nonStaticMethodTwo (param) {
return \`Method Two received: $\{param}\`;
}

/**
* {@link methodOne} shouldn't report eslint error
* {@link nonStaticMethodTwo} also shouldn't report eslint error
* @returns {number} A number representing the answer to everything.
*/
static methodThree () {
return 42;
}

/**
* {@link Filler.methodOne} doesn't report eslint error
* {@link Filler.nonStaticMethodTwo} also doesn't report eslint error
* @returns {string} A string indicating the method's purpose.
*/
methodFour() {
return 'Method Four';
}
}
`,
},
{
code: `
class Foo {
foo = "foo";
/**
* Something related to {@link foo}
* @returns {string} Something awesome
*/
bar() {
return "bar";
}
}
`,
languageOptions: {
ecmaVersion: 2_022,
},
},
],
});
Loading