Skip to content

Commit d26fdf0

Browse files
committed
fix(no-undefined-types): add line info to inline tags; fixes #1079
1 parent 46fcd7e commit d26fdf0

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

docs/rules/no-undefined-types.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,24 @@ function foo () {
283283
const a = new Todo();
284284
// Settings: {"jsdoc":{"mode":"jsdoc"}}
285285
// Message: The type 'Omit' is undefined.
286+
287+
/**
288+
* Message with {@link NotKnown}
289+
*/
290+
// Message: The type 'NotKnown' is undefined.
291+
292+
/**
293+
* Message with
294+
* a link that is {@link NotKnown}
295+
*/
296+
// Message: The type 'NotKnown' is undefined.
297+
298+
/**
299+
* @abc
300+
* @someTag Message with
301+
* a link that is {@link NotKnown}
302+
*/
303+
// Message: The type 'NotKnown' is undefined.
286304
````
287305

288306

src/jsdocUtils.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,54 @@ const hasTag = (jsdoc, targetTagName) => {
533533
const getAllTags = (jsdoc, includeInlineTags) => {
534534
return includeInlineTags ? [
535535
...jsdoc.tags,
536-
...jsdoc.inlineTags,
536+
...jsdoc.inlineTags.map((inlineTag) => {
537+
// Tags don't have source or line numbers, so add before returning
538+
let line = -1;
539+
for (const {
540+
tokens: {
541+
description,
542+
},
543+
} of jsdoc.source) {
544+
line++;
545+
if (description && description.includes(`{@${inlineTag.tag}`)) {
546+
break;
547+
}
548+
}
549+
550+
inlineTag.line = line;
551+
552+
return inlineTag;
553+
}),
537554
...jsdoc.tags.flatMap((tag) => {
555+
let tagBegins = -1;
556+
for (const {
557+
tokens: {
558+
tag: tg,
559+
},
560+
} of jsdoc.source) {
561+
tagBegins++;
562+
if (tg) {
563+
break;
564+
}
565+
}
566+
567+
for (const inlineTag of tag.inlineTags) {
568+
let line;
569+
for (const {
570+
number,
571+
tokens: {
572+
description,
573+
},
574+
} of tag.source) {
575+
if (description && description.includes(`{@${inlineTag.tag}`)) {
576+
line = number;
577+
break;
578+
}
579+
}
580+
581+
inlineTag.line = tagBegins + line - 1;
582+
}
583+
538584
return (
539585
/**
540586
* @type {import('comment-parser').Spec & {

test/rules/assertions/noUndefinedTypes.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,48 @@ export default {
490490
},
491491
},
492492
},
493+
{
494+
code: `
495+
/**
496+
* Message with {@link NotKnown}
497+
*/
498+
`,
499+
errors: [
500+
{
501+
line: 3,
502+
message: 'The type \'NotKnown\' is undefined.',
503+
},
504+
],
505+
},
506+
{
507+
code: `
508+
/**
509+
* Message with
510+
* a link that is {@link NotKnown}
511+
*/
512+
`,
513+
errors: [
514+
{
515+
line: 4,
516+
message: 'The type \'NotKnown\' is undefined.',
517+
},
518+
],
519+
},
520+
{
521+
code: `
522+
/**
523+
* @abc
524+
* @someTag Message with
525+
* a link that is {@link NotKnown}
526+
*/
527+
`,
528+
errors: [
529+
{
530+
line: 5,
531+
message: 'The type \'NotKnown\' is undefined.',
532+
},
533+
],
534+
},
493535
],
494536
valid: [
495537
{

0 commit comments

Comments
 (0)