Skip to content

Commit a614338

Browse files
committed
feat(proxy-state-tree): don't allow nesting proxies in state tree
1 parent b6e9ac0 commit a614338

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

packages/node_modules/proxy-state-tree/src/index.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ describe('OBJECTS', () => {
6666
tree.get().foo = 'bar2'
6767
}).toThrow()
6868
})
69+
test('should throw if parts of state are nested', () => {
70+
const state = {
71+
foo: {
72+
nested: 'bar',
73+
},
74+
}
75+
const tree = new ProxyStateTree(state)
76+
expect(() => {
77+
tree.startMutationTracking()
78+
tree.get().foo = tree.get().foo
79+
tree.clearMutationTracking()
80+
}).toThrowError(/You tried to nest parts of the state/i)
81+
})
6982
test('should track SET mutations', () => {
7083
const state = {
7184
foo: 'bar',
@@ -228,6 +241,28 @@ describe('ARRAYS', () => {
228241
tree.get().foo.push('foo')
229242
}).toThrow()
230243
})
244+
test('should throw if parts of state are nested', () => {
245+
const state = {
246+
foo: [undefined],
247+
bar: {
248+
nested: 'baz',
249+
},
250+
}
251+
const tree = new ProxyStateTree(state)
252+
tree.startMutationTracking()
253+
254+
// Property mutation
255+
expect(() => {
256+
tree.get().foo[0] = tree.get().bar
257+
}).toThrowError(/You tried to nest parts of the state/i)
258+
259+
// PUSH mutation
260+
expect(() => {
261+
tree.get().foo.push(tree.get().bar)
262+
}).toThrowError(/You tried to nest parts of the state/i)
263+
264+
tree.clearMutationTracking()
265+
})
231266
test('should track PUSH mutations', () => {
232267
const state = {
233268
foo: [],

packages/node_modules/proxy-state-tree/src/proxify.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ function createArrayProxy(tree, value, path) {
6060
)
6161
}
6262
return (...args) => {
63+
if (args.some((value) => value[IS_PROXY] === true)) {
64+
throw new Error(
65+
`proxy-state-tree - You tried to nest parts of the state, but it is not allowed`
66+
)
67+
}
68+
6369
tree.currentMutations.push({
6470
method: prop,
6571
path: path,
@@ -84,7 +90,11 @@ function createArrayProxy(tree, value, path) {
8490
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
8591
)
8692
}
87-
93+
if (value[IS_PROXY] === true) {
94+
throw new Error(
95+
`proxy-state-tree - You tried to nest parts of the state, but it is not allowed`
96+
)
97+
}
8898
tree.currentMutations.push({
8999
method: 'set',
90100
path: nestedPath,
@@ -131,6 +141,11 @@ function createObjectProxy(tree, value, path) {
131141
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
132142
)
133143
}
144+
if (value[IS_PROXY] === true) {
145+
throw new Error(
146+
`proxy-state-tree - You tried to nest parts of the state, but it is not allowed`
147+
)
148+
}
134149
if (shouldTrackMutations(tree, nestedPath)) {
135150
if (!(prop in target)) {
136151
tree.objectChanges.add(path)

0 commit comments

Comments
 (0)