[英]ESLint disable localStorage and sessionStorage
有沒有辦法配置 ESLint 在代碼使用localStorage
或sessionStorage
時拋出錯誤?
這是需要的,因為我使用第三方庫進行存儲,並且我希望所有存儲都通過它完成。
我已經嘗試搜索插件,但沒有結果。
ESLint 目前沒有這方面的規則。 id-blacklist
只會阻止您使用指定的名稱創建變量/函數。 有兩個公開的規則提案可以讓您這樣做: no-restricted-globals
和no-restricted-properties
。 但是您可以輕松創建一個插件來執行此操作,只需復制no-alert
規則並將名稱更改為localStorage
和sessionStorage
。
我創建了一個ESlint 插件,它會在每次使用 sessionStorage 和 localStorage 時發出警報。
如果您想禁止所有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.