Skip to content

Commit eade377

Browse files
brettz9claude
andcommitted
fix(no-undefined-types): recognize class members from the class's own JSDoc
The rule collected sibling class members (for `{@link}` and other namepath references) only when the JSDoc block was attached to a `MethodDefinition` or `PropertyDefinition`. A block on the class itself did not get the class's members, so a reference like `{@link property}` in the class's own JSDoc was reported as undefined. Handle `ClassDeclaration` and `ClassExpression` nodes too, reading members from `node.body` and the name from `node.id`, and collapse the near-identical method/property arms into one. Fixes #1762 Co-Authored-By: Claude Sonnet 5 <[email protected]>
1 parent 1b3c339 commit eade377

3 files changed

Lines changed: 78 additions & 24 deletions

File tree

‎docs/rules/no-undefined-types.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,21 @@ class MyClass {
10101010
}
10111011
// Settings: {"jsdoc":{"mode":"typescript"}}
10121012

1013+
/** xyz {@link property} */
1014+
class MyClass {
1015+
property = true;
1016+
}
1017+
1018+
/** xyz {@link property} and {@link MyClass.property} */
1019+
class MyClass {
1020+
public property: boolean;
1021+
}
1022+
// Settings: {"jsdoc":{"mode":"typescript"}}
1023+
1024+
const MyClass = /** xyz {@link property} */ class {
1025+
property = true;
1026+
};
1027+
10131028
/* globals SomeGlobal, AnotherGlobal */
10141029
import * as Ably from "ably"
10151030
import Testing, { another as Another, stillMore as StillMore } from "testing"

‎src/rules/noUndefinedTypes.js‎

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -483,32 +483,34 @@ export default iterateJsdoc(({
483483
.concat(/** @type {string[]} */ (definedPreferredTypes))
484484
.concat((() => {
485485
// Other class members are not in scope, but we need them (e.g., for a
486-
// sibling property or method referenced by `{@link}`), and we grab
487-
// them here
486+
// sibling property or method referenced by `{@link}`, or a member
487+
// referenced by `{@link}` from the class's own JSDoc block), and we
488+
// grab them here
489+
/** @type {import('estree').ClassBody|undefined} */
490+
let classBody;
491+
/** @type {string|undefined} */
492+
let className;
488493
if (node?.type === 'MethodDefinition' || node?.type === 'PropertyDefinition') {
489-
return /** @type {import('estree').ClassBody} */ (node.parent).body.flatMap((methodOrProp) => {
490-
if (methodOrProp.type === 'MethodDefinition') {
491-
// eslint-disable-next-line unicorn/no-lonely-if -- Pattern
492-
if (methodOrProp.key.type === 'Identifier') {
493-
return [
494-
methodOrProp.key.name,
495-
`${/** @type {import('estree').ClassDeclaration} */ (
496-
node.parent?.parent
497-
)?.id?.name}.${methodOrProp.key.name}`,
498-
];
499-
}
500-
}
494+
classBody = /** @type {import('estree').ClassBody} */ (node.parent);
495+
className = /** @type {import('estree').ClassDeclaration} */ (
496+
node.parent?.parent
497+
)?.id?.name;
498+
} else if (node?.type === 'ClassDeclaration' || node?.type === 'ClassExpression') {
499+
classBody = node.body;
500+
className = node.id?.name;
501+
}
501502

502-
if (methodOrProp.type === 'PropertyDefinition') {
503-
// eslint-disable-next-line unicorn/no-lonely-if -- Pattern
504-
if (methodOrProp.key.type === 'Identifier') {
505-
return [
506-
methodOrProp.key.name,
507-
`${/** @type {import('estree').ClassDeclaration} */ (
508-
node.parent?.parent
509-
)?.id?.name}.${methodOrProp.key.name}`,
510-
];
511-
}
503+
if (classBody) {
504+
return classBody.body.flatMap((methodOrProp) => {
505+
if (
506+
(methodOrProp.type === 'MethodDefinition' ||
507+
methodOrProp.type === 'PropertyDefinition') &&
508+
methodOrProp.key.type === 'Identifier'
509+
) {
510+
return [
511+
methodOrProp.key.name,
512+
`${className}.${methodOrProp.key.name}`,
513+
];
512514
}
513515
/* c8 ignore next 2 -- Not yet built */
514516

‎test/rules/assertions/noUndefinedTypes.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,43 @@ export default /** @type {import('../index.js').TestCases} */ ({
17681768
},
17691769
},
17701770
},
1771+
{
1772+
code: `
1773+
/** xyz {@link property} */
1774+
class MyClass {
1775+
property = true;
1776+
}
1777+
`,
1778+
languageOptions: {
1779+
ecmaVersion: 2_022,
1780+
},
1781+
},
1782+
{
1783+
code: `
1784+
/** xyz {@link property} and {@link MyClass.property} */
1785+
class MyClass {
1786+
public property: boolean;
1787+
}
1788+
`,
1789+
languageOptions: {
1790+
parser: typescriptEslintParser,
1791+
},
1792+
settings: {
1793+
jsdoc: {
1794+
mode: 'typescript',
1795+
},
1796+
},
1797+
},
1798+
{
1799+
code: `
1800+
const MyClass = /** xyz {@link property} */ class {
1801+
property = true;
1802+
};
1803+
`,
1804+
languageOptions: {
1805+
ecmaVersion: 2_022,
1806+
},
1807+
},
17711808
{
17721809
code: `
17731810
/* globals SomeGlobal, AnotherGlobal */

0 commit comments

Comments
 (0)