Skip to content

Commit fed7586

Browse files
committed
fix(no-undefined-types): if no scope found, resume checking current node for template tags; fixes #578, fixes #579
1 parent 0d29a80 commit fed7586

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6284,6 +6284,25 @@ const init = () => {
62846284
return Promise.resolve('success');
62856285
}
62866286
};
6287+
6288+
/** Gets a Promise resolved with a given value.
6289+
*
6290+
* @template ValueType
6291+
* @param {ValueType} value Value to resolve.
6292+
* @returns {Promise<ValueType>} Promise resolved with value.
6293+
*/
6294+
exports.resolve1 = function resolve1(value) {
6295+
return Promise.resolve(value);
6296+
};
6297+
// Settings: {"jsdoc":{"mode":"typescript"}}
6298+
6299+
/**
6300+
* A function returning the same type as its argument.
6301+
*
6302+
* @template ValueType
6303+
* @typedef {ValueType} ValueFunc
6304+
*/
6305+
// Settings: {"jsdoc":{"mode":"typescript"}}
62876306
````
62886307
62896308

src/rules/noUndefinedTypes.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,28 @@ export default iterateJsdoc(({
7272

7373
const ancestorNodes = [];
7474
let currentScope = scopeManager.acquire(node);
75+
7576
while (currentScope && currentScope.block.type !== 'Program') {
7677
ancestorNodes.push(currentScope.block);
7778
currentScope = currentScope.upper;
7879
}
7980

80-
let templateTags = _(ancestorNodes).flatMap((ancestorNode) => {
81-
const commentNode = getJSDocComment(sourceCode, ancestorNode, settings);
82-
if (!commentNode) {
83-
return [];
84-
}
81+
// `currentScope` may be `null` or `Program`, so in such a case,
82+
// we look to present tags instead
83+
let templateTags = ancestorNodes.length ?
84+
_(ancestorNodes).flatMap((ancestorNode) => {
85+
const commentNode = getJSDocComment(sourceCode, ancestorNode, settings);
86+
if (!commentNode) {
87+
return [];
88+
}
8589

86-
const jsdoc = parseComment(commentNode, '');
90+
const jsdoc = parseComment(commentNode, '');
8791

88-
return jsdocUtils.filterTags(jsdoc.tags, (tag) => {
89-
return 'template' === tag.tag;
90-
});
91-
}).value();
92+
return jsdocUtils.filterTags(jsdoc.tags, (tag) => {
93+
return 'template' === tag.tag;
94+
});
95+
}).value() :
96+
utils.getPresentTags('template');
9297

9398
const classJsdoc = utils.getClassJsdoc();
9499
if (classJsdoc?.tags) {

test/rules/assertions/noUndefinedTypes.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,5 +740,44 @@ export default {
740740
es6: true,
741741
},
742742
},
743+
{
744+
code: `
745+
/** Gets a Promise resolved with a given value.
746+
*
747+
* @template ValueType
748+
* @param {ValueType} value Value to resolve.
749+
* @returns {Promise<ValueType>} Promise resolved with value.
750+
*/
751+
exports.resolve1 = function resolve1(value) {
752+
return Promise.resolve(value);
753+
};
754+
`,
755+
env: {
756+
es6: true,
757+
},
758+
settings: {
759+
jsdoc: {
760+
mode: 'typescript',
761+
},
762+
},
763+
},
764+
{
765+
code: `
766+
/**
767+
* A function returning the same type as its argument.
768+
*
769+
* @template ValueType
770+
* @typedef {ValueType} ValueFunc
771+
*/
772+
`,
773+
env: {
774+
es6: true,
775+
},
776+
settings: {
777+
jsdoc: {
778+
mode: 'typescript',
779+
},
780+
},
781+
},
743782
],
744783
};

0 commit comments

Comments
 (0)