MCPcopy Index your code
hub / github.com/nodejs/node / mustNotMutateObjectDeep

Function mustNotMutateObjectDeep

test/common/index.js:575–617  ·  view source on GitHub ↗
(original)

Source from the content-addressed store, hash-verified

573const _mustNotMutateObjectDeepProxies = new WeakMap();
574
575function mustNotMutateObjectDeep(original) {
576 // Return primitives and functions directly. Primitives are immutable, and
577 // proxied functions are impossible to compare against originals, e.g. with
578 // `assert.deepEqual()`.
579 if (original === null || typeof original !== 'object') {
580 return original;
581 }
582
583 const cachedProxy = _mustNotMutateObjectDeepProxies.get(original);
584 if (cachedProxy) {
585 return cachedProxy;
586 }
587
588 const _mustNotMutateObjectDeepHandler = {
589 __proto__: null,
590 defineProperty(target, property, descriptor) {
591 assert.fail(`Expected no side effects, got ${inspect(property)} ` +
592 'defined');
593 },
594 deleteProperty(target, property) {
595 assert.fail(`Expected no side effects, got ${inspect(property)} ` +
596 'deleted');
597 },
598 get(target, prop, receiver) {
599 return mustNotMutateObjectDeep(Reflect.get(target, prop, receiver));
600 },
601 preventExtensions(target) {
602 assert.fail('Expected no side effects, got extensions prevented on ' +
603 inspect(target));
604 },
605 set(target, property, value, receiver) {
606 assert.fail(`Expected no side effects, got ${inspect(value)} ` +
607 `assigned to ${inspect(property)}`);
608 },
609 setPrototypeOf(target, prototype) {
610 assert.fail(`Expected no side effects, got set prototype to ${prototype}`);
611 },
612 };
613
614 const proxy = new Proxy(original, _mustNotMutateObjectDeepHandler);
615 _mustNotMutateObjectDeepProxies.set(original, proxy);
616 return proxy;
617}
618
619function printSkipMessage(msg) {
620 console.log(`1..0 # Skipped: ${msg}`);

Calls 2

getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…