Skip to content

Commit

Permalink
fix(no-if): check both types of function expression (#672)
Browse files Browse the repository at this point in the history
Fixes #670
  • Loading branch information
G-Rath authored Sep 20, 2020
1 parent b4319e8 commit d462d50
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
49 changes: 40 additions & 9 deletions src/rules/__tests__/no-if.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ const ruleTester = new TSESLint.RuleTester({
ruleTester.run('conditional expressions', rule, {
valid: [
'const x = y ? 1 : 0',
{
code: dedent`
it('foo', () => {
const foo = function(bar) {
return foo ? bar : null;
};
});
`,
},
dedent`
it('foo', () => {
const foo = function (bar) {
return foo ? bar : null;
};
});
`,
dedent`
it('foo', function () {
const foo = function (bar) {
return foo ? bar : null;
};
});
`,
],
invalid: [
{
Expand Down Expand Up @@ -341,6 +346,19 @@ ruleTester.run('switch statements', rule, {
},
],
},
{
code: dedent`
xtest('foo', function () {
switch('bar') {}
})
`,
errors: [
{
data: { condition: 'switch' },
messageId: 'conditionalInTest',
},
],
},
{
code: dedent`
describe('foo', () => {
Expand Down Expand Up @@ -586,6 +604,19 @@ ruleTester.run('if statements', rule, {
},
],
},
{
code: dedent`
it.skip('foo', function () {
if('bar') {}
})
`,
errors: [
{
data: { condition: 'if' },
messageId: 'conditionalInTest',
},
],
},
{
code: dedent`
it.concurrent.skip('foo', () => {
Expand Down
10 changes: 6 additions & 4 deletions src/rules/no-if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const testCaseNames = new Set<string | null>([
'fit.concurrent',
]);

const isTestArrowFunction = (node: TSESTree.ArrowFunctionExpression) =>
const isTestFunctionExpression = (
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
) =>
node.parent !== undefined &&
node.parent.type === AST_NODE_TYPES.CallExpression &&
testCaseNames.has(getNodeName(node.parent.callee));
Expand Down Expand Up @@ -77,8 +79,8 @@ export default createRule({
stack.push(true);
}
},
FunctionExpression() {
stack.push(false);
FunctionExpression(node) {
stack.push(isTestFunctionExpression(node));
},
FunctionDeclaration(node) {
const declaredVariables = context.getDeclaredVariables(node);
Expand All @@ -89,7 +91,7 @@ export default createRule({
stack.push(testCallExpressions.length > 0);
},
ArrowFunctionExpression(node) {
stack.push(isTestArrowFunction(node));
stack.push(isTestFunctionExpression(node));
},
IfStatement: validate,
SwitchStatement: validate,
Expand Down

0 comments on commit d462d50

Please # to comment.