Awesome Reviewers

Reviewer · Null Handling

Precise nullable handling

When code deals with optional or possibly-undefined values, be exact about which values can actually be null/undefined and apply idiomatic patterns correctly rather than defensively:

Instruction

When code deals with optional or possibly-undefined values, be exact about which values can actually be null/undefined and apply idiomatic patterns correctly rather than defensively:

  • Prefer a concise ternary or nullish-coalescing expression over verbose if/else branches for simple fallback logic, e.g. declarator.init != null ? declarator.init : t.identifier('undefined') instead of a multi-branch if/else that returns the same shape.
  • Double-check that optional chaining (?.) actually introduces undefined/null into the expression’s type before relying on ?? to supply a fallback. If the left-hand side’s inferred type doesn’t include undefined, TypeScript won’t flag it, and the fallback on the right becomes dead code (e.g. block.instructions[0]?.id ?? block.terminal silently never falls back — it should be block.instructions[0]?.id ?? block.terminal.id).
  • Avoid over-restricting logic to only run when all optional values are present. Handle each nullable input independently — e.g., when deriving a combined location from a required id and an optional init, still use id.loc alone when init (or init.loc) is absent instead of requiring both to be non-null.
  • When analysis/invariants guarantee a value can never be undefined (verified by auditing all producers/call sites), replace silent defensive checks like if (x === undefined) { continue; } with an explicit CompilerError.invariant(x !== undefined, ...) (or equivalent assertion) so violations fail loudly instead of being silently skipped.

Applying these patterns keeps nullable-handling code both correct (no accidentally-dead fallbacks or missed cases) and concise, while making genuine invariants visible rather than hidden behind defensive null checks.