Skip to content

Commit 35e95a1

Browse files
authored
fix(require-template, no-undefined-types, valid-types): properly parse template tags with defaults; fixes #1418 (#1419)
1 parent efbcb1c commit 35e95a1

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

docs/rules/require-template.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,5 +354,13 @@ export default class <NumType> {
354354
* @callback
355355
* @returns {[Something | undefined]}
356356
*/
357+
358+
/**
359+
* @template {string | Buffer} [T=string, U=number]
360+
* @typedef {object} Dirent
361+
* @property {T} name name
362+
* @property {U} aNumber number
363+
* @property {string} parentPath path
364+
*/
357365
````
358366

src/jsdocUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ const parseClosureTemplateTag = (tag) => {
14951495
return tag.name
14961496
.split(',')
14971497
.map((type) => {
1498-
return type.trim().replace(/^\[(?<name>.*?)=.*\]$/u, '$<name>');
1498+
return type.trim().replace(/^\[?(?<name>.*?)=.*$/u, '$<name>');
14991499
});
15001500
};
15011501

src/rules/requireTemplate.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ export default iterateJsdoc(({
2222

2323
const usedNames = new Set();
2424
const templateTags = utils.getTags('template');
25-
const templateNames = templateTags.flatMap(({
26-
name,
27-
}) => {
28-
return name.split(/,\s*/u);
25+
const templateNames = templateTags.flatMap((tag) => {
26+
return utils.parseClosureTemplateTag(tag);
2927
});
3028

31-
for (const tag of templateTags) {
32-
const {
33-
name,
34-
} = tag;
35-
const names = name.split(/,\s*/u);
36-
if (requireSeparateTemplates && names.length > 1) {
37-
report(`Missing separate @template for ${names[1]}`, null, tag);
29+
if (requireSeparateTemplates) {
30+
for (const tag of templateTags) {
31+
const {
32+
name,
33+
} = tag;
34+
const names = name.split(/,\s*/u);
35+
if (names.length > 1) {
36+
report(`Missing separate @template for ${names[1]}`, null, tag);
37+
}
3838
}
3939
}
4040

test/rules/assertions/requireTemplate.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,5 +634,16 @@ export default /** @type {import('../index.js').TestCases} */ ({
634634
*/
635635
`,
636636
},
637+
{
638+
code: `
639+
/**
640+
* @template {string | Buffer} [T=string, U=number]
641+
* @typedef {object} Dirent
642+
* @property {T} name name
643+
* @property {U} aNumber number
644+
* @property {string} parentPath path
645+
*/
646+
`,
647+
},
637648
],
638649
});

0 commit comments

Comments
 (0)