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.
ESLint doesn't currently have a rule for this. id-blacklist
will only prevent you from creating variables/function with the names specified. There are two open proposals for rules that would allow you to do this: no-restricted-globals
and no-restricted-properties
. But you could easily create a plugin that would do this, just copy no-alert
rule and change the names to localStorage
and sessionStorage
.
我创建了一个ESlint 插件,它会在每次使用 sessionStorage 和 localStorage 时发出警报。
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',
},
],
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.