]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: warn when a getter conflicts with the state (#1356)
authorPouya Mohammadkhani <31730646+BlackCrowxyz@users.noreply.github.com>
Fri, 17 Jun 2022 09:13:27 +0000 (13:43 +0430)
committerGitHub <noreply@github.com>
Fri, 17 Jun 2022 09:13:27 +0000 (11:13 +0200)
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
packages/pinia/__tests__/store.spec.ts
packages/pinia/src/store.ts

index cdab3deb733188742f63bad377eead95a41df3dd..d5122d58a7756d434b5597d9434248ab867c886c 100644 (file)
@@ -366,4 +366,16 @@ describe('Store', () => {
     useMyStore()
     expect(warnTextCheckPlainObject('poInit')).toHaveBeenWarnedTimes(0)
   })
+
+  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 },
+    })
+    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)
+  })
 })
index 9987608a21c9d9ce67f5e67397ffadb78bb7e6d3..d7156a0ea6f34d5a039d933b436cb434c78cb9fd 100644 (file)
@@ -149,6 +149,12 @@ function createOptionsStore<
       localState,
       actions,
       Object.keys(getters || {}).reduce((computedGetters, name) => {
+        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(
           computed(() => {
             setActivePinia(pinia)