Skip to content

Commit

Permalink
es2018: visit other binding elements when transforming object destruc…
Browse files Browse the repository at this point in the history
…turing with rest (#35872)

* es2018: visit other binding elements when transforming object destructuring with rest

fixes: #35771

* more tests
  • Loading branch information
ajafff authored and rbuckton committed Jan 24, 2020
1 parent 7c05e1a commit 8e0b091
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/transformers/destructuring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ namespace ts {
&& !(element.transformFlags & (TransformFlags.ContainsRestOrSpread | TransformFlags.ContainsObjectRestOrSpread))
&& !(getTargetOfBindingOrAssignmentElement(element)!.transformFlags & (TransformFlags.ContainsRestOrSpread | TransformFlags.ContainsObjectRestOrSpread))
&& !isComputedPropertyName(propertyName)) {
bindingElements = append(bindingElements, element);
bindingElements = append(bindingElements, visitNode(element, flattenContext.visitor));
}
else {
if (bindingElements) {
Expand Down
54 changes: 54 additions & 0 deletions tests/baselines/reference/objectRestSpread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//// [objectRestSpread.ts]
let obj = {};

({...obj});
let {
prop = { ...obj },
more = { ...obj } = { ...obj },
['' + 'other']: other = { ...obj },
yetAnother: {nested: { ['nested' + 'prop']: nestedProp = { ...obj }, ...nestedRest } = { ...obj }} = { ...obj },
fn = async function*() {},
...props
} = {} as any;

({
prop = { ...obj },
['' + 'other']: other = { ...obj },
...props
} = {} as any)

function test({
prop = { ...obj },
...props
}) {}

//// [objectRestSpread.js]
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
var _a, _b, _c, _d;
let obj = {};
(Object.assign({}, obj));
let _e = {}, { prop = Object.assign({}, obj), more = _a = Object.assign({}, obj), obj = __rest(_a, []), _a } = _e, _f = '' + 'other', _g = _e[_f], other = _g === void 0 ? Object.assign({}, obj) : _g, _h = _e.yetAnother, _j = (_h === void 0 ? Object.assign({}, obj) : _h).nested, _k = _j === void 0 ? Object.assign({}, obj) : _j, _l = 'nested' + 'prop', _m = _k[_l], nestedProp = _m === void 0 ? Object.assign({}, obj) : _m, nestedRest = __rest(_k, [typeof _l === "symbol" ? _l : _l + ""]), { fn = function () { return __asyncGenerator(this, arguments, function* () { }); } } = _e, props = __rest(_e, ["prop", "more", typeof _f === "symbol" ? _f : _f + "", "yetAnother", "fn"]);
(_b = {}, { prop = Object.assign({}, obj) } = _b, _c = '' + 'other', _d = _b[_c], other = _d === void 0 ? Object.assign({}, obj) : _d, props = __rest(_b, ["prop", typeof _c === "symbol" ? _c : _c + ""]));
function test(_a) { var { prop = Object.assign({}, obj) } = _a, props = __rest(_a, ["prop"]); }
25 changes: 25 additions & 0 deletions tests/cases/compiler/objectRestSpread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// @target: es2017
// @lib: es2018
// @noTypesAndSymbols: true
let obj = {};

({...obj});
let {
prop = { ...obj },
more = { ...obj } = { ...obj },
['' + 'other']: other = { ...obj },
yetAnother: {nested: { ['nested' + 'prop']: nestedProp = { ...obj }, ...nestedRest } = { ...obj }} = { ...obj },
fn = async function*() {},
...props
} = {} as any;

({
prop = { ...obj },
['' + 'other']: other = { ...obj },
...props
} = {} as any)

function test({
prop = { ...obj },
...props
}) {}

0 comments on commit 8e0b091

Please sign in to comment.