Skip to content

Commit 4c19b9a

Browse files
l1bbcsgbrettz9
authored andcommitted
Parse multiple template types in one tag
1 parent 038f0f2 commit 4c19b9a

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/jsdocUtils.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,22 @@ const isInlineTag = (tag) => {
461461
};
462462
*/
463463

464+
/**
465+
* Parses GCC Generic/Template types
466+
*
467+
* @see {https://github.com/google/closure-compiler/wiki/Generic-Types}
468+
* @param {JsDocTag} tag
469+
* @returns {Array<string>}
470+
*/
471+
const parseClosureTemplateTag = (tag) => {
472+
return tag.source
473+
.split('@template')[1]
474+
.split(',')
475+
.map((type) => {
476+
return type.trim();
477+
});
478+
};
479+
464480
export default {
465481
getFunctionParameterNames,
466482
getJsdocParameterNames,
@@ -474,5 +490,6 @@ export default {
474490
isNamepathTag,
475491
isPotentiallyEmptyNamepathTag,
476492
isTagWithType,
477-
isValidTag
493+
isValidTag,
494+
parseClosureTemplateTag
478495
};

src/rules/noUndefinedTypes.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'flat-map-polyfill';
33
import _ from 'lodash';
44
import {parse as parseType, traverse} from 'jsdoctypeparser';
55
import iterateJsdoc, {parseComment} from '../iterateJsdoc';
6+
import jsdocUtils from '../jsdocUtils';
67

78
const extraTypes = [
89
'null', 'undefined', 'string', 'boolean', 'object',
@@ -70,12 +71,12 @@ export default iterateJsdoc(({
7071
let closureGenericTypes = [];
7172
const classJsdoc = utils.getClassJsdoc();
7273
if (classJsdoc && classJsdoc.tags) {
73-
closureGenericTypes = classJsdoc.tags
74+
classJsdoc.tags
7475
.filter((tag) => {
7576
return tag.tag === 'template';
7677
})
77-
.map((tag) => {
78-
return tag.name;
78+
.forEach((tag) => {
79+
closureGenericTypes = closureGenericTypes.concat(jsdocUtils.parseClosureTemplateTag(tag));
7980
});
8081
}
8182

test/rules/assertions/noUndefinedTypes.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,21 @@ export default {
407407
}
408408
}
409409
`
410+
},
411+
{
412+
code: `
413+
/**
414+
* @template TEMPLATE_TYPE_A, TEMPLATE_TYPE_B
415+
*/
416+
class Foo {
417+
/**
418+
* @param {TEMPLATE_TYPE_A} baz
419+
* @return {TEMPLATE_TYPE_B}
420+
*/
421+
bar (baz) {
422+
}
423+
}
424+
`
410425
}
411426
]
412427
};

0 commit comments

Comments
 (0)