Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use single object spread call in loose mode (#11520)
- Loading branch information
1 parent
b8a6145
commit a080a1d
Showing
32 changed files
with
383 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
...proposal-object-rest-spread/test/fixtures/object-spread-loose-builtins/expression/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var log = []; | ||
|
||
var a = { | ||
...{ get foo() { log.push(1); } }, | ||
get bar() { log.push(2); } | ||
}; | ||
|
||
// Loose mode uses regular Get, not GetOwnProperty. | ||
expect(log).toEqual([1, 2]); |
16 changes: 16 additions & 0 deletions
16
...roposal-object-rest-spread/test/fixtures/object-spread-loose-builtins/expression/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var a; | ||
var b; | ||
var c; | ||
var d; | ||
var x; | ||
var y; | ||
|
||
({ x, ...y, a, ...b, c }); | ||
|
||
({ ...Object.prototype }); | ||
|
||
({ ...{ foo: 'bar' } }); | ||
|
||
({ ...{ foo: 'bar' }, ...{ bar: 'baz' } }); | ||
|
||
({ ...{ get foo () { return 'foo' } } }); |
28 changes: 28 additions & 0 deletions
28
...oposal-object-rest-spread/test/fixtures/object-spread-loose-builtins/expression/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var a; | ||
var b; | ||
var c; | ||
var d; | ||
var x; | ||
var y; | ||
Object.assign({ | ||
x | ||
}, y, { | ||
a | ||
}, b, { | ||
c | ||
}); | ||
Object.assign({}, Object.prototype); | ||
Object.assign({}, { | ||
foo: 'bar' | ||
}); | ||
Object.assign({}, { | ||
foo: 'bar' | ||
}, { | ||
bar: 'baz' | ||
}); | ||
Object.assign({}, { | ||
get foo() { | ||
return 'foo'; | ||
} | ||
|
||
}); |
8 changes: 8 additions & 0 deletions
8
...st-spread/test/fixtures/object-spread-loose-builtins/no-getOwnPropertyDescriptors/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const oldGOPDs = Object.getOwnPropertyDescriptors; | ||
Object.getOwnPropertyDescriptors = null; | ||
|
||
try { | ||
({ ...{ a: 1 }, b: 1, ...{} }); | ||
} finally { | ||
Object.getOwnPropertyDescriptors = oldGOPDs; | ||
} |
46 changes: 46 additions & 0 deletions
46
...ject-rest-spread/test/fixtures/object-spread-loose-builtins/no-object-assign-exec/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"use strict"; | ||
const NOSET = `NOSET${__filename}`; | ||
const NOWRITE = `NOWRITE${__filename}`; | ||
|
||
Object.defineProperty(Object.prototype, NOSET, { | ||
get(value) { | ||
// noop | ||
}, | ||
}); | ||
|
||
Object.defineProperty(Object.prototype, NOWRITE, { | ||
writable: false, | ||
value: 'abc', | ||
}); | ||
|
||
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 }; | ||
// 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) | ||
expect(() => { | ||
const obj2Spread = { ...obj2 }; | ||
}).toThrow(); | ||
|
||
const KEY = Symbol('key'); | ||
const obj3Spread = { ...{ get foo () { return 'bar' } }, [KEY]: 'symbol' }; | ||
expect(Object.getOwnPropertyDescriptor(obj3Spread, 'foo').value).toBe('bar'); | ||
expect(Object.getOwnPropertyDescriptor(obj3Spread, KEY).value).toBe('symbol'); | ||
|
||
const obj4Spread = { ...Object.prototype }; | ||
expect(Object.getOwnPropertyDescriptor(obj4Spread, 'hasOwnProperty')).toBeUndefined(); | ||
|
||
expect(() => ({ ...null, ...undefined })).not.toThrow(); | ||
|
||
const o = Object.create(null); | ||
o.a = 'foo'; | ||
o.__proto__ = []; | ||
const o2 = { ...o }; | ||
// Loose will do o2.__proto__ = [] | ||
expect(Array.isArray(Object.getPrototypeOf(o2))).toBe(true); |
File renamed without changes.
12 changes: 12 additions & 0 deletions
12
...roposal-object-rest-spread/test/fixtures/object-spread-loose-builtins/side-effect/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var k = { a: 1, b: 2 }; | ||
var o = { a: 3, ...k, b: k.a++ }; | ||
// Loose will evaluate the static `b: k.a++` before spreading `...k`. | ||
// It should be {a: 1, b: 1} | ||
expect(o).toEqual({a: 2, b: 1}); | ||
|
||
var k = { a: 1, get b() { l = { z: 9 }; return 2; } }; | ||
var l = { c: 3 }; | ||
var o = { ...k, ...l }; | ||
// Loose will evaluate the `l` before spreading `...k`. | ||
// It should be {a: 1, b: 2, z: 9} | ||
expect(o).toEqual({ a: 1, b: 2, c: 3 }); |
16 changes: 16 additions & 0 deletions
16
...oposal-object-rest-spread/test/fixtures/object-spread-loose-builtins/side-effect/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var k = { a: 1, b: 2 }; | ||
var o = { a: 3, ...k, b: k.a++ }; | ||
|
||
var pureA = {}; | ||
var pureB = {}; | ||
var pureC = {}; | ||
var pureD = {}; | ||
var pureE = {}; | ||
|
||
function impureFunc() { | ||
console.log('hello') | ||
} | ||
|
||
var output = { ...pureA, get foo() {}, get bar() {}, ...pureB, ...pureC, ...impureFunc(), ...pureD, pureD } | ||
|
||
var simpleOutput = { ...pureA, test: '1', ...pureB, } |
30 changes: 30 additions & 0 deletions
30
...posal-object-rest-spread/test/fixtures/object-spread-loose-builtins/side-effect/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var k = { | ||
a: 1, | ||
b: 2 | ||
}; | ||
var o = Object.assign({ | ||
a: 3 | ||
}, k, { | ||
b: k.a++ | ||
}); | ||
var pureA = {}; | ||
var pureB = {}; | ||
var pureC = {}; | ||
var pureD = {}; | ||
var pureE = {}; | ||
|
||
function impureFunc() { | ||
console.log('hello'); | ||
} | ||
|
||
var output = Object.assign({}, pureA, { | ||
get foo() {}, | ||
|
||
get bar() {} | ||
|
||
}, pureB, pureC, impureFunc(), pureD, { | ||
pureD | ||
}); | ||
var simpleOutput = Object.assign({}, pureA, { | ||
test: '1' | ||
}, pureB); |
3 changes: 3 additions & 0 deletions
3
...roposal-object-rest-spread/test/fixtures/object-spread-loose-builtins/string-exec/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
expect([...'']).toHaveLength(0); | ||
expect([...'abc']).toHaveLength(3); | ||
expect([...'def']).toMatchObject(['d','e','f']); |
1 change: 1 addition & 0 deletions
1
...ject-rest-spread/test/fixtures/object-spread-loose-builtins/variable-declaration/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var z = { ...x }; |
1 change: 1 addition & 0 deletions
1
...ect-rest-spread/test/fixtures/object-spread-loose-builtins/variable-declaration/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var z = Object.assign({}, x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,3 @@ var z; | |
z = { x, ...y }; | ||
|
||
z = { x, w: { ...y } }; | ||
|
||
const { q, ...rest } = z; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...l-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/expression/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var log = []; | ||
|
||
var a = { | ||
...{ get foo() { log.push(1); } }, | ||
get bar() { log.push(2); } | ||
}; | ||
|
||
// Loose mode uses regular Get, not GetOwnProperty. | ||
expect(log).toEqual([1, 2]); |
16 changes: 16 additions & 0 deletions
16
...-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/expression/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var a; | ||
var b; | ||
var c; | ||
var d; | ||
var x; | ||
var y; | ||
|
||
({ x, ...y, a, ...b, c }); | ||
|
||
({ ...Object.prototype }); | ||
|
||
({ ...{ foo: 'bar' } }); | ||
|
||
({ ...{ foo: 'bar' }, ...{ bar: 'baz' } }); | ||
|
||
({ ...{ get foo () { return 'foo' } } }); |
35 changes: 35 additions & 0 deletions
35
...plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/expression/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
|
||
var a; | ||
var b; | ||
var c; | ||
var d; | ||
var x; | ||
var y; | ||
|
||
_extends({ | ||
x | ||
}, y, { | ||
a | ||
}, b, { | ||
c | ||
}); | ||
|
||
_extends({}, Object.prototype); | ||
|
||
_extends({}, { | ||
foo: 'bar' | ||
}); | ||
|
||
_extends({}, { | ||
foo: 'bar' | ||
}, { | ||
bar: 'baz' | ||
}); | ||
|
||
_extends({}, { | ||
get foo() { | ||
return 'foo'; | ||
} | ||
|
||
}); |
9 changes: 9 additions & 0 deletions
9
...object-rest-spread/test/fixtures/object-spread-loose/no-getOwnPropertyDescriptors/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const oldGOPDs = Object.getOwnPropertyDescriptors; | ||
Object.getOwnPropertyDescriptors = null; | ||
|
||
try { | ||
({ ...{ a: 1 }, b: 1, ...{} }); | ||
} finally { | ||
Object.getOwnPropertyDescriptors = oldGOPDs; | ||
} | ||
|
46 changes: 46 additions & 0 deletions
46
...oposal-object-rest-spread/test/fixtures/object-spread-loose/no-object-assign-exec/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"use strict"; | ||
const NOSET = `NOSET${__filename}`; | ||
const NOWRITE = `NOWRITE${__filename}`; | ||
|
||
Object.defineProperty(Object.prototype, NOSET, { | ||
get(value) { | ||
// noop | ||
}, | ||
}); | ||
|
||
Object.defineProperty(Object.prototype, NOWRITE, { | ||
writable: false, | ||
value: 'abc', | ||
}); | ||
|
||
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 }; | ||
// 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) | ||
expect(() => { | ||
const obj2Spread = { ...obj2 }; | ||
}).toThrow(); | ||
|
||
const KEY = Symbol('key'); | ||
const obj3Spread = { ...{ get foo () { return 'bar' } }, [KEY]: 'symbol' }; | ||
expect(Object.getOwnPropertyDescriptor(obj3Spread, 'foo').value).toBe('bar'); | ||
expect(Object.getOwnPropertyDescriptor(obj3Spread, KEY).value).toBe('symbol'); | ||
|
||
const obj4Spread = { ...Object.prototype }; | ||
expect(Object.getOwnPropertyDescriptor(obj4Spread, 'hasOwnProperty')).toBeUndefined(); | ||
|
||
expect(() => ({ ...null, ...undefined })).not.toThrow(); | ||
|
||
const o = Object.create(null); | ||
o.a = 'foo'; | ||
o.__proto__ = []; | ||
const o2 = { ...o }; | ||
// Loose will do o2.__proto__ = [] | ||
expect(Array.isArray(Object.getPrototypeOf(o2))).toBe(true); |
5 changes: 5 additions & 0 deletions
5
...s/babel-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
["proposal-object-rest-spread", { "loose": true }] | ||
] | ||
} |
12 changes: 12 additions & 0 deletions
12
...-plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/side-effect/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var k = { a: 1, b: 2 }; | ||
var o = { a: 3, ...k, b: k.a++ }; | ||
// Loose will evaluate the static `b: k.a++` before spreading `...k`. | ||
// It should be {a: 1, b: 1} | ||
expect(o).toEqual({a: 2, b: 1}); | ||
|
||
var k = { a: 1, get b() { l = { z: 9 }; return 2; } }; | ||
var l = { c: 3 }; | ||
var o = { ...k, ...l }; | ||
// Loose will evaluate the `l` before spreading `...k`. | ||
// It should be {a: 1, b: 2, z: 9} | ||
expect(o).toEqual({ a: 1, b: 2, c: 3 }); |
16 changes: 16 additions & 0 deletions
16
...plugin-proposal-object-rest-spread/test/fixtures/object-spread-loose/side-effect/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var k = { a: 1, b: 2 }; | ||
var o = { a: 3, ...k, b: k.a++ }; | ||
|
||
var pureA = {}; | ||
var pureB = {}; | ||
var pureC = {}; | ||
var pureD = {}; | ||
var pureE = {}; | ||
|
||
function impureFunc() { | ||
console.log('hello') | ||
} | ||
|
||
var output = { ...pureA, get foo() {}, get bar() {}, ...pureB, ...pureC, ...impureFunc(), ...pureD, pureD } | ||
|
||
var simpleOutput = { ...pureA, test: '1', ...pureB, } |
Oops, something went wrong.