Stanza

guard-chain

The blank lines inside a run of compact guards.

Consecutive compact guards (if with a single statement body and no else) have no blank line between them.

function accepts(entry: Entry, owner: string) {
  if (entry.owner !== owner) return false;

  if (entry.archived) return false;

  if (entry.size > limit) {
    log(entry);
    return false;
  }

  return true;
}

A guard is an if without else whose body is a single statement, braceless or in a block of exactly one statement. When two guards follow each other and both are compact, the blank line between them goes. Compact means on one line, or braceless with a joined line that fits the formatter width. The body does not have to leave: if (dirty) count = 1; chains like if (!item) return;.

Only short-body outranks it. It outranks after-multiline, which matters for a braceless guard written on two lines that would fit on one, and it outranks after-guard.

An if with an else is not a guard and breaks the chain. So does an if whose block holds two statements, or one whose header spans lines. The third if in the first example has a two statement block, so the blank line before it stays.

A guard too long for one line is not compact. The formatter leaves it on two lines, so after-multiline decides the gap below it and keeps the blank line.

function selectShipment(shipment: Shipment, registry: Registry) {
  if (!registry.hasEligibleCarrier(shipment.destinationCountry, shipment.deliveryWindow))
    return shipment.withStatus("no eligible carrier for the requested delivery window");

  if (!registry.hasAvailableRoute(shipment.destinationCountry, shipment.deliveryWindow))
    return shipment.withStatus("no available route for the requested delivery window");

  return registry.assign(shipment);
}