immer
immer copied to clipboard
Throw when accessing non-draftables in strict mode
This MR attempts to implement the idea from #686
It adds a strictMode (false by default, enabled by setStrictMode or passing strictMode in Immer constructor) that makes it so that accessing non-draftable properties will throw an error, unless the access is made in an unsafe callback.
Fixes #686
Example
class Foo {}
setStrictMode(true)
produce({ instance: new Foo() }, draft => {
unsafe(() => {
draft.instance.abc = 1; // works fine in unsafe
});
console.log(draft.instance); // will throw, because access not wrapped in unsafe
});
See the tests for more examples
Limitations
I tried to make it as compliant with existing functionalities as possible. I was not able to make it fully ES5-compliant. There are a few tests that fail when strict mode is enabled in base.js tests.
I do not have enough expertise to fully fix them. Suggestions are welcome
This pull request is automatically built and testable in CodeSandbox.
To see build info of the built libraries, click here or the icon next to each commit SHA.
Latest deployment of this branch, based on commit c60c592bd26eb1fdca2644e5a0571e711762fcd1:
| Sandbox | Source |
|---|---|
| Immer sandbox | Configuration |
Really awesome! Thanks for doing this :). Looks really promising, just up front a notif that it will probably take me a while to review 😅
@mweststrate I'm glad you like it! Take your time to review it