Skip to content

Commit 4fa4bde

Browse files
committed
Parse multiple template types in one tag
1 parent d846b22 commit 4fa4bde

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
@@ -455,6 +455,22 @@ const isInlineTag = (tag) => {
455455
};
456456
*/
457457

458+
/**
459+
* Parses GCC Generic/Template types
460+
*
461+
* @see {https://github.com/google/closure-compiler/wiki/Generic-Types}
462+
* @param {JsDocTag} tag
463+
* @returns {Array<string>}
464+
*/
465+
const parseClosureTemplateTag = (tag) => {
466+
return tag.source
467+
.split('@template')[1]
468+
.split(',')
469+
.map((type) => {
470+
return type.trim();
471+
});
472+
};
473+
458474
export default {
459475
getFunctionParameterNames,
460476
getJsdocParameterNames,
@@ -468,5 +484,6 @@ export default {
468484
isNamepathTag,
469485
isPotentiallyEmptyNamepathTag,
470486
isTagWithType,
471-
isValidTag
487+
isValidTag,
488+
parseClosureTemplateTag
472489
};

src/rules/noUndefinedTypes.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash';
22
import {parse as parseType, traverse} from 'jsdoctypeparser';
33
import iterateJsdoc, {parseComment} from '../iterateJsdoc';
4+
import jsdocUtils from '../jsdocUtils';
45

56
const extraTypes = [
67
'null', 'undefined', 'string', 'boolean', 'object',
@@ -63,12 +64,12 @@ export default iterateJsdoc(({
6364
let closureGenericTypes = [];
6465
const classJsdoc = utils.getClassJsdoc();
6566
if (classJsdoc && classJsdoc.tags) {
66-
closureGenericTypes = classJsdoc.tags
67+
classJsdoc.tags
6768
.filter((tag) => {
6869
return tag.tag === 'template';
6970
})
70-
.map((tag) => {
71-
return tag.name;
71+
.forEach((tag) => {
72+
closureGenericTypes = closureGenericTypes.concat(jsdocUtils.parseClosureTemplateTag(tag));
7273
});
7374
}
7475

test/rules/assertions/noUndefinedTypes.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ export default {
340340
}
341341
}
342342
`
343+
},
344+
{
345+
code: `
346+
/**
347+
* @template TEMPLATE_TYPE_A, TEMPLATE_TYPE_B
348+
*/
349+
class Foo {
350+
/**
351+
* @param {TEMPLATE_TYPE_A} baz
352+
* @return {TEMPLATE_TYPE_B}
353+
*/
354+
bar (baz) {
355+
}
356+
}
357+
`
343358
}
344359
]
345360
};

0 commit comments

Comments
 (0)