Skip to content

Commit

Permalink
fix(valid-describe): ignore describe.each (#337)
Browse files Browse the repository at this point in the history
Fixes #334
  • Loading branch information
G-Rath authored and SimenB committed Jul 22, 2019
1 parent d0a8428 commit ed2a0f6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/rules/__tests__/valid-describe.test.ts
Expand Up @@ -9,6 +9,7 @@ const ruleTester = new TSESLint.RuleTester({

ruleTester.run('valid-describe', rule, {
valid: [
'describe.each()()',
'describe("foo", function() {})',
'describe("foo", () => {})',
'describe(`foo`, () => {})',
Expand Down
103 changes: 55 additions & 48 deletions src/rules/valid-describe.ts
@@ -1,6 +1,7 @@
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree';
import {
FunctionExpression,
JestFunctionCallExpression,
createRule,
isDescribe,
isFunction,
Expand All @@ -25,6 +26,11 @@ const paramsLocation = (
};
};

const isDescribeEach = (node: JestFunctionCallExpression) =>
node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
node.callee.property.name === 'each';

export default createRule({
name: __filename,
meta: {
Expand All @@ -50,63 +56,64 @@ export default createRule({
create(context) {
return {
CallExpression(node) {
if (isDescribe(node)) {
if (node.arguments.length === 0) {
return context.report({
messageId: 'nameAndCallback',
loc: node.loc,
});
}
if (!isDescribe(node) || isDescribeEach(node)) {
return;
}

if (node.arguments.length === 0) {
return context.report({
messageId: 'nameAndCallback',
loc: node.loc,
});
}
const [name] = node.arguments;
const [, callbackFunction] = node.arguments;
if (!isString(name)) {
context.report({
messageId: 'firstArgumentMustBeName',
loc: paramsLocation(node.arguments),
});
}
if (!callbackFunction) {
context.report({
messageId: 'nameAndCallback',
loc: paramsLocation(node.arguments),
});

const [name] = node.arguments;
const [, callbackFunction] = node.arguments;
if (!isString(name)) {
return;
}
if (isFunction(callbackFunction)) {
if (isAsync(callbackFunction)) {
context.report({
messageId: 'firstArgumentMustBeName',
loc: paramsLocation(node.arguments),
messageId: 'noAsyncDescribeCallback',
node: callbackFunction,
});
}
if (!callbackFunction) {
if (hasParams(callbackFunction)) {
context.report({
messageId: 'nameAndCallback',
loc: paramsLocation(node.arguments),
messageId: 'unexpectedDescribeArgument',
loc: paramsLocation(callbackFunction.params),
});

return;
}
if (isFunction(callbackFunction)) {
if (isAsync(callbackFunction)) {
context.report({
messageId: 'noAsyncDescribeCallback',
node: callbackFunction,
});
}
if (hasParams(callbackFunction)) {
context.report({
messageId: 'unexpectedDescribeArgument',
loc: paramsLocation(callbackFunction.params),
});
}
if (
callbackFunction.body &&
callbackFunction.body.type === AST_NODE_TYPES.BlockStatement
) {
callbackFunction.body.body.forEach(node => {
if (node.type === 'ReturnStatement') {
context.report({
messageId: 'unexpectedReturnInDescribe',
node,
});
}
});
}
} else {
context.report({
messageId: 'secondArgumentMustBeFunction',
loc: paramsLocation(node.arguments),
if (
callbackFunction.body &&
callbackFunction.body.type === AST_NODE_TYPES.BlockStatement
) {
callbackFunction.body.body.forEach(node => {
if (node.type === 'ReturnStatement') {
context.report({
messageId: 'unexpectedReturnInDescribe',
node,
});
}
});
return;
}
} else {
context.report({
messageId: 'secondArgumentMustBeFunction',
loc: paramsLocation(node.arguments),
});
return;
}
},
};
Expand Down

0 comments on commit ed2a0f6

Please sign in to comment.