From 56c6f4571dd0dfda307ed11c5afb8c837ad928b0 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Wed, 24 Jul 2024 11:30:57 +0200 Subject: [PATCH] prefer `localStorage` over `window.localStorage` (#3646) --- .changeset/light-geckos-vanish.md | 5 +++++ .eslintrc.js | 9 ++++++++- packages/graphiql-toolkit/src/storage/base.ts | 12 ++++++------ packages/graphiql-toolkit/src/storage/custom.ts | 6 +++--- .../src/components/__tests__/GraphiQL.spec.tsx | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 .changeset/light-geckos-vanish.md diff --git a/.changeset/light-geckos-vanish.md b/.changeset/light-geckos-vanish.md new file mode 100644 index 00000000000..b32bf0e01a3 --- /dev/null +++ b/.changeset/light-geckos-vanish.md @@ -0,0 +1,5 @@ +--- +"@graphiql/toolkit": patch +--- + +prefer `localStorage` over `window.localStorage` diff --git a/.eslintrc.js b/.eslintrc.js index a619be9b904..521e132c476 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -133,7 +133,14 @@ module.exports = { 'no-octal-escape': 'error', 'no-param-reassign': 'error', 'no-proto': 'error', - 'no-restricted-properties': 'off', + 'no-restricted-properties': [ + 'error', + { + object: 'window', + property: 'localStorage', + message: 'Use `localStorage` instead', + }, + ], 'no-return-assign': 'error', 'no-return-await': 'error', 'no-script-url': 'error', diff --git a/packages/graphiql-toolkit/src/storage/base.ts b/packages/graphiql-toolkit/src/storage/base.ts index 31b3dcddf60..f059b11df6d 100644 --- a/packages/graphiql-toolkit/src/storage/base.ts +++ b/packages/graphiql-toolkit/src/storage/base.ts @@ -64,13 +64,13 @@ export class StorageAPI { this.storage = null; } else { this.storage = { - getItem: window.localStorage.getItem.bind(window.localStorage), - setItem: window.localStorage.setItem.bind(window.localStorage), - removeItem: window.localStorage.removeItem.bind(window.localStorage), + getItem: localStorage.getItem.bind(localStorage), + setItem: localStorage.setItem.bind(localStorage), + removeItem: localStorage.removeItem.bind(localStorage), get length() { let keys = 0; - for (const key in window.localStorage) { + for (const key in localStorage) { if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) { keys += 1; } @@ -80,9 +80,9 @@ export class StorageAPI { clear() { // We only want to clear the namespaced items - for (const key in window.localStorage) { + for (const key in localStorage) { if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) { - window.localStorage.removeItem(key); + localStorage.removeItem(key); } } }, diff --git a/packages/graphiql-toolkit/src/storage/custom.ts b/packages/graphiql-toolkit/src/storage/custom.ts index b321c98c96b..7e82cdd5b62 100644 --- a/packages/graphiql-toolkit/src/storage/custom.ts +++ b/packages/graphiql-toolkit/src/storage/custom.ts @@ -25,7 +25,7 @@ export function createLocalStorage({ removeItem: key => localStorage.removeItem(getStorageKey(key)), get length() { let keys = 0; - for (const key in window.localStorage) { + for (const key in localStorage) { if (key.indexOf(storageKeyPrefix) === 0) { keys += 1; } @@ -35,9 +35,9 @@ export function createLocalStorage({ clear() { // We only want to clear the namespaced items - for (const key in window.localStorage) { + for (const key in localStorage) { if (key.indexOf(storageKeyPrefix) === 0) { - window.localStorage.removeItem(key); + localStorage.removeItem(key); } } }, diff --git a/packages/graphiql/src/components/__tests__/GraphiQL.spec.tsx b/packages/graphiql/src/components/__tests__/GraphiQL.spec.tsx index 8eacd3b6719..e278a1310fe 100644 --- a/packages/graphiql/src/components/__tests__/GraphiQL.spec.tsx +++ b/packages/graphiql/src/components/__tests__/GraphiQL.spec.tsx @@ -29,7 +29,7 @@ const simpleIntrospection = { }; beforeEach(() => { - window.localStorage.clear(); + localStorage.clear(); }); describe('GraphiQL', () => {