From af09ff6c03e8bc62c0075eff7ddccef82b07f025 Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani Date: Fri, 10 Jun 2022 12:44:06 +0430 Subject: [PATCH 01/10] Warn when Getter with same name as State --- packages/pinia/src/store.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 9987608a21..35a0406537 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -149,6 +149,17 @@ function createOptionsStore< localState, actions, Object.keys(getters || {}).reduce((computedGetters, name) => { + if (__DEV__) { + Object.keys(pinia.state.value[id] || {}).forEach((stateName) => { + if (name === stateName) { + console.error( + `[🍍]: "state" property name cannot be the same as "getters" property name.\n` + + `\tGetter "${name}" is the same as state "${stateName}" in "${id}" store.` + ) + } + }) + } + computedGetters[name] = markRaw( computed(() => { setActivePinia(pinia) From 25b6b08b888196d23c571accf052989a36f29797 Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani <31730646+BlackCrowxyz@users.noreply.github.com> Date: Thu, 16 Jun 2022 18:47:36 +0430 Subject: [PATCH 02/10] Update packages/pinia/src/store.ts Co-authored-by: Eduardo San Martin Morote --- packages/pinia/src/store.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 35a0406537..0b96f3a3ae 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -152,7 +152,7 @@ function createOptionsStore< if (__DEV__) { Object.keys(pinia.state.value[id] || {}).forEach((stateName) => { if (name === stateName) { - console.error( + console.warn( `[🍍]: "state" property name cannot be the same as "getters" property name.\n` + `\tGetter "${name}" is the same as state "${stateName}" in "${id}" store.` ) From 7082aaa10773f7d93e1760ca8be406eea7901063 Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani <31730646+BlackCrowxyz@users.noreply.github.com> Date: Thu, 16 Jun 2022 18:48:28 +0430 Subject: [PATCH 03/10] Update packages/pinia/src/store.ts Co-authored-by: Eduardo San Martin Morote --- packages/pinia/src/store.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 0b96f3a3ae..373ac923ed 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -153,8 +153,7 @@ function createOptionsStore< Object.keys(pinia.state.value[id] || {}).forEach((stateName) => { if (name === stateName) { console.warn( - `[🍍]: "state" property name cannot be the same as "getters" property name.\n` + - `\tGetter "${name}" is the same as state "${stateName}" in "${id}" store.` + `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".` ) } }) From 97ded711767b60ff074758373c2741adc68ff6fe Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani Date: Thu, 16 Jun 2022 20:00:21 +0430 Subject: [PATCH 04/10] resolve: use localState instead of pinia.state.value[id] --- packages/pinia/src/store.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 373ac923ed..dd006f659c 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -150,7 +150,7 @@ function createOptionsStore< actions, Object.keys(getters || {}).reduce((computedGetters, name) => { if (__DEV__) { - Object.keys(pinia.state.value[id] || {}).forEach((stateName) => { + Object.keys(localState || {}).forEach((stateName) => { if (name === stateName) { console.warn( `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".` From 500dafe08acf26294be97d72d8369590838e7d69 Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani Date: Thu, 16 Jun 2022 20:19:03 +0430 Subject: [PATCH 05/10] test: add test for getter and state property conflict --- .vscode/settings.json | 3 ++- packages/pinia/__tests__/store.spec.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7b3270124b..dfc1c3317a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,8 @@ "editor.formatOnSave": true }, "[typescript]": { - "editor.formatOnSave": true + "editor.formatOnSave": true, + "editor.defaultFormatter": "esbenp.prettier-vscode" }, "typescript.tsdk": "node_modules/typescript/lib", "jest.jestCommandLine": "yarn jest --watchAll", diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index cdab3deb73..acaf7aca17 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -366,4 +366,17 @@ describe('Store', () => { useMyStore() expect(warnTextCheckPlainObject('poInit')).toHaveBeenWarnedTimes(0) }) + + it('only warns when state name conflicts with getters name', () => { + const useStore = defineStore({ + id: 'main', + state: () => ({ anyName: 0 }), + getters: { anyName: (state) => state.anyName }, + }) + useStore() + + expect( + `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "anyName" in store "main".` + ).toHaveBeenWarnedTimes(1) + }) }) From 810d42f2e81361208677c2a02392f935710b5387 Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani <31730646+BlackCrowxyz@users.noreply.github.com> Date: Thu, 16 Jun 2022 20:20:39 +0430 Subject: [PATCH 06/10] Delete settings.json --- .vscode/settings.json | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index dfc1c3317a..0000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "[javascript]": { - "editor.formatOnSave": true - }, - "[typescript]": { - "editor.formatOnSave": true, - "editor.defaultFormatter": "esbenp.prettier-vscode" - }, - "typescript.tsdk": "node_modules/typescript/lib", - "jest.jestCommandLine": "yarn jest --watchAll", - "editor.defaultFormatter": "esbenp.prettier-vscode" -} From 87e7f67ee5e9f04ad07ac5ecaaf7c5dca72c8f8c Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani Date: Thu, 16 Jun 2022 20:27:15 +0430 Subject: [PATCH 07/10] test: add test for getter and state property conflict --- .vscode/settings.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index dfc1c3317a..7b3270124b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,8 +3,7 @@ "editor.formatOnSave": true }, "[typescript]": { - "editor.formatOnSave": true, - "editor.defaultFormatter": "esbenp.prettier-vscode" + "editor.formatOnSave": true }, "typescript.tsdk": "node_modules/typescript/lib", "jest.jestCommandLine": "yarn jest --watchAll", From 3327cc5f3ed3cf498502dca4a5664dcac8013ecb Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani Date: Fri, 17 Jun 2022 12:36:37 +0430 Subject: [PATCH 08/10] resolve requested changes --- packages/pinia/__tests__/store.spec.ts | 5 ++--- packages/pinia/src/store.ts | 12 ++++-------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/pinia/__tests__/store.spec.ts b/packages/pinia/__tests__/store.spec.ts index acaf7aca17..d5122d58a7 100644 --- a/packages/pinia/__tests__/store.spec.ts +++ b/packages/pinia/__tests__/store.spec.ts @@ -367,9 +367,8 @@ describe('Store', () => { expect(warnTextCheckPlainObject('poInit')).toHaveBeenWarnedTimes(0) }) - it('only warns when state name conflicts with getters name', () => { - const useStore = defineStore({ - id: 'main', + it('warns when state name conflicts with getters name (with id as first argument)', () => { + const useStore = defineStore('main', { state: () => ({ anyName: 0 }), getters: { anyName: (state) => state.anyName }, }) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index dd006f659c..1576f0b85b 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -149,14 +149,10 @@ function createOptionsStore< localState, actions, Object.keys(getters || {}).reduce((computedGetters, name) => { - if (__DEV__) { - Object.keys(localState || {}).forEach((stateName) => { - if (name === stateName) { - console.warn( - `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".` - ) - } - }) + if (__DEV__ && name in (localState || {})) { + console.warn( + `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".` + ) } computedGetters[name] = markRaw( From b3e475a4c09b8d68439e9c76056d826540f17f21 Mon Sep 17 00:00:00 2001 From: Pouya Mohammadkhani <31730646+BlackCrowxyz@users.noreply.github.com> Date: Fri, 17 Jun 2022 13:37:28 +0430 Subject: [PATCH 09/10] Update packages/pinia/src/store.ts Co-authored-by: Eduardo San Martin Morote --- packages/pinia/src/store.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pinia/src/store.ts b/packages/pinia/src/store.ts index 1576f0b85b..d7156a0ea6 100644 --- a/packages/pinia/src/store.ts +++ b/packages/pinia/src/store.ts @@ -149,7 +149,7 @@ function createOptionsStore< localState, actions, Object.keys(getters || {}).reduce((computedGetters, name) => { - if (__DEV__ && name in (localState || {})) { + if (__DEV__ && name in localState) { console.warn( `[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".` ) From 0c5756fe3e507874a696e45b70442846592c17ad Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 16 Oct 2023 11:48:31 +0200 Subject: [PATCH 10/10] feat(nuxt): add storeToRefs --- packages/nuxt/src/module.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index e02f61177c..9fed9d39a6 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -90,6 +90,7 @@ const module: NuxtModule = defineNuxtModule({ { from: composables, name: 'defineStore' }, { from: composables, name: 'acceptHMRUpdate' }, { from: composables, name: 'usePinia' }, + { from: composables, name: 'storeToRefs' }, ]) if (options.storesDirs) {