typescript.global_mutable_state
Correctness
High
Detects module-level mutable state.
Why It Matters
Section titled “Why It Matters”Global mutable state causes subtle bugs:
- Request leakage - In serverless, state persists between invocations
- Race conditions - Concurrent requests modify shared state
- Testing pain - State leaks between tests
- Debugging difficulty - State changes from anywhere
Example
Section titled “Example”// ❌ Beforelet cache: Map<string, User> = new Map();let requestCount = 0;
export function getUser(id: string) { requestCount++; if (!cache.has(id)) { cache.set(id, fetchUser(id)); } return cache.get(id);}// ✅ After// Encapsulate in classclass UserService { private cache = new Map<string, User>();
getUser(id: string) { if (!this.cache.has(id)) { this.cache.set(id, this.fetchUser(id)); } return this.cache.get(id); }}
// Or use dependency injectionexport function createUserService(cache: Cache) { return { getUser(id: string) { return cache.get(id) ?? fetchUser(id); } };}What Unfault Detects
Section titled “What Unfault Detects”letat module level with mutable values- Module-level arrays, objects, Maps, Sets
- Global variables that get reassigned
Auto-Fix
Section titled “Auto-Fix”Unfault suggests encapsulating state or using const.