Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(no-if): check both types of function expression #672

Merged
merged 1 commit into from Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 40 additions & 9 deletions src/rules/__tests__/no-if.test.ts
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
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