From 903d137cea1dc25556bf2e90fda21f4d416eb72c Mon Sep 17 00:00:00 2001 From: Jan Melcher Date: Tue, 27 Jun 2017 17:22:39 +0200 Subject: [PATCH 1/2] Add missing visitNode call to object literal members Object literal elements that are neither spread elements, nor property assignments would not have visitNode called on them. Therefore, the esnext transformer would not be called on them and their children. Fixes #16765. --- src/compiler/transformers/esnext.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/compiler/transformers/esnext.ts b/src/compiler/transformers/esnext.ts index 8d71016b05..3bdcc9e9ee 100644 --- a/src/compiler/transformers/esnext.ts +++ b/src/compiler/transformers/esnext.ts @@ -158,8 +158,8 @@ namespace ts { return visitEachChild(node, visitor, context); } - function chunkObjectLiteralElements(elements: ReadonlyArray): Expression[] { - let chunkObject: (ShorthandPropertyAssignment | PropertyAssignment)[]; + function chunkObjectLiteralElements(elements: ReadonlyArray): Expression[] { + let chunkObject: ObjectLiteralElementLike[]; const objects: Expression[] = []; for (const e of elements) { if (e.kind === SyntaxKind.SpreadAssignment) { @@ -179,7 +179,7 @@ namespace ts { chunkObject.push(createPropertyAssignment(p.name, visitNode(p.initializer, visitor, isExpression))); } else { - chunkObject.push(e as ShorthandPropertyAssignment); + chunkObject.push(visitNode(e, visitor, isObjectLiteralElementLike)); } } } From 8b4db9875d6eb60a243dc7e20fc51c7c04ab569d Mon Sep 17 00:00:00 2001 From: Jan Melcher Date: Tue, 27 Jun 2017 18:03:16 +0200 Subject: [PATCH 2/2] Add test case for nested object spread / methods See #16765. --- ...preadWithinMethodWithinObjectWithSpread.js | 26 +++++++++++++++++ ...WithinMethodWithinObjectWithSpread.symbols | 24 +++++++++++++++ ...adWithinMethodWithinObjectWithSpread.types | 29 +++++++++++++++++++ ...preadWithinMethodWithinObjectWithSpread.ts | 10 +++++++ 4 files changed, 89 insertions(+) create mode 100644 tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.js create mode 100644 tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.symbols create mode 100644 tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.types create mode 100644 tests/cases/compiler/objectSpreadWithinMethodWithinObjectWithSpread.ts diff --git a/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.js b/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.js new file mode 100644 index 0000000000..e16695b655 --- /dev/null +++ b/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.js @@ -0,0 +1,26 @@ +//// [objectSpreadWithinMethodWithinObjectWithSpread.ts] +const obj = {}; +const a = { + ...obj, + prop() { + return { + ...obj, + metadata: 213 + }; + } +}; + + +//// [objectSpreadWithinMethodWithinObjectWithSpread.js] +var __assign = (this && this.__assign) || Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; +}; +var obj = {}; +var a = __assign({}, obj, { prop: function () { + return __assign({}, obj, { metadata: 213 }); + } }); diff --git a/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.symbols b/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.symbols new file mode 100644 index 0000000000..181cacd5e0 --- /dev/null +++ b/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.symbols @@ -0,0 +1,24 @@ +=== tests/cases/compiler/objectSpreadWithinMethodWithinObjectWithSpread.ts === +const obj = {}; +>obj : Symbol(obj, Decl(objectSpreadWithinMethodWithinObjectWithSpread.ts, 0, 5)) + +const a = { +>a : Symbol(a, Decl(objectSpreadWithinMethodWithinObjectWithSpread.ts, 1, 5)) + + ...obj, +>obj : Symbol(obj, Decl(objectSpreadWithinMethodWithinObjectWithSpread.ts, 0, 5)) + + prop() { +>prop : Symbol(prop, Decl(objectSpreadWithinMethodWithinObjectWithSpread.ts, 2, 11)) + + return { + ...obj, +>obj : Symbol(obj, Decl(objectSpreadWithinMethodWithinObjectWithSpread.ts, 0, 5)) + + metadata: 213 +>metadata : Symbol(metadata, Decl(objectSpreadWithinMethodWithinObjectWithSpread.ts, 5, 19)) + + }; + } +}; + diff --git a/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.types b/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.types new file mode 100644 index 0000000000..351aa0c374 --- /dev/null +++ b/tests/baselines/reference/objectSpreadWithinMethodWithinObjectWithSpread.types @@ -0,0 +1,29 @@ +=== tests/cases/compiler/objectSpreadWithinMethodWithinObjectWithSpread.ts === +const obj = {}; +>obj : {} +>{} : {} + +const a = { +>a : { prop(): { metadata: number; }; } +>{ ...obj, prop() { return { ...obj, metadata: 213 }; }} : { prop(): { metadata: number; }; } + + ...obj, +>obj : {} + + prop() { +>prop : () => { metadata: number; } + + return { +>{ ...obj, metadata: 213 } : { metadata: number; } + + ...obj, +>obj : {} + + metadata: 213 +>metadata : number +>213 : 213 + + }; + } +}; + diff --git a/tests/cases/compiler/objectSpreadWithinMethodWithinObjectWithSpread.ts b/tests/cases/compiler/objectSpreadWithinMethodWithinObjectWithSpread.ts new file mode 100644 index 0000000000..4d1663bb45 --- /dev/null +++ b/tests/cases/compiler/objectSpreadWithinMethodWithinObjectWithSpread.ts @@ -0,0 +1,10 @@ +const obj = {}; +const a = { + ...obj, + prop() { + return { + ...obj, + metadata: 213 + }; + } +};