Flow complexity (ARCH3206)
Ce contenu n’est pas encore disponible dans votre langue.
Key typescript.quality.flow-complexity · Severity warning · Category quality
Flags a function whose branches and loops are nested so deeply that a reader has to keep several conditions in mind at once. Each if, loop, catch, switch and ternary costs one plus one per level it is nested at; else and else-if cost one; a run of the same &&, || or ?? costs one; a function calling itself costs one; anonymous callbacks deepen what they hold. A named inner function (a component’s handler, a hook’s callback) is scored on its own, and a condition inside JSX markup costs one, flat. Reported above 15.
Incorrect
Section titled “Incorrect”export function ship(orders: Order[], retry: boolean) { for (const order of orders) { if (order.paid) { for (const line of order.lines) { if (line.stock > 0 && line.active) { try { send(line); } catch { if (retry) queue(line); else fail(line); } } } } }}Correct
Section titled “Correct”export function ship(orders: Order[], retry: boolean) { for (const order of orders.filter(o => o.paid)) shipLines(order, retry);}
function shipLines(order: Order, retry: boolean) { for (const line of order.lines.filter(isShippable)) trySend(line, retry);}