Skip to content

Commit

Permalink
fix(return-yields, return-yields-check): allow generator detectio…
Browse files Browse the repository at this point in the history
…n on exported; handle more AST types; fixes gajus#682
  • Loading branch information
brettz9 committed Jan 31, 2021
1 parent e694dfd commit bdc2ea5
Show file tree
Hide file tree
Showing 3 changed files with 434 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ const getUtils = (
utils.isGenerator = () => {
return node && (
node.generator ||
node.type === 'MethodDefinition' && node.value.generator
node.type === 'MethodDefinition' && node.value.generator ||
['ExportNamedDeclaration', 'ExportDefaultDeclaration'].includes(node.type) &&
node.declaration.generator
);
};

Expand Down Expand Up @@ -462,6 +464,10 @@ const getUtils = (
};

utils.hasYieldValue = () => {
if (['ExportNamedDeclaration', 'ExportDefaultDeclaration'].includes(node.type)) {
return jsdocUtils.hasYieldValue(node.declaration);
}

return jsdocUtils.hasYieldValue(node);
};

Expand Down Expand Up @@ -891,7 +897,6 @@ export default function iterateJsdoc (iterator, ruleConfig) {
return iterateAllJsdocs(iterator, ruleConfig).create(context);
}
}

const sourceCode = context.getSourceCode();
const settings = getSettings(context);
if (!settings) {
Expand Down
73 changes: 72 additions & 1 deletion src/jsdocUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ const hasNonEmptyResolverCall = (node, resolverName) => {
case 'AssignmentPattern':
return hasNonEmptyResolverCall(node.right, resolverName);

// Todo: Review all of the types below this for adapting to yields checks
case 'AssignmentExpression':
case 'BinaryExpression':
case 'LogicalExpression': {
Expand Down Expand Up @@ -811,6 +810,13 @@ const hasNonFunctionYield = (node, checkYieldReturnValue) => {
);
});
}
// istanbul ignore next -- In Babel?
case 'OptionalCallExpression':
case 'CallExpression':
return node.arguments.some((element) => {
return hasNonFunctionYield(element, checkYieldReturnValue);
});
case 'ChainExpression':
case 'ExpressionStatement': {
return hasNonFunctionYield(node.expression, checkYieldReturnValue);
}
Expand Down Expand Up @@ -860,6 +866,71 @@ const hasNonFunctionYield = (node, checkYieldReturnValue) => {
return hasNonFunctionYield(node.id, checkYieldReturnValue) ||
hasNonFunctionYield(node.init, checkYieldReturnValue);
}

case 'AssignmentExpression':
case 'BinaryExpression':
case 'LogicalExpression': {
return hasNonFunctionYield(node.left, checkYieldReturnValue) ||
hasNonFunctionYield(node.right, checkYieldReturnValue);
}

// Comma
case 'SequenceExpression':
case 'TemplateLiteral':
return node.expressions.some((subExpression) => {
return hasNonFunctionYield(subExpression, checkYieldReturnValue);
});

case 'ObjectPattern':
case 'ObjectExpression':
return node.properties.some((property) => {
return hasNonFunctionYield(property, checkYieldReturnValue);
});

// istanbul ignore next -- In Babel?
case 'ObjectProperty':
/* eslint-disable no-fallthrough */
// istanbul ignore next -- In Babel?
case 'ClassProperty':
/* eslint-enable no-fallthrough */
case 'Property':
return node.computed && hasNonFunctionYield(node.key, checkYieldReturnValue) ||
hasNonFunctionYield(node.value, checkYieldReturnValue);
// istanbul ignore next -- In Babel?
case 'ObjectMethod':
// istanbul ignore next -- In Babel?
return node.computed && hasNonFunctionYield(node.key, checkYieldReturnValue) ||
node.arguments.some((nde) => {
return hasNonFunctionYield(nde, checkYieldReturnValue);
});

case 'SpreadElement':
case 'UnaryExpression':
return hasNonFunctionYield(node.argument, checkYieldReturnValue);

case 'TaggedTemplateExpression':
return hasNonFunctionYield(node.quasi, checkYieldReturnValue);

// ?.
// istanbul ignore next -- In Babel?
case 'OptionalMemberExpression':
case 'MemberExpression':
return hasNonFunctionYield(node.object, checkYieldReturnValue) ||
hasNonFunctionYield(node.property, checkYieldReturnValue);

// istanbul ignore next -- In Babel?
case 'Import':
case 'ImportExpression':
return hasNonFunctionYield(node.source, checkYieldReturnValue);

case 'ReturnStatement': {
if (node.argument === null) {
return false;
}

return hasNonFunctionYield(node.argument, checkYieldReturnValue);
}

case 'YieldExpression': {
if (checkYieldReturnValue) {
if (node.parent.type === 'VariableDeclarator') {
Expand Down

0 comments on commit bdc2ea5

Please sign in to comment.