From db96c4556df31619eda87e4b5f3e8b737429707a Mon Sep 17 00:00:00 2001 From: Denis Karabaza Date: Fri, 3 Aug 2018 21:51:52 +0200 Subject: [PATCH 1/2] feat: warn about conflicts between state and module --- src/store.js | 7 +++++++ test/unit/modules.spec.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/store.js b/src/store.js index b5b083c92..5cb2ff5b3 100644 --- a/src/store.js +++ b/src/store.js @@ -282,6 +282,13 @@ function installModule (store, rootState, path, module, hot) { const parentState = getNestedState(rootState, path.slice(0, -1)) const moduleName = path[path.length - 1] store._withCommit(() => { + if (process.env.NODE_ENV !== 'production') { + if (moduleName in parentState) { + console.warn( + `[vuex] state field "${moduleName}" was overridden by a module with the same name` + ) + } + } Vue.set(parentState, moduleName, module.state) }) } diff --git a/test/unit/modules.spec.js b/test/unit/modules.spec.js index bc8fa2247..8ba9d24fc 100644 --- a/test/unit/modules.spec.js +++ b/test/unit/modules.spec.js @@ -547,6 +547,24 @@ describe('Modules', () => { store.dispatch('parent/test') }) + it('module: warn when module overrides state', () => { + spyOn(console, 'warn') + const store = new Vuex.Store({ + state () { + return { value: 1 } + }, + modules: { + value: { + state: () => 2 + } + } + }) + expect(store.state.value).toBe(2) + expect(console.warn).toHaveBeenCalledWith( + `[vuex] state field "value" was overridden by a module with the same name` + ) + }) + it('dispatching multiple actions in different modules', done => { const store = new Vuex.Store({ modules: { From 4e252745a41e2a92fd94399b59891c8aeab40d25 Mon Sep 17 00:00:00 2001 From: ktsn Date: Sun, 10 Nov 2019 22:14:43 +0800 Subject: [PATCH 2/2] fix: show module path when it overwrite a state field --- src/store.js | 2 +- test/unit/modules.spec.js | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/store.js b/src/store.js index 5cb2ff5b3..b30e0d31a 100644 --- a/src/store.js +++ b/src/store.js @@ -285,7 +285,7 @@ function installModule (store, rootState, path, module, hot) { if (process.env.NODE_ENV !== 'production') { if (moduleName in parentState) { console.warn( - `[vuex] state field "${moduleName}" was overridden by a module with the same name` + `[vuex] state field "${moduleName}" was overridden by a module with the same name at "${path.join('.')}"` ) } } diff --git a/test/unit/modules.spec.js b/test/unit/modules.spec.js index 8ba9d24fc..bc34ae1e6 100644 --- a/test/unit/modules.spec.js +++ b/test/unit/modules.spec.js @@ -550,18 +550,22 @@ describe('Modules', () => { it('module: warn when module overrides state', () => { spyOn(console, 'warn') const store = new Vuex.Store({ - state () { - return { value: 1 } - }, modules: { - value: { - state: () => 2 + foo: { + state () { + return { value: 1 } + }, + modules: { + value: { + state: () => 2 + } + } } } }) - expect(store.state.value).toBe(2) + expect(store.state.foo.value).toBe(2) expect(console.warn).toHaveBeenCalledWith( - `[vuex] state field "value" was overridden by a module with the same name` + `[vuex] state field "value" was overridden by a module with the same name at "foo.value"` ) })