Skip to content

Commit 84168fe

Browse files
committed
Checking and marking types from inline links
1 parent 3bcee32 commit 84168fe

File tree

6 files changed

+140
-4
lines changed

6 files changed

+140
-4
lines changed

docs/rules/no-undefined-types.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,5 +745,29 @@ function quux(foo, bar) {
745745

746746
}
747747
// "jsdoc/no-undefined-types": ["error"|"warn", {"disableReporting":true}]
748+
749+
class MyClass {}
750+
class AnotherClass {}
751+
752+
/**
753+
* A description mentioning {@link MyClass} and {@link AnotherClass | another class} and a URL via [this link]{@link https://www.example.com}.
754+
*/
755+
function quux(foo) {
756+
console.log(foo);
757+
}
758+
759+
quux(0);
760+
761+
class MyClass {}
762+
class AnotherClass {}
763+
764+
/**
765+
* @see A tag mentioning {@link MyClass} and {@link AnotherClass | another class} and a URL via [this link]{@link https://www.example.com}.
766+
*/
767+
function quux(foo) {
768+
console.log(foo);
769+
}
770+
771+
quux(0);
748772
````
749773

src/getDefaultTagStructureForMode.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ const getDefaultTagStructureForMode = (mode) => {
3535
// "namepath" (e.g., param can't define a namepath)
3636

3737
// Once checking inline tags:
38-
// Todo: Re: `typeOrNameRequired`, `@link` (or @linkcode/@linkplain) seems
39-
// to require a namepath OR URL and might be checked as such.
4038
// Todo: Should support a `tutorialID` type (for `@tutorial` block and
4139
// inline)
4240

@@ -461,6 +459,65 @@ const getDefaultTagStructureForMode = (mode) => {
461459
]),
462460
],
463461

462+
[
463+
'link', new Map([
464+
// Signature seems to require a namepath OR URL and might be checked as such.
465+
[
466+
'nameContents', 'namepath-referencing',
467+
],
468+
469+
// "namepath"
470+
[
471+
'typeOrNameRequired', true,
472+
],
473+
474+
// "type"
475+
[
476+
'typeAllowed', true,
477+
],
478+
]),
479+
],
480+
481+
[
482+
'linkcode', new Map([
483+
// Synonym for "link"
484+
// Signature seems to require a namepath OR URL and might be checked as such.
485+
[
486+
'nameContents', 'namepath-referencing',
487+
],
488+
489+
// "namepath"
490+
[
491+
'typeOrNameRequired', true,
492+
],
493+
494+
// "type"
495+
[
496+
'typeAllowed', true,
497+
],
498+
]),
499+
],
500+
501+
[
502+
'linkplain', new Map([
503+
// Synonym for "link"
504+
// Signature seems to require a namepath OR URL and might be checked as such.
505+
[
506+
'nameContents', 'namepath-referencing',
507+
],
508+
509+
// "namepath"
510+
[
511+
'typeOrNameRequired', true,
512+
],
513+
514+
// "type"
515+
[
516+
'typeAllowed', true,
517+
],
518+
]),
519+
],
520+
464521
[
465522
'listens', new Map([
466523
// Signature seems to require a "name" (of an event) and no

src/iterateJsdoc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ const getUtils = (
869869
};
870870

871871
utils.filterTags = (filter) => {
872-
return jsdocUtils.filterTags(jsdoc.tags, filter);
872+
return jsdocUtils.filterTags(jsdocUtils.getAllTags(jsdoc), filter);
873873
};
874874

875875
utils.getTagsByType = (tags) => {

src/jsdocUtils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,22 @@ const hasTag = (jsdoc, targetTagName) => {
452452
});
453453
};
454454

455+
/**
456+
* Get all tags, inline tags and inline tags in tags
457+
*
458+
* @param {object} jsdoc
459+
* @returns {Array}
460+
*/
461+
const getAllTags = (jsdoc) => {
462+
return [
463+
...jsdoc.tags,
464+
...jsdoc.inlineTags,
465+
...jsdoc.tags.flatMap((tag) => {
466+
return tag.inlineTags;
467+
}),
468+
];
469+
};
470+
455471
/**
456472
* @param {object} jsdoc
457473
* @param {Array} targetTagNames
@@ -1232,6 +1248,7 @@ export default {
12321248
exemptSpeciaMethods,
12331249
filterTags,
12341250
flattenRoots,
1251+
getAllTags,
12351252
getContextObject,
12361253
getFunctionParameterNames,
12371254
getIndent,

src/rules/noUndefinedTypes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,12 @@ export default iterateJsdoc(({
193193
});
194194

195195
for (const tag of jsdocTagsWithPossibleType) {
196+
const possibleType = tag.type || tag.namepathOrURL;
197+
196198
let parsedType;
197199

198200
try {
199-
parsedType = mode === 'permissive' ? tryParseType(tag.type) : parseType(tag.type, mode);
201+
parsedType = mode === 'permissive' ? tryParseType(possibleType) : parseType(possibleType, mode);
200202
} catch {
201203
// On syntax error, will be handled by valid-types.
202204
continue;

test/rules/assertions/noUndefinedTypes.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,5 +1324,41 @@ export default {
13241324
sourceType: 'module',
13251325
},
13261326
},
1327+
{
1328+
code: `
1329+
class MyClass {}
1330+
class AnotherClass {}
1331+
1332+
/**
1333+
* A description mentioning {@link MyClass} and {@link AnotherClass | another class} and a URL via [this link]{@link https://www.example.com}.
1334+
*/
1335+
function quux(foo) {
1336+
console.log(foo);
1337+
}
1338+
1339+
quux(0);
1340+
`,
1341+
rules: {
1342+
'no-unused-vars': 'error',
1343+
},
1344+
},
1345+
{
1346+
code: `
1347+
class MyClass {}
1348+
class AnotherClass {}
1349+
1350+
/**
1351+
* @see A tag mentioning {@link MyClass} and {@link AnotherClass | another class} and a URL via [this link]{@link https://www.example.com}.
1352+
*/
1353+
function quux(foo) {
1354+
console.log(foo);
1355+
}
1356+
1357+
quux(0);
1358+
`,
1359+
rules: {
1360+
'no-unused-vars': 'error',
1361+
},
1362+
},
13271363
],
13281364
};

0 commit comments

Comments
 (0)