Skip to content

Commit d462d50

Browse files
authored
fix(no-if): check both types of function expression (#672)
Fixes #670
1 parent b4319e8 commit d462d50

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/rules/__tests__/no-if.test.ts

+40-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ const ruleTester = new TSESLint.RuleTester({
1313
ruleTester.run('conditional expressions', rule, {
1414
valid: [
1515
'const x = y ? 1 : 0',
16-
{
17-
code: dedent`
18-
it('foo', () => {
19-
const foo = function(bar) {
20-
return foo ? bar : null;
21-
};
22-
});
23-
`,
24-
},
16+
dedent`
17+
it('foo', () => {
18+
const foo = function (bar) {
19+
return foo ? bar : null;
20+
};
21+
});
22+
`,
23+
dedent`
24+
it('foo', function () {
25+
const foo = function (bar) {
26+
return foo ? bar : null;
27+
};
28+
});
29+
`,
2530
],
2631
invalid: [
2732
{
@@ -341,6 +346,19 @@ ruleTester.run('switch statements', rule, {
341346
},
342347
],
343348
},
349+
{
350+
code: dedent`
351+
xtest('foo', function () {
352+
switch('bar') {}
353+
})
354+
`,
355+
errors: [
356+
{
357+
data: { condition: 'switch' },
358+
messageId: 'conditionalInTest',
359+
},
360+
],
361+
},
344362
{
345363
code: dedent`
346364
describe('foo', () => {
@@ -586,6 +604,19 @@ ruleTester.run('if statements', rule, {
586604
},
587605
],
588606
},
607+
{
608+
code: dedent`
609+
it.skip('foo', function () {
610+
if('bar') {}
611+
})
612+
`,
613+
errors: [
614+
{
615+
data: { condition: 'if' },
616+
messageId: 'conditionalInTest',
617+
},
618+
],
619+
},
589620
{
590621
code: dedent`
591622
it.concurrent.skip('foo', () => {

src/rules/no-if.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const testCaseNames = new Set<string | null>([
2323
'fit.concurrent',
2424
]);
2525

26-
const isTestArrowFunction = (node: TSESTree.ArrowFunctionExpression) =>
26+
const isTestFunctionExpression = (
27+
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
28+
) =>
2729
node.parent !== undefined &&
2830
node.parent.type === AST_NODE_TYPES.CallExpression &&
2931
testCaseNames.has(getNodeName(node.parent.callee));
@@ -77,8 +79,8 @@ export default createRule({
7779
stack.push(true);
7880
}
7981
},
80-
FunctionExpression() {
81-
stack.push(false);
82+
FunctionExpression(node) {
83+
stack.push(isTestFunctionExpression(node));
8284
},
8385
FunctionDeclaration(node) {
8486
const declaredVariables = context.getDeclaredVariables(node);
@@ -89,7 +91,7 @@ export default createRule({
8991
stack.push(testCallExpressions.length > 0);
9092
},
9193
ArrowFunctionExpression(node) {
92-
stack.push(isTestArrowFunction(node));
94+
stack.push(isTestFunctionExpression(node));
9395
},
9496
IfStatement: validate,
9597
SwitchStatement: validate,

0 commit comments

Comments
 (0)