forked from babel/babel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
objectRestNoSymbols
, setSpreadProperties
and `pureGette…
…rs` (babel#12505)
- Loading branch information
1 parent
2abcd19
commit 2adb1c8
Showing
46 changed files
with
452 additions
and
18 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
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
...gin-proposal-object-rest-spread/test/fixtures/assumption-objectRestNoSymbols/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,9 @@ | ||
{ | ||
"plugins": [ | ||
["external-helpers", { "helperVersion": "7.100.0"}], | ||
"proposal-object-rest-spread" | ||
], | ||
"assumptions": { | ||
"objectRestNoSymbols": true | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...t-spread/test/fixtures/assumption-objectRestNoSymbols/rest-assignment-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 @@ | ||
({ a, b, ...c } = obj); |
7 changes: 7 additions & 0 deletions
7
...-spread/test/fixtures/assumption-objectRestNoSymbols/rest-assignment-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,7 @@ | ||
var _obj = obj; | ||
({ | ||
a, | ||
b | ||
} = _obj); | ||
c = babelHelpers.objectWithoutPropertiesLoose(_obj, ["a", "b"]); | ||
_obj; |
1 change: 1 addition & 0 deletions
1
...al-object-rest-spread/test/fixtures/assumption-objectRestNoSymbols/rest-computed/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 @@ | ||
let { [a]: b, ...c } = obj; |
5 changes: 5 additions & 0 deletions
5
...l-object-rest-spread/test/fixtures/assumption-objectRestNoSymbols/rest-computed/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,5 @@ | ||
let _a = a, | ||
{ | ||
[_a]: b | ||
} = obj, | ||
c = babelHelpers.objectWithoutPropertiesLoose(obj, [_a].map(babelHelpers.toPropertyKey)); |
7 changes: 7 additions & 0 deletions
7
...ject-rest-spread/test/fixtures/assumption-objectRestNoSymbols/rest-ignore-symbols/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,7 @@ | ||
let sym = Symbol(); | ||
|
||
let { a, ...r } = { a: 1, b: 2, [sym]: 3 }; | ||
|
||
expect(a).toBe(1); | ||
expect(r.b).toBe(2); | ||
expect(sym in r).toBe(false); |
1 change: 1 addition & 0 deletions
1
...osal-object-rest-spread/test/fixtures/assumption-objectRestNoSymbols/rest-nested/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 @@ | ||
let { a, nested: { b, c, ...d }, e } = obj; |
9 changes: 9 additions & 0 deletions
9
...sal-object-rest-spread/test/fixtures/assumption-objectRestNoSymbols/rest-nested/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,9 @@ | ||
let { | ||
a, | ||
nested: { | ||
b, | ||
c | ||
}, | ||
e | ||
} = obj, | ||
d = babelHelpers.objectWithoutPropertiesLoose(obj.nested, ["b", "c"]); |
1 change: 1 addition & 0 deletions
1
...ct-rest-spread/test/fixtures/assumption-objectRestNoSymbols/rest-var-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 { a, b, ...c } = obj; |
5 changes: 5 additions & 0 deletions
5
...t-rest-spread/test/fixtures/assumption-objectRestNoSymbols/rest-var-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,5 @@ | ||
var { | ||
a, | ||
b | ||
} = obj, | ||
c = babelHelpers.objectWithoutPropertiesLoose(obj, ["a", "b"]); |
9 changes: 9 additions & 0 deletions
9
...abel-plugin-proposal-object-rest-spread/test/fixtures/assumption-pureGetters/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,9 @@ | ||
{ | ||
"plugins": [ | ||
["external-helpers", { "helperVersion": "7.100.0"}], | ||
"proposal-object-rest-spread" | ||
], | ||
"assumptions": { | ||
"pureGetters": true | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...rest-spread/test/fixtures/assumption-pureGetters/rest-remove-unused-excluded-keys/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,11 @@ | ||
let called = false; | ||
let obj = { | ||
get foo() { called = true } | ||
}; | ||
|
||
let { foo, ...rest } = obj; | ||
|
||
expect("foo" in rest).toBe(false); | ||
|
||
// Without assuming that getters are pure (in this case it isn't), this should be true | ||
expect(called).toBe(false); |
20 changes: 20 additions & 0 deletions
20
...est-spread/test/fixtures/assumption-pureGetters/rest-remove-unused-excluded-keys/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,20 @@ | ||
// should not remove when destructuring into existing bindings | ||
({ a2, ...b2 } = c2); | ||
|
||
function render() { | ||
const { | ||
excluded, | ||
excluded2: excludedRenamed, | ||
used, | ||
used2: usedRenamed, | ||
...props | ||
} = this.props; | ||
|
||
console.log(used, usedRenamed); | ||
|
||
return React.createElement("input", props); | ||
} | ||
|
||
function smth({ unused, ...rest }) { | ||
call(rest); | ||
} |
23 changes: 23 additions & 0 deletions
23
...st-spread/test/fixtures/assumption-pureGetters/rest-remove-unused-excluded-keys/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,23 @@ | ||
// should not remove when destructuring into existing bindings | ||
var _c = c2; | ||
({ | ||
a2 | ||
} = _c); | ||
b2 = babelHelpers.objectWithoutProperties(_c, ["a2"]); | ||
_c; | ||
|
||
function render() { | ||
const _this$props = this.props, | ||
{ | ||
used, | ||
used2: usedRenamed | ||
} = _this$props, | ||
props = babelHelpers.objectWithoutProperties(_this$props, ["excluded", "excluded2", "used", "used2"]); | ||
console.log(used, usedRenamed); | ||
return React.createElement("input", props); | ||
} | ||
|
||
function smth(_ref) { | ||
let rest = babelHelpers.objectWithoutProperties(_ref, ["unused"]); | ||
call(rest); | ||
} |
10 changes: 10 additions & 0 deletions
10
...oposal-object-rest-spread/test/fixtures/assumption-pureGetters/spread-single-call/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,10 @@ | ||
let count = 0; | ||
|
||
let withFoo = { get foo() { return count++; } }; | ||
let withBar = { get bar() { return count++; } }; | ||
|
||
let res = { ...withFoo, middle: count, ...withBar }; | ||
|
||
// Without assuming that getters are pure (in this case it isn't), | ||
// the result should be { foo: 0, middle: 1, bar: 1 } | ||
expect(res).toEqual({ foo: 0, middle: 0, bar: 1 }); |
1 change: 1 addition & 0 deletions
1
...posal-object-rest-spread/test/fixtures/assumption-pureGetters/spread-single-call/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 @@ | ||
let obj = { a, ...b, c, ...d, e }; |
7 changes: 7 additions & 0 deletions
7
...osal-object-rest-spread/test/fixtures/assumption-pureGetters/spread-single-call/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,7 @@ | ||
let obj = babelHelpers.objectSpread2({ | ||
a | ||
}, b, { | ||
c | ||
}, d, { | ||
e | ||
}); |
7 changes: 7 additions & 0 deletions
7
...-spread/test/fixtures/assumption-setSpreadProperties-with-useBuiltIns/assignment/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,7 @@ | ||
var x; | ||
var y; | ||
var z; | ||
|
||
z = { x, ...y }; | ||
|
||
z = { x, w: { ...y } }; |
10 changes: 10 additions & 0 deletions
10
...spread/test/fixtures/assumption-setSpreadProperties-with-useBuiltIns/assignment/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,10 @@ | ||
var x; | ||
var y; | ||
var z; | ||
z = Object.assign({ | ||
x | ||
}, y); | ||
z = { | ||
x, | ||
w: Object.assign({}, y) | ||
}; |
9 changes: 9 additions & 0 deletions
9
...t-spread/test/fixtures/assumption-setSpreadProperties-with-useBuiltIns/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
...-spread/test/fixtures/assumption-setSpreadProperties-with-useBuiltIns/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
...spread/test/fixtures/assumption-setSpreadProperties-with-useBuiltIns/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(Object.assign(Object.assign({ | ||
x | ||
}, y), {}, { | ||
a | ||
}, b), {}, { | ||
c | ||
}); | ||
Object.assign({}, Object.prototype); | ||
Object.assign({}, { | ||
foo: 'bar' | ||
}); | ||
Object.assign(Object.assign({}, { | ||
foo: 'bar' | ||
}), { | ||
bar: 'baz' | ||
}); | ||
Object.assign({}, { | ||
get foo() { | ||
return 'foo'; | ||
} | ||
|
||
}); |
9 changes: 9 additions & 0 deletions
9
...ures/assumption-setSpreadProperties-with-useBuiltIns/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; | ||
} | ||
|
43 changes: 43 additions & 0 deletions
43
...st/fixtures/assumption-setSpreadProperties-with-useBuiltIns/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,43 @@ | ||
"use strict"; | ||
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); |
9 changes: 9 additions & 0 deletions
9
...ct-rest-spread/test/fixtures/assumption-setSpreadProperties-with-useBuiltIns/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,9 @@ | ||
{ | ||
"plugins": [ | ||
["external-helpers", { "helperVersion": "7.100.0"}], | ||
["proposal-object-rest-spread", { "useBuiltIns": true }] | ||
], | ||
"assumptions": { | ||
"setSpreadProperties": true | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...posal-object-rest-spread/test/fixtures/assumption-setSpreadProperties/assignment/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,7 @@ | ||
var x; | ||
var y; | ||
var z; | ||
|
||
z = { x, ...y }; | ||
|
||
z = { x, w: { ...y } }; |
10 changes: 10 additions & 0 deletions
10
...osal-object-rest-spread/test/fixtures/assumption-setSpreadProperties/assignment/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,10 @@ | ||
var x; | ||
var y; | ||
var z; | ||
z = babelHelpers.extends({ | ||
x | ||
}, y); | ||
z = { | ||
x, | ||
w: babelHelpers.extends({}, y) | ||
}; |
Oops, something went wrong.