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

Improve S3776: Exclude complexity of JSX short-circuits #377

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 25 additions & 17 deletions src/rules/cognitive-complexity.ts
Expand Up @@ -339,7 +339,9 @@ const rule: TSESLint.RuleModule<string, (number | 'metric' | 'sonar-runtime')[]>
}

function visitLogicalExpression(logicalExpression: TSESTree.LogicalExpression) {
if (isJsxShortCircuitNode(logicalExpression)) {
const jsxShortCircuitNodes = getJsxShortCircuitNodes(logicalExpression);
if (jsxShortCircuitNodes) {
ilia-kebets-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
jsxShortCircuitNodes.forEach(node => consideredLogicalExpressions.add(node));
return;
}

Expand All @@ -357,26 +359,32 @@ const rule: TSESLint.RuleModule<string, (number | 'metric' | 'sonar-runtime')[]>
}
}

function isJsxShortCircuitNode(logicalExpression: TSESTree.LogicalExpression) {
const isShortCircuit =
logicalExpression.parent?.type === 'JSXExpressionContainer' &&
isJsxShortCircuitSubNode(logicalExpression, logicalExpression.left) &&
isJsxShortCircuitSubNode(logicalExpression, logicalExpression.right);
if (isShortCircuit) {
consideredLogicalExpressions.add(logicalExpression);
function getJsxShortCircuitNodes(logicalExpression: TSESTree.LogicalExpression) {
if (logicalExpression.parent?.type !== 'JSXExpressionContainer') {
return null;
} else {
return getJsxShortCircuitSubNodes(logicalExpression, logicalExpression);
}
return isShortCircuit;
}

function isJsxShortCircuitSubNode(root: TSESTree.LogicalExpression, node: TSESTree.Node) {
if (node.type === 'LogicalExpression') {
const isShortCircuit = node.operator === root.operator;
if (isShortCircuit) {
consideredLogicalExpressions.add(node);
}
return isShortCircuit;
function getJsxShortCircuitSubNodes(
root: TSESTree.LogicalExpression,
ilia-kebets-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
node: TSESTree.Node,
): TSESTree.Node[] | null {
if (
node.type === 'ConditionalExpression' ||
(node.type === 'LogicalExpression' && node.operator !== root.operator)
) {
return null;
} else if (node.type !== 'LogicalExpression') {
return [];
} else {
return node.type !== 'ConditionalExpression';
const leftNodes = getJsxShortCircuitSubNodes(root, node.left);
const rightNodes = getJsxShortCircuitSubNodes(root, node.right);
if (leftNodes == null || rightNodes == null) {
return null;
}
return [...leftNodes, node, ...rightNodes];
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/rules/cognitive-complexity.test.ts
Expand Up @@ -60,6 +60,18 @@ ruleTester.run('cognitive-complexity', rule, {
parserOptions: { ecmaFeatures: { jsx: true } },
options: [0],
},
{
code: `
function Component(obj) {
return (
<>
{ obj.x && obj.y && obj.z && <strong>Welcome</strong> }
</>
);
}`,
parserOptions: { ecmaFeatures: { jsx: true } },
options: [0],
},
{
code: `
function Component(obj) {
Expand Down