Skip to content

Commit 2d85fed

Browse files
committed
fix: for @link targets, check properties within export class declarations; fixes #1771
1 parent f1f318a commit 2d85fed

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ const a = 's';
427427
const b = 's';
428428
// "jsdoc/no-undefined-types": ["error"|"warn", {"definedTypes":["MyNamespace"]}]
429429
// Message: The type 'MyNamespace.OtherType' is undefined.
430+
431+
/** xyz {@link property} */
432+
export default function () {}
433+
// Message: The type 'property' is undefined.
430434
````
431435

432436

@@ -1308,5 +1312,15 @@ const b = (foo: number) => {};
13081312
const concatIteratorJSDoc = (...its) => {
13091313
return Iterator.from(its.flat());
13101314
};
1315+
1316+
/** xyz {@link property} */
1317+
export class MyClass {
1318+
public property: boolean
1319+
}
1320+
1321+
/** xyz {@link property} */
1322+
export default class MyClass {
1323+
property = true;
1324+
}
13111325
````
13121326

‎src/rules/noUndefinedTypes.js‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ export default iterateJsdoc(({
482482
.concat(tsModuleVariables)
483483
.concat(/** @type {string[]} */ (definedPreferredTypes))
484484
.concat((() => {
485+
let nodeToCheck = node;
486+
if (node?.type === 'ExportNamedDeclaration' || node?.type === 'ExportDefaultDeclaration') {
487+
nodeToCheck = /** @type {import('estree').ClassDeclaration & import('eslint').Rule.NodeParentExtension} */ (
488+
/** @type {import('estree').ExportNamedDeclaration & import('eslint').Rule.NodeParentExtension} */
489+
(nodeToCheck)?.declaration
490+
) ?? null;
491+
}
492+
485493
// Other class members are not in scope, but we need them (e.g., for a
486494
// sibling property or method referenced by `{@link}`, or a member
487495
// referenced by `{@link}` from the class's own JSDoc block), and we
@@ -490,14 +498,14 @@ export default iterateJsdoc(({
490498
let classBody;
491499
/** @type {string|undefined} */
492500
let className;
493-
if (node?.type === 'MethodDefinition' || node?.type === 'PropertyDefinition') {
494-
classBody = /** @type {import('estree').ClassBody} */ (node.parent);
501+
if (nodeToCheck?.type === 'MethodDefinition' || nodeToCheck?.type === 'PropertyDefinition') {
502+
classBody = /** @type {import('estree').ClassBody} */ (nodeToCheck.parent);
495503
className = /** @type {import('estree').ClassDeclaration} */ (
496-
node.parent?.parent
504+
nodeToCheck.parent?.parent
497505
)?.id?.name;
498-
} else if (node?.type === 'ClassDeclaration' || node?.type === 'ClassExpression') {
499-
classBody = node.body;
500-
className = node.id?.name;
506+
} else if (nodeToCheck?.type === 'ClassDeclaration' || nodeToCheck?.type === 'ClassExpression') {
507+
classBody = nodeToCheck.body;
508+
className = nodeToCheck.id?.name;
501509
}
502510

503511
if (classBody) {

‎test/rules/assertions/noUndefinedTypes.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,22 @@ export default /** @type {import('../index.js').TestCases} */ ({
746746
},
747747
],
748748
},
749+
{
750+
code: `
751+
/** xyz {@link property} */
752+
export default function () {}
753+
`,
754+
errors: [
755+
{
756+
line: 2,
757+
message: 'The type \'property\' is undefined.',
758+
},
759+
],
760+
languageOptions: {
761+
ecmaVersion: 2_022,
762+
},
763+
},
764+
749765
],
750766
valid: [
751767
{
@@ -2273,5 +2289,27 @@ export default /** @type {import('../index.js').TestCases} */ ({
22732289
};
22742290
`,
22752291
},
2292+
{
2293+
code: `
2294+
/** xyz {@link property} */
2295+
export class MyClass {
2296+
public property: boolean
2297+
}
2298+
`,
2299+
languageOptions: {
2300+
parser: typescriptEslintParser,
2301+
},
2302+
},
2303+
{
2304+
code: `
2305+
/** xyz {@link property} */
2306+
export default class MyClass {
2307+
property = true;
2308+
}
2309+
`,
2310+
languageOptions: {
2311+
ecmaVersion: 2_022,
2312+
},
2313+
},
22762314
],
22772315
});

0 commit comments

Comments
 (0)