Code Standards¶
This document defines mandatory code standards for Rezi contributors and AI agents. Treat this as a normative checklist for code review and PR readiness.
See also: - Style Guide - Contributing
Enforcement Levels¶
MUST: required for merge.SHOULD: expected unless there is a clear, documented reason not to.
1) Baseline Checks (MUST)¶
For any code change:
For runtime/layout/renderer/reconcile changes, also run:
For drawlist protocol or command layout changes, also run:
2) TypeScript Standards¶
Type safety¶
MUSTpreserve strict typing (strict,noImplicitAny,useUnknownInCatchVariables).MUST NOTintroduceanyunless there is no safe alternative.MUSTnarrowunknownbefore property access.MUSTpreferimport type { ... }for type-only imports.
Public API and domain modeling¶
MUSTmodel variants with discriminated unions (tagged unions), not class hierarchies.MUSTkeep exported API shapes explicit and stable.SHOULDuse explicit return types on exported functions.
Nullability and indexing¶
MUSThandleundefinedfrom array/index lookups explicitly (noUncheckedIndexedAccess).MUST NOTuse non-null assertion (!) unless the invariant is proven in the same scope.
Immutability and determinism¶
SHOULDprefer readonly shapes (Readonly, readonly arrays) for shared data.MUSTavoid hidden mutation of shared objects in render/layout paths.
3) Rezi-Specific Architecture Standards¶
Runtime boundaries¶
MUST NOTimport runtime-specific APIs into@rezi-ui/core.MUSTkeep module boundaries intact:core-> no imports fromnode/jsx/nativenode/jsx-> may import fromcore
Widget and reconciliation rules¶
MUSTuse stableidfor interactive widgets.MUSTuse stablekeyvalues for list reconciliation.MUSTkeep hook invocation order stable (no conditional hooks).MUSTpreserve deterministic behavior for the same input state.
API layering¶
SHOULDpreferui.*factories over raw vnode construction.MUSTkeep JSX parity when changing core widget APIs (ui.ts, JSX components/types/tests/docs together).
Generated code and protocol¶
MUST NOThand-edit generated drawlist writers.MUSTupdate source spec + regenerate writers + update protocol docs when command bytes/layout changes.
4) Error Handling Standards (Critical)¶
Choose the right failure contract¶
MUSTuse typed result unions for parse/validation-style flows where callers should recover.MUSTthrow for unrecoverable programmer/configuration errors.MUSTkeep error contracts consistent within each module.
Catch blocks and thrown values¶
MUSTtreat caught values asunknown.MUSTuse safe thrown-value formatting in logs/warnings.MUST NOTcallString(error)directly in safety-critical catch blocks without a nested guard.
Recommended pattern:
function describeThrown(value: unknown): string {
if (value instanceof Error) return `${value.name}: ${value.message}`;
try {
return String(value);
} catch {
return "[unstringifiable thrown value]";
}
}
Callback boundaries¶
MUSTisolate user callback failures so they do not destabilize routing/render loops.MUSTkeep callback wrappers deterministic and side-effect safe.SHOULDemit dev warnings (warnDev) instead of throwing for user callback exceptions in routing/event handlers.
Async cancellation and stale results¶
MUSTguard async completion paths against cancellation/stale-token updates.MUSTensure canceled validations/effects cannot mutate state after cleanup/unmount.
Error wrapping¶
SHOULDpreserve original errors viacausewhen wrapping.MUSTkeep wrapped messages actionable and specific.
5) Performance and Hot-Path Standards¶
MUSTavoid unnecessary allocations in per-frame render/layout paths.SHOULDuse simple loops in hot paths instead of allocation-heavy array pipelines.MUSTpreserve existing deterministic ordering and stable signatures where applicable.
6) Documentation and Parity Standards¶
MUSTupdate docs in the same PR when changing public behavior.MUSTupdate examples/templates when recommended patterns change.MUSTkeepCLAUDE.mdandAGENTS.mdaligned with these standards.
7) PR Checklist¶
Before merging, verify:
- [ ]
lintandtypecheckare green. - [ ] Required tests for touched areas are green.
- [ ] Error paths and cancellation/stale guards are covered where relevant.
- [ ] Module boundaries and JSX parity are preserved.
- [ ] Public API/documentation updates are included.