no-storage-mutation
Disallow in-place mutation of localStorage and sessionStorage.
Targeted pattern scopeâ
This rule targets browser storage singletons (localStorage and sessionStorage) and aliases derived from them.
What this rule reportsâ
localStorage.setItem(...)localStorage.removeItem(...)localStorage.clear(...)sessionStorage.setItem(...),removeItem(...), andclear(...)- Property writes/deletes/updates on storage objects and tracked aliases
Why this rule existsâ
Web storage is shared mutable global state. In-place writes from multiple modules create implicit coupling and make application state transitions difficult to reason about and test.
This rule promotes immutable architecture by preventing direct mutation of storage primitives and encouraging explicit state derivation flows.
â Incorrectâ
localStorage.setItem("theme", "dark");
â Correctâ
const theme = localStorage.getItem("theme") ?? "light";
Additional examplesâ
// â Mutates shared storage through alias
const storage = sessionStorage;
storage.clear();
// â
Reads storage without mutating global state
const storage = sessionStorage;
storage.getItem("session-id");
ESLint flat config exampleâ
import immutable from "eslint-plugin-immutable-2";
export default [
{
files: ["**/*.{js,mjs,cjs,ts,mts,cts,tsx}"],
plugins: { immutable },
rules: {
"immutable/no-storage-mutation": "error",
},
},
];
When not to use itâ
If your application intentionally writes to browser storage in a tightly controlled adapter layer, this rule may be too strict outside that boundary.
Rule catalog ID: R927