[英]ESLint disable localStorage and sessionStorage
Is there a way to configure ESLint to throw an error when the code uses localStorage
or sessionStorage
?有没有办法配置 ESLint 在代码使用
localStorage
或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. ESLint 目前没有这方面的规则。
id-blacklist
will only prevent you from creating variables/function with the names specified. id-blacklist
只会阻止您使用指定的名称创建变量/函数。 There are two open proposals for rules that would allow you to do this: no-restricted-globals
and no-restricted-properties
.有两个公开的规则提案可以让您这样做:
no-restricted-globals
和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
.但是您可以轻松创建一个插件来执行此操作,只需复制
no-alert
规则并将名称更改为localStorage
和sessionStorage
。
我创建了一个ESlint 插件,它会在每次使用 sessionStorage 和 localStorage 时发出警报。
You can also use no-restricted-syntax
, if you want to forbid all localStorage.method()
calls.如果您想禁止所有
localStorage.method()
调用,您也可以使用no-restricted-syntax
。
'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',
},
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.