Skip to content

Commit a5a0a3c

Browse files
authored
fix(no-undefined-types): do not treat type parameters or their references as undefined; #1215 (#1553)
1 parent 6efd9e9 commit a5a0a3c

File tree

5 files changed

+86
-13
lines changed

5 files changed

+86
-13
lines changed

docs/rules/no-undefined-types.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,29 @@ class Filler {
384384
/** @type {AnotherType} */
385385
// "jsdoc/no-undefined-types": ["error"|"warn", {"checkUsedTypedefs":true}]
386386
// Message: This typedef was not used within the file
387+
388+
/** @typedef {'cwd'} */
389+
let MyOwnType1
390+
391+
/**
392+
* @param {`${MyOwnType1}-${string}`} tagName
393+
* @param {CustomElementConstructor} component
394+
*/
395+
let defineCustomElement = (tagName, component) => {
396+
customElements.define(tagName, component)
397+
}
398+
399+
/** @typedef {string} */
400+
let MyOwnType2
401+
402+
/**
403+
* @param {<T extends unknown>(element: MyOwnType2) => T} callback
404+
* @returns {void}
405+
*/
406+
let getValue = (callback) => {
407+
callback(`hello`)
408+
}
409+
// Message: The type 'CustomElementConstructor' is undefined.
387410
````
388411

389412

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"url": "http://gajus.com"
66
},
77
"dependencies": {
8-
"@es-joy/jsdoccomment": "~0.68.1",
8+
"@es-joy/jsdoccomment": "~0.69.0",
99
"are-docs-informative": "^0.0.2",
1010
"comment-parser": "1.4.1",
1111
"debug": "^4.4.3",
@@ -58,7 +58,7 @@
5858
"glob": "^11.0.3",
5959
"globals": "^16.4.0",
6060
"husky": "^9.1.7",
61-
"jsdoc-type-pratt-parser": "^6.3.3",
61+
"jsdoc-type-pratt-parser": "^6.4.0",
6262
"json-schema": "^0.4.0",
6363
"json-schema-to-typescript": "^15.0.4",
6464
"lint-staged": "^16.2.3",

pnpm-lock.yaml

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rules/noUndefinedTypes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,25 @@ export default iterateJsdoc(({
512512
if (!allDefinedTypes.has(val) &&
513513
(!Array.isArray(structuredTypes) || !structuredTypes.includes(val))
514514
) {
515+
const parent =
516+
/**
517+
* @type {import('jsdoc-type-pratt-parser').RootResult & {
518+
* _parent?: import('jsdoc-type-pratt-parser').NonRootResult
519+
* }}
520+
*/ (nde)._parent;
521+
if (parent?.type === 'JsdocTypeTypeParameter') {
522+
return;
523+
}
524+
525+
if (parent?.type === 'JsdocTypeFunction' &&
526+
/** @type {import('jsdoc-type-pratt-parser').FunctionResult} */
527+
(parent)?.typeParameters?.some((typeParam) => {
528+
return value === typeParam.name.value;
529+
})
530+
) {
531+
return;
532+
}
533+
515534
if (!disableReporting) {
516535
report(`The type '${val}' is undefined.`, null, tag);
517536
}

test/rules/assertions/noUndefinedTypes.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,37 @@ export default /** @type {import('../index.js').TestCases} */ ({
670670
},
671671
],
672672
},
673+
{
674+
code: `
675+
/** @typedef {'cwd'} */
676+
let MyOwnType1
677+
678+
/**
679+
* @param {\`\${MyOwnType1}-\${string}\`} tagName
680+
* @param {CustomElementConstructor} component
681+
*/
682+
let defineCustomElement = (tagName, component) => {
683+
customElements.define(tagName, component)
684+
}
685+
686+
/** @typedef {string} */
687+
let MyOwnType2
688+
689+
/**
690+
* @param {<T extends unknown>(element: MyOwnType2) => T} callback
691+
* @returns {void}
692+
*/
693+
let getValue = (callback) => {
694+
callback(\`hello\`)
695+
}
696+
`,
697+
errors: [
698+
{
699+
line: 7,
700+
message: 'The type \'CustomElementConstructor\' is undefined.',
701+
},
702+
],
703+
},
673704
],
674705
valid: [
675706
{

0 commit comments

Comments
 (0)