Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint disable localStorage and sessionStorage

Is there a way to configure ESLint to throw an error when the code uses localStorage or sessionStorage?

This is needed as I use a third party library for the storage, and I want all the storage to be done through it.

I already tried searching for a plugin, with no results.

like image 955
Tzach Avatar asked Sep 16 '25 12:09

Tzach


1 Answers

You can also use no-restricted-syntax, if you want to forbid all localStorage.method() calls.

'no-restricted-syntax': [
  'error',
  {
    selector: "CallExpression[callee.object.name='localStorage']",
    message: 'Do not use `localStorage` directly, use the storage wrapper instead',
  },
  {
    selector: "CallExpression[callee.object.name='sessionStorage']",
    message: 'Do not use `sessionStorage` directly, use the storage wrapper instead',
  },
],
like image 85
harunurhan Avatar answered Sep 18 '25 10:09

harunurhan