Control statements are nested deeper than the configured maximum
What it finds
Code nested many levels deep has to be read with every enclosing condition and loop in mind. Returning early, or moving the inner part into a function of its own, flattens it.
The depth counts the if, for, while, do, switch and try statements around a statement, in the same function: a nested function starts again at zero, and the top-level code of a file is measured like a function. An else if continues its chain rather than nesting. The statement that first goes past the limit is reported, and the ones inside it are not, so one deep spot gives one finding. The limit is the parameter max (4 by default).
From the rule's source, nesting_depth.rs.
function walk(items: number[][], ok: boolean) { for (const row of items) { if (ok) { while (row.length) { try { if (row.pop()) {tx0 reports: this if is nested inside 4 other control statements if (ok) { return 1; } } } catch { return 2; } } } else if (row.length > 1) { return 3; } } return 0;}function flat(a: number) { if (a === 1) { return 1; } else if (a === 2) { return 2; } else if (a === 3) { return 3; } else if (a === 4) { return 4; } else if (a === 5) { return 5; } return 0;} 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:
[rules]
"tx0/ts:nesting-depth" = "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-depth] reason="..."tx0 explain tx0/ts:nesting-depth shows the same in the terminal. Found a false positive? Report it.