Skip to content

Commit

Permalink
Use unique object property to avoid polluted contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed May 7, 2020
1 parent 64da98d commit 654e0a3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
@@ -1,23 +1,26 @@
"use strict";
Object.defineProperty(Object.prototype, 'NOSET', {
const NOSET = `NOSET${__filename}`;
const NOWRITE = `NOWRITE${__filename}`;

Object.defineProperty(Object.prototype, NOSET, {
get(value) {
// noop
},
});

Object.defineProperty(Object.prototype, 'NOWRITE', {
Object.defineProperty(Object.prototype, NOWRITE, {
writable: false,
value: 'abc',
});

const obj = { NOSET: 123 };
const obj = { [NOSET]: 123 };
// this won't work as expected if transformed as Object.assign (or equivalent)
// because those trigger object setters (spread don't)
expect(() => {
const objSpread = { ...obj };
}).toThrow();

const obj2 = { NOWRITE: 456 };
const obj2 = { [NOWRITE]: 456 };
// this throws `TypeError: Cannot assign to read only property 'NOWRITE'`
// if transformed as Object.assign (or equivalent) because those use *assignment* for creating properties
// (spread defines them)
Expand Down
@@ -1,23 +1,26 @@
"use strict";
Object.defineProperty(Object.prototype, 'NOSET', {
const NOSET = `NOSET${__filename}`;
const NOWRITE = `NOWRITE${__filename}`;

Object.defineProperty(Object.prototype, NOSET, {
get(value) {
// noop
},
});

Object.defineProperty(Object.prototype, 'NOWRITE', {
Object.defineProperty(Object.prototype, NOWRITE, {
writable: false,
value: 'abc',
});

const obj = { NOSET: 123 };
const obj = { [NOSET]: 123 };
// this won't work as expected if transformed as Object.assign (or equivalent)
// because those trigger object setters (spread don't)
expect(() => {
const objSpread = { ...obj };
}).toThrow();

const obj2 = { NOWRITE: 456 };
const obj2 = { [NOWRITE]: 456 };
// this throws `TypeError: Cannot assign to read only property 'NOWRITE'`
// if transformed as Object.assign (or equivalent) because those use *assignment* for creating properties
// (spread defines them)
Expand Down
@@ -1,20 +1,24 @@
Object.defineProperty(Object.prototype, 'NOSET', {
"use strict";
const NOSET = `NOSET${__filename}`;
const NOWRITE = `NOWRITE${__filename}`;

Object.defineProperty(Object.prototype, NOSET, {
set(value) {
// noop
},
});

Object.defineProperty(Object.prototype, 'NOWRITE', {
Object.defineProperty(Object.prototype, NOWRITE, {
writable: false,
value: 'abc',
});

const obj = { NOSET: 123 };
const obj = { [NOSET]: 123 };
// this wouldn't work as expected if transformed as Object.assign (or equivalent)
// because those trigger object setters (spread don't)
const objSpread = { ...obj };

const obj2 = { NOSET: 123, NOWRITE: 456 };
const obj2 = { NOSET: 123, [NOWRITE]: 456 };
// this line would throw `TypeError: Cannot assign to read only property 'NOWRITE'`
// if transformed as Object.assign (or equivalent) because those use *assignment* for creating properties
// (spread defines them)
Expand Down

0 comments on commit 654e0a3

Please sign in to comment.