Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 3d632f7

Browse files
Ignore React functional components with fragment in cognitive-complexity (#245)
Authored-by: Tibinko <[email protected]>
1 parent 9dafdfe commit 3d632f7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/rules/cognitive-complexity.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,11 @@ const rule: Rule.RuleModule<string, Options> = {
300300

301301
function visitReturnStatement({ argument }: TSESTree.ReturnStatement) {
302302
// top level function
303-
if (enclosingFunctions.length === 1 && argument && (argument.type as any) === 'JSXElement') {
303+
if (
304+
enclosingFunctions.length === 1 &&
305+
argument &&
306+
['JSXElement', 'JSXFragment'].includes(argument.type as any)
307+
) {
304308
reactFunctionalComponent.returnsJsx = true;
305309
}
306310
}

tests/rules/cognitive-complexity.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,24 @@ ruleTester.run('cognitive-complexity', rule, {
491491
options: [0],
492492
errors: [message(1, { line: 2 }), message(1, { line: 3 })],
493493
},
494+
{
495+
code: `
496+
const Welcome = () => {
497+
const handleSomething = () => {
498+
if (x) {} // +1
499+
}
500+
if (x) {} // +1
501+
return (
502+
<>
503+
<h1>Hello, world</h1>
504+
<p>cat</p>
505+
</>
506+
);
507+
}`,
508+
parserOptions: { ecmaFeatures: { jsx: true } },
509+
options: [0],
510+
errors: [message(1, { line: 2 }), message(1, { line: 3 })],
511+
},
494512
],
495513
});
496514

0 commit comments

Comments
 (0)