# javascript:S3776: an arrow function in a ternary branch adds +1 nesting to the rest of the function

**URL:** <https://community.sonarsource.com/t/javascript-an-arrow-function-in-a-ternary-branch-adds-1-nesting-to-the-rest-of-the-function/191047>\
**Category:** Report False-positive / False-negative...\
**Tags:** javascript\
**Created:** [November 7, 2026, 4:27pm UTC](https://community.sonarsource.com/t/javascript-an-arrow-function-in-a-ternary-branch-adds-1-nesting-to-the-rest-of-the-function/191047 "2026-11-07T16:27:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mati-caliz](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mati-caliz/32/78259_2.png) [@mati-caliz](https://community.sonarsource.com/u/mati-caliz)\
**Post date:** [November 7, 2026, 4:27pm UTC](https://community.sonarsource.com/t/javascript-an-arrow-function-in-a-ternary-branch-adds-1-nesting-to-the-rest-of-the-function/191047/1 "2026-11-07T16:27:49Z")

</div>

**Product / version:** eslint-plugin-sonarjs 4.2.2, ESLint 10.14.0, Node (flat config)

**Rule:** `sonarjs/cognitive-complexity` (S3776)

**What happens:** when an arrow function is a branch of a conditional expression, every structure after it in the same function gets one extra level of nesting, as if the nesting from the arrow function was never decremented.

**Reproducer** (threshold set to 0 to show the score):

```js
// Reported complexity: 3 (expected 2)
function withArrow(flag, value) {
  const handler = flag ? () => 1 : null;
  if (value) {
    return handler;
  }
  return null;
}

// Reported complexity: 2
function withoutArrow(flag, value) {
  const handler = flag ? 1 : null;
  if (value) {
    return handler;
  }
  return null;
}

// Reported complexity: 2 (same arrow, declared outside the ternary)
function arrowOutside(flag, value) {
  const arrow = () => 1;
  const handler = flag ? arrow : null;
  if (value) {
    return handler;
  }
  return null;
}

```

**Expected:** 2 for all three: +1 for the `?:` and +1 for the `if` (nesting 0). In the first function the `if` is counted as +2, i.e. nested.

```js
// eslint.config.mjs
import sonarjs from "eslint-plugin-sonarjs";
export default [{ files: ["*.js"], plugins: { sonarjs },
  rules: { "sonarjs/cognitive-complexity": ["error", 0] } }];

```

I found this while comparing the rule’s output against my own implementation of the metric on several open source repos.

---

<div class="post-metadata">

**Author:** ![victor.diez](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/victor.diez/32/38525_2.png) [@victor.diez](https://community.sonarsource.com/u/victor.diez)\
**Post date:** [November 9, 2026, 1:09pm UTC](https://community.sonarsource.com/t/javascript-an-arrow-function-in-a-ternary-branch-adds-1-nesting-to-the-rest-of-the-function/191047/3 "2026-11-09T13:09:26Z")

</div>

Hello @mati-caliz,

thank you for reporting this. Indeed you are right this is a bug in the rule. I created a [ticket](https://sonarsource.atlassian.net/browse/JS-2847) to fix it.

Cheers,  
Victor
