Skip to content

Commit a1c9824

Browse files
committed
fix(no-undefined-types): ensure working in all contexts; fixes #324
1 parent e9145c4 commit a1c9824

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/iterateJsdoc.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ const getUtils = (
206206
};
207207

208208
utils.getClassNode = () => {
209-
const greatGrandParent = ancestors.slice(-3)[0];
209+
// Ancestors missing in `Program` comment iteration
210+
const greatGrandParent = ancestors.length ?
211+
ancestors.slice(-3)[0] :
212+
jsdocUtils.getAncestor(sourceCode, jsdocNode, 3);
213+
210214
const greatGrandParentValue = greatGrandParent && sourceCode.getFirstToken(greatGrandParent).value;
211215

212216
if (greatGrandParentValue === 'class') {
@@ -350,7 +354,7 @@ const iterateAllJsdocs = (iterator, ruleConfig) => {
350354
return {
351355
create (context) {
352356
return {
353-
'Program:exit' () {
357+
'Program' () {
354358
const sourceCode = context.getSourceCode();
355359
const comments = sourceCode.getAllComments();
356360

src/jsdocUtils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,22 @@ const getTagsByType = (tags, tagPreference) => {
545545
};
546546
};
547547

548+
const getAncestor = (sourceCode, nde, depth, idx = 0) => {
549+
if (idx === depth) {
550+
return nde;
551+
}
552+
const prevToken = sourceCode.getTokenBefore(nde);
553+
if (prevToken) {
554+
return getAncestor(sourceCode, prevToken, depth, idx + 1);
555+
}
556+
557+
return null;
558+
};
559+
548560
export default {
549561
enforcedContexts,
550562
filterTags,
563+
getAncestor,
551564
getContextObject,
552565
getFunctionParameterNames,
553566
getJsdocParameterNames,

src/rules/noUndefinedTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export default iterateJsdoc(({
132132
});
133133
});
134134
}, {
135+
iterateAllJsdocs: true,
135136
meta: {
136137
schema: [
137138
{

test/rules/assertions/noUndefinedTypes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ export default {
208208
message: 'The type \'TEMPLATE_TYPE\' is undefined.'
209209
}
210210
]
211+
},
212+
{
213+
code: `
214+
/**
215+
* @type {strnig}
216+
*/
217+
var quux = {
218+
219+
};
220+
`,
221+
errors: [
222+
{
223+
line: 3,
224+
message: 'The type \'strnig\' is undefined.'
225+
}
226+
],
227+
rules: {
228+
'no-undef': 'error'
229+
}
211230
}
212231
],
213232
valid: [

0 commit comments

Comments
 (0)