Skip to content

Commit

Permalink
Ignore React functional components with fragment in cognitive-complex…
Browse files Browse the repository at this point in the history
…ity (#245)

Authored-by: Tibinko <tibor.hanesz@gmail.com>
  • Loading branch information
vilchik-elena committed Jun 30, 2021
1 parent 9dafdfe commit 3d632f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/rules/cognitive-complexity.ts
Expand Up @@ -300,7 +300,11 @@ const rule: Rule.RuleModule<string, Options> = {

function visitReturnStatement({ argument }: TSESTree.ReturnStatement) {
// top level function
if (enclosingFunctions.length === 1 && argument && (argument.type as any) === 'JSXElement') {
if (
enclosingFunctions.length === 1 &&
argument &&
['JSXElement', 'JSXFragment'].includes(argument.type as any)
) {
reactFunctionalComponent.returnsJsx = true;
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/rules/cognitive-complexity.test.ts
Expand Up @@ -491,6 +491,24 @@ ruleTester.run('cognitive-complexity', rule, {
options: [0],
errors: [message(1, { line: 2 }), message(1, { line: 3 })],
},
{
code: `
const Welcome = () => {
const handleSomething = () => {
if (x) {} // +1
}
if (x) {} // +1
return (
<>
<h1>Hello, world</h1>
<p>cat</p>
</>
);
}`,
parserOptions: { ecmaFeatures: { jsx: true } },
options: [0],
errors: [message(1, { line: 2 }), message(1, { line: 3 })],
},
],
});

Expand Down

0 comments on commit 3d632f7

Please sign in to comment.