A function's branches, weighted by how deeply they are nested, add up to more than the configured maximum

tx0/ts:nesting-weighted-complexityrecommendedwarningmaintainability

What it finds

Counting branches treats a flat list of cases like the same number of conditions nested in one another, though the nested ones are much harder to follow. This measure weighs each branch by how deep it sits.

tx0 computes it like this, for each function on its own:

- an if, conditional expression, switch, loop or catch clause adds one, plus one for each of these statements and expressions it is nested in; - an else if and an else add one, without weight, as they continue a chain; - a chain of logical operators adds one for each run of the same operator, so a && b && c adds one and a && b || c adds two; - a break or continue to a label adds one, as it jumps across the structure.

Nested functions are measured on their own. The limit is the parameter max (25 by default).

From the rule's source, nesting_weighted_complexity.rs.

Example from the rule's tests
function deep(a: number[][][]) {tx0 reports: deep has a nesting-weighted complexity of 28, more than 25  for (const x of a) {    for (const y of x) {      for (const z of y) {        if (z.length) {          while (z.pop()) {            if (z.length > 2) {              if (z.length > 3) {                return 1;              }            }          }        }      }    }  }  return 0;}function callbacks(a: number[]) {  return a.map((x) => (x ? 1 : 2));}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:nesting-weighted-complexity" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:nesting-weighted-complexity] reason="..."

tx0 explain tx0/ts:nesting-weighted-complexity shows the same in the terminal. Found a false positive? Report it.