Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Don't report nested arrow functions in jest/no-if #331

Merged
merged 6 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
## Unreleased

### Changed

- [Major] Updated to eslint v6, enabled `no-console` and enabled `no-async-promise-executor` ([330](https://github.com/Shopify/eslint-plugin-shopify/pull/330))
- Enabled `typescript/interface-name-prefix` to prevent `I` prefixes in TypeScript interface names

### Fixed
- [Patch] Fix `jest/no-if` from falsely reporting if statements inside of arrow functions ([331](https://github.com/Shopify/eslint-plugin-shopify/pull/331))

## [29.0.2] - 2019-06-18

### Changed
Expand Down
20 changes: 19 additions & 1 deletion lib/rules/jest/no-if.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {docsUrl} = require('../../utilities');
const {isTestDefinition} = require('../../utilities/jest');
const {isTestDefinition, isTestArrowFunction} = require('../../utilities/jest');

module.exports = {
meta: {
Expand Down Expand Up @@ -56,11 +56,29 @@ module.exports = {
CallExpression(node) {
stack.push(isTestDefinition(node));
},
FunctionExpression() {
stack.push(false);
},
FunctionDeclaration() {
stack.push(false);
},
ArrowFunctionExpression(node) {
stack.push(isTestArrowFunction(node));
},
IfStatement: validate,
ConditionalExpression: validate,
'CallExpression:exit': function() {
stack.pop();
},
'FunctionExpression:exit': function() {
stack.pop();
},
'FunctionDeclaration:exit': function() {
stack.pop();
},
'ArrowFunctionExpression:exit': function() {
stack.pop();
},
};
},
};
9 changes: 9 additions & 0 deletions lib/utilities/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ function isTestDefinition(node) {
);
}

function isTestArrowFunction(node) {
return (
node.type === 'ArrowFunctionExpression' &&
node.parent.type === 'CallExpression' &&
TEST_DEFINITION_NAMES.includes(getTestMethodName(node.parent))
);
}

module.exports = {
TEST_FUNCTION_NAMES,
getTestMethodName,
isTestDefinition,
isTestArrowFunction,
};
43 changes: 43 additions & 0 deletions tests/lib/rules/jest/no-if.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,49 @@ ruleTester.run('no-if', rule, {
});
`,
},
{
code: `it('foo', () => {
const foo = bar => {
return foo ? bar : null;
};
});`,
},
{
code: `it('foo', () => {
const foo = function(bar) {
return foo ? bar : null;
};
});`,
},
{
code: `it('foo', () => {
const foo = function(bar) {
if (bar) {
return 1;
} else {
return 2;
}
};
});`,
},
{
code: `it('foo', () => {
function foo(bar) {
return foo ? bar : null;
};
});`,
},
{
code: `it('foo', () => {
function foo(bar) {
if (bar) {
return 1;
} else {
return 2;
}
};
});`,
},
],
invalid: [
{
Expand Down