Skip to content

Commit dce0d89

Browse files
SulimanAbdulrazzaqbrettz9
authored andcommitted
fix(no-undefined-types): treat type parameters of function types as defined anywhere within the function type
1 parent 6a4c8fe commit dce0d89

3 files changed

Lines changed: 70 additions & 7 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,13 @@ export default function () {}
456456
export const d = [];
457457
// Settings: {"jsdoc":{"mode":"jsdoc"}}
458458
// Message: The type 'ReadonlyArray' is undefined.
459+
460+
/**
461+
* @param {{a: <T>(x: T) => T, b: T}} obj
462+
* @param {{m<U>(): U, n(): U}} obj2
463+
*/
464+
function quux (obj, obj2) {}
465+
// Message: The type 'T' is undefined.
459466
````
460467

461468

@@ -1439,5 +1446,17 @@ export async function * g (source) {}
14391446
function get (x) {
14401447
return x;
14411448
}
1449+
1450+
/**
1451+
* @param {<T>(value: T, cb?: (value: T) => string) => void} a
1452+
* @param {<T, U>(x: T[], f: (t: T) => U) => Promise<U[]>} b
1453+
* @param {<K extends string>(k: K) => {[P in K]: number}} c
1454+
* @param {{<T>(x: T): T}} d
1455+
* @param {{map<U>(f: (x: number) => U): U[]}} e
1456+
* @param {{new <T>(x: T): T}} f
1457+
* @param {<T>() => <U>(x: U) => [T, U]} g
1458+
* @returns {void}
1459+
*/
1460+
function quux (a, b, c, d, e, f, g) {}
14421461
````
14431462

‎src/rules/noUndefinedTypes.js‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -914,13 +914,24 @@ export default iterateJsdoc(({
914914
return;
915915
}
916916

917-
if (parent?.type === 'JsdocTypeFunction' &&
918-
/** @type {import('jsdoc-type-pratt-parser').FunctionResult} */
919-
(parent)?.typeParameters?.some((typeParam) => {
917+
// Type parameters of a function type or signature (e.g.,
918+
// `<T>(x: T) => T` or `{<T>(x: T): T}`) are in scope anywhere
919+
// within that function type
920+
/**
921+
* @type {import('jsdoc-type-pratt-parser').NonRootResult & {
922+
* _parent?: import('jsdoc-type-pratt-parser').NonRootResult,
923+
* typeParameters?: import('jsdoc-type-pratt-parser').TypeParameterResult[]
924+
* }|undefined}
925+
*/
926+
let ancestor = parent;
927+
while (ancestor) {
928+
if (ancestor.typeParameters?.some((typeParam) => {
920929
return value === typeParam.name.value;
921-
})
922-
) {
923-
return;
930+
})) {
931+
return;
932+
}
933+
934+
ancestor = ancestor._parent;
924935
}
925936

926937
if (parent?.type === 'JsdocTypeInfer' && value === parent.element.value) {

‎test/rules/assertions/noUndefinedTypes.js‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,25 @@ export default /** @type {import('../index.js').TestCases} */ ({
804804
},
805805
},
806806
},
807-
807+
{
808+
code: `
809+
/**
810+
* @param {{a: <T>(x: T) => T, b: T}} obj
811+
* @param {{m<U>(): U, n(): U}} obj2
812+
*/
813+
function quux (obj, obj2) {}
814+
`,
815+
errors: [
816+
{
817+
line: 3,
818+
message: 'The type \'T\' is undefined.',
819+
},
820+
{
821+
line: 4,
822+
message: 'The type \'U\' is undefined.',
823+
},
824+
],
825+
},
808826
],
809827
valid: [
810828
{
@@ -2497,5 +2515,20 @@ export default /** @type {import('../index.js').TestCases} */ ({
24972515
}
24982516
`,
24992517
},
2518+
{
2519+
code: `
2520+
/**
2521+
* @param {<T>(value: T, cb?: (value: T) => string) => void} a
2522+
* @param {<T, U>(x: T[], f: (t: T) => U) => Promise<U[]>} b
2523+
* @param {<K extends string>(k: K) => {[P in K]: number}} c
2524+
* @param {{<T>(x: T): T}} d
2525+
* @param {{map<U>(f: (x: number) => U): U[]}} e
2526+
* @param {{new <T>(x: T): T}} f
2527+
* @param {<T>() => <U>(x: U) => [T, U]} g
2528+
* @returns {void}
2529+
*/
2530+
function quux (a, b, c, d, e, f, g) {}
2531+
`,
2532+
},
25002533
],
25012534
});

0 commit comments

Comments
 (0)