Skip to content

Commit

Permalink
fix(no-undefined-types): ensure working in all contexts; fixes #324
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jul 13, 2019
1 parent e9145c4 commit a1c9824
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/iterateJsdoc.js
Expand Up @@ -206,7 +206,11 @@ const getUtils = (
};

utils.getClassNode = () => {
const greatGrandParent = ancestors.slice(-3)[0];
// Ancestors missing in `Program` comment iteration
const greatGrandParent = ancestors.length ?
ancestors.slice(-3)[0] :
jsdocUtils.getAncestor(sourceCode, jsdocNode, 3);

const greatGrandParentValue = greatGrandParent && sourceCode.getFirstToken(greatGrandParent).value;

if (greatGrandParentValue === 'class') {
Expand Down Expand Up @@ -350,7 +354,7 @@ const iterateAllJsdocs = (iterator, ruleConfig) => {
return {
create (context) {
return {
'Program:exit' () {
'Program' () {
const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();

Expand Down
13 changes: 13 additions & 0 deletions src/jsdocUtils.js
Expand Up @@ -545,9 +545,22 @@ const getTagsByType = (tags, tagPreference) => {
};
};

const getAncestor = (sourceCode, nde, depth, idx = 0) => {
if (idx === depth) {
return nde;
}
const prevToken = sourceCode.getTokenBefore(nde);
if (prevToken) {
return getAncestor(sourceCode, prevToken, depth, idx + 1);
}

return null;
};

export default {
enforcedContexts,
filterTags,
getAncestor,
getContextObject,
getFunctionParameterNames,
getJsdocParameterNames,
Expand Down
1 change: 1 addition & 0 deletions src/rules/noUndefinedTypes.js
Expand Up @@ -132,6 +132,7 @@ export default iterateJsdoc(({
});
});
}, {
iterateAllJsdocs: true,
meta: {
schema: [
{
Expand Down
19 changes: 19 additions & 0 deletions test/rules/assertions/noUndefinedTypes.js
Expand Up @@ -208,6 +208,25 @@ export default {
message: 'The type \'TEMPLATE_TYPE\' is undefined.'
}
]
},
{
code: `
/**
* @type {strnig}
*/
var quux = {
};
`,
errors: [
{
line: 3,
message: 'The type \'strnig\' is undefined.'
}
],
rules: {
'no-undef': 'error'
}
}
],
valid: [
Expand Down

0 comments on commit a1c9824

Please sign in to comment.