Skip to content

Commit 6fb2ae2

Browse files
authored
Merge pull request #13757 from Microsoft/es6-target-emits-Object.assign-for-spread
ES6 and above targets emit Object.assign for object spread
2 parents aeeff28 + cca68ad commit 6fb2ae2

File tree

8 files changed

+35
-31
lines changed

8 files changed

+35
-31
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11721,7 +11721,7 @@ namespace ts {
1172111721
member = prop;
1172211722
}
1172311723
else if (memberDecl.kind === SyntaxKind.SpreadAssignment) {
11724-
if (languageVersion < ScriptTarget.ESNext) {
11724+
if (languageVersion < ScriptTarget.ES2015) {
1172511725
checkExternalEmitHelpers(memberDecl, ExternalEmitHelpers.Assign);
1172611726
}
1172711727
if (propertiesArray.length > 0) {

src/compiler/transformers/esnext.ts

+5
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ namespace ts {
402402
};
403403

404404
export function createAssignHelper(context: TransformationContext, attributesSegments: Expression[]) {
405+
if (context.getCompilerOptions().target >= ScriptTarget.ES2015) {
406+
return createCall(createPropertyAccess(createIdentifier("Object"), "assign"),
407+
/*typeArguments*/ undefined,
408+
attributesSegments);
409+
}
405410
context.requestEmitHelper(assignHelper);
406411
return createCall(
407412
getHelperName("__assign"),

tests/baselines/reference/importHelpersES6.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ declare var dec: any;
55
@dec export class A {
66

77
}
8+
9+
const o = { a: 1 };
10+
const y = { ...o };
811

912
//// [tslib.d.ts]
1013
export declare function __extends(d: Function, b: Function): void;
11-
export declare function __assign(t: any, ...sources: any[]): any;
1214
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
1315
export declare function __param(paramIndex: number, decorator: Function): Function;
1416
export declare function __metadata(metadataKey: any, metadataValue: any): Function;
@@ -23,3 +25,5 @@ A = tslib_1.__decorate([
2325
dec
2426
], A);
2527
export { A };
28+
const o = { a: 1 };
29+
const y = Object.assign({}, o);

tests/baselines/reference/importHelpersES6.symbols

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ declare var dec: any;
88

99
}
1010

11+
const o = { a: 1 };
12+
>o : Symbol(o, Decl(a.ts, 5, 5))
13+
>a : Symbol(a, Decl(a.ts, 5, 11))
14+
15+
const y = { ...o };
16+
>y : Symbol(y, Decl(a.ts, 6, 5))
17+
>o : Symbol(o, Decl(a.ts, 5, 5))
18+
1119
=== tests/cases/compiler/tslib.d.ts ===
1220
export declare function __extends(d: Function, b: Function): void;
1321
>__extends : Symbol(__extends, Decl(tslib.d.ts, --, --))
@@ -16,11 +24,6 @@ export declare function __extends(d: Function, b: Function): void;
1624
>b : Symbol(b, Decl(tslib.d.ts, --, --))
1725
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
1826

19-
export declare function __assign(t: any, ...sources: any[]): any;
20-
>__assign : Symbol(__assign, Decl(tslib.d.ts, --, --))
21-
>t : Symbol(t, Decl(tslib.d.ts, --, --))
22-
>sources : Symbol(sources, Decl(tslib.d.ts, --, --))
23-
2427
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
2528
>__decorate : Symbol(__decorate, Decl(tslib.d.ts, --, --))
2629
>decorators : Symbol(decorators, Decl(tslib.d.ts, --, --))

tests/baselines/reference/importHelpersES6.types

+11-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ declare var dec: any;
88

99
}
1010

11+
const o = { a: 1 };
12+
>o : { a: number; }
13+
>{ a: 1 } : { a: number; }
14+
>a : number
15+
>1 : 1
16+
17+
const y = { ...o };
18+
>y : { a: number; }
19+
>{ ...o } : { a: number; }
20+
>o : { a: number; }
21+
1122
=== tests/cases/compiler/tslib.d.ts ===
1223
export declare function __extends(d: Function, b: Function): void;
1324
>__extends : (d: Function, b: Function) => void
@@ -16,11 +27,6 @@ export declare function __extends(d: Function, b: Function): void;
1627
>b : Function
1728
>Function : Function
1829

19-
export declare function __assign(t: any, ...sources: any[]): any;
20-
>__assign : (t: any, ...sources: any[]) => any
21-
>t : any
22-
>sources : any[]
23-
2430
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
2531
>__decorate : (decorators: Function[], target: any, key?: string | symbol, desc?: any) => any
2632
>decorators : Function[]

tests/baselines/reference/objectRest2.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ rootConnection('test');
1515

1616

1717
//// [objectRest2.js]
18-
var __assign = (this && this.__assign) || Object.assign || function(t) {
19-
for (var s, i = 1, n = arguments.length; i < n; i++) {
20-
s = arguments[i];
21-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22-
t[p] = s[p];
23-
}
24-
return t;
25-
};
2618
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2719
return new (P || (P = Promise))(function (resolve, reject) {
2820
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -35,7 +27,7 @@ function rootConnection(name) {
3527
return {
3628
resolve: (context, args) => __awaiter(this, void 0, void 0, function* () {
3729
const { objects } = yield { objects: 12 };
38-
return __assign({}, connectionFromArray(objects, args));
30+
return Object.assign({}, connectionFromArray(objects, args));
3931
})
4032
};
4133
}

tests/baselines/reference/objectRestForOf.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ for (const norest of array.map(a => ({ ...a, x: 'a string' }))) {
1515

1616

1717
//// [objectRestForOf.js]
18-
var __assign = (this && this.__assign) || Object.assign || function(t) {
19-
for (var s, i = 1, n = arguments.length; i < n; i++) {
20-
s = arguments[i];
21-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22-
t[p] = s[p];
23-
}
24-
return t;
25-
};
2618
var __rest = (this && this.__rest) || function (s, e) {
2719
var t = {};
2820
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
@@ -43,6 +35,6 @@ for (let _b of array) {
4335
({ x: xx } = _b, rrestOff = __rest(_b, ["x"]));
4436
[xx, rrestOff];
4537
}
46-
for (const norest of array.map(a => (__assign({}, a, { x: 'a string' })))) {
38+
for (const norest of array.map(a => (Object.assign({}, a, { x: 'a string' })))) {
4739
[norest.x, norest.y];
4840
}

tests/cases/compiler/importHelpersES6.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ declare var dec: any;
77

88
}
99

10+
const o = { a: 1 };
11+
const y = { ...o };
12+
1013
// @filename: tslib.d.ts
1114
export declare function __extends(d: Function, b: Function): void;
12-
export declare function __assign(t: any, ...sources: any[]): any;
1315
export declare function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
1416
export declare function __param(paramIndex: number, decorator: Function): Function;
1517
export declare function __metadata(metadataKey: any, metadataValue: any): Function;

0 commit comments

Comments
 (0)