Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,34 @@ export function apply<
for (let index = 0; index < path.length - 1; index += 1) {
const parentType = getType(base);
let key = path[index];
if (typeof key !== 'string' && typeof key !== 'number') {
key = String(key);
}
const keyForCheck =
typeof key === 'symbol' ? undefined : String(key as any);
if (
((parentType === DraftType.Object ||
parentType === DraftType.Array) &&
(key === '__proto__' || key === 'constructor')) ||
(typeof base === 'function' && key === 'prototype')
keyForCheck !== undefined &&
(keyForCheck === '__proto__' || keyForCheck === 'constructor')) ||
(typeof base === 'function' &&
keyForCheck !== undefined &&
keyForCheck === 'prototype')
) {
throw new Error(
`Patching reserved attributes like __proto__ and constructor is not allowed.`
);
}
if (
(parentType === DraftType.Object ||
parentType === DraftType.Array ||
typeof base === 'function') &&
typeof key !== 'string' &&
typeof key !== 'number' &&
typeof key !== 'symbol'
) {
// keyForCheck cannot be undefined here, because:
// - If key is a symbol, this conditional block will not be entered
// - All other types will be converted to String(key)
key = keyForCheck!;
}
// use `index` in Set draft
base = get(parentType === DraftType.Set ? Array.from(base) : base, key);
if (typeof base !== 'object') {
Expand Down
66 changes: 66 additions & 0 deletions test/immer-non-support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,3 +735,69 @@ test('Unexpected undefined not assigned', () => {
expect(Object.prototype.hasOwnProperty.call(foo, 'name')).toBe(true);
}
});

test('apply - map with object key', () => {
{
enablePatches();
enableMapSet();
const key = { id: 1 };
const base = {
map: new Map([[key, { value: 1 }]]),
};
const [next, patches, inverse] = produceWithPatches(base, (draft) => {
draft.map.get(key)!.value = 2;
});
expect(() => applyPatches(base, patches)).toThrow();
expect(() => applyPatches(next, inverse)).toThrow();
}
{
const key = { id: 1 };
const base = {
map: new Map([[key, { value: 1 }]]),
};
const [next, patches, inverse] = create(
base,
(draft) => {
draft.map.get(key)!.value = 2;
},
{ enablePatches: true }
);
expect(apply(base, patches)).toEqual(next);
expect(apply(next, inverse)).toEqual(base);
}
});

test('apply - symbol key on object', () => {
{
enablePatches();
const sym = Symbol('key');
const base = {
obj: {
[sym]: { value: 1 },
},
};
const [next, patches, inverse] = produceWithPatches(base, (draft) => {
draft.obj[sym].value = 2;
});
expect(() => applyPatches(base, patches)).toThrow();
expect(() => applyPatches(next, inverse)).toThrow();
}
{
const sym = Symbol('key');
const base = {
obj: {
[sym]: { value: 1 },
},
};
const [next, patches, inverse] = create(
base,
(draft) => {
draft.obj[sym].value = 2;
},
{ enablePatches: true }
);
expect(apply(base, patches)).toEqual(next);
expect(apply(next, inverse)).toEqual(base);
}
});

52 changes: 52 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4086,6 +4086,58 @@ test('#59 - Failure to apply inverse patchset(Map)', () => {
expect(reverted2).toEqual(newState);
});

test('apply - map with object key', () => {
const key = { id: 1 };
const base = {
map: new Map([[key, { value: 1 }]]),
};
const [next, patches, inverse] = create(
base,
(draft) => {
draft.map.get(key)!.value = 2;
},
{ enablePatches: true }
);
expect(apply(base, patches)).toEqual(next);
expect(apply(next, inverse)).toEqual(base);
});

test('apply - symbol key on object', () => {
const sym = Symbol('key');
const base = {
obj: {
[sym]: { value: 1 },
},
};
const [next, patches, inverse] = create(
base,
(draft) => {
draft.obj[sym].value = 2;
},
{ enablePatches: true }
);
expect(apply(base, patches)).toEqual(next);
expect(apply(next, inverse)).toEqual(base);
});

test('apply - coerces non-primitive intermediate key for object', () => {
const objectKey: any = { toString: () => 'customKey' };
const base = {
foo: {
customKey: { value: 1 },
},
};
const patches: any = [
{ op: 'replace', path: ['foo', objectKey, 'value'], value: 2 },
];
const applied = apply(base, patches);
expect(applied).toEqual({
foo: {
customKey: { value: 2 },
},
});
});

test('#61 - type issue: current of Draft<T> type should return T type', () => {
function test<T extends { x: { y: ReadonlySet<string> } }>(base: T): T {
const [draft, f] = create(base);
Expand Down