Skip to content

Commit 7b1214e

Browse files
JoostKthePunderWoman
authored andcommitted
feat(ngcc): support __spreadArray helper as used by TypeScript 4.2 (angular#41201)
In TypeScript 4.2 the `__spread` and `__spreadArrays` helpers were both replaced by the new helper function `__spreadArray` in microsoft/TypeScript#41523. These helpers may be used in downleveled JavaScript bundles that ngcc has to process, so ngcc has the ability to statically detect these helpers and provide evaluation logic for them. Because Angular is adopting support for TypeScript 4.2 it becomes possible for libraries to be compiled by TypeScript 4.2 and thus ngcc has to add support for the `__spreadArray` helper. The deprecated `__spread` and `__spreadArrays` helpers are not affected by this change. Closes angular#40394 PR Close angular#41201
1 parent 6bdad5e commit 7b1214e

File tree

9 files changed

+181
-4
lines changed

9 files changed

+181
-4
lines changed

packages/compiler-cli/ngcc/src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ export function getTsHelperFnFromIdentifier(id: ts.Identifier): KnownDeclaration
161161
return KnownDeclaration.TsHelperSpread;
162162
case '__spreadArrays':
163163
return KnownDeclaration.TsHelperSpreadArrays;
164+
case '__spreadArray':
165+
return KnownDeclaration.TsHelperSpreadArray;
164166
default:
165167
return null;
166168
}

packages/compiler-cli/ngcc/test/host/commonjs_host_spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,10 +1999,12 @@ exports.MissingClass2 = MissingClass2;
19991999
function __assign(t, ...sources) { /* ... */ }
20002000
function __spread(...args) { /* ... */ }
20012001
function __spreadArrays(...args) { /* ... */ }
2002+
function __spreadArray(to, from) { /* ... */ }
20022003
20032004
var a = __assign({foo: 'bar'}, {baz: 'qux'});
20042005
var b = __spread(['foo', 'bar'], ['baz', 'qux']);
20052006
var c = __spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2007+
var d = __spreadArray(['foo', 'bar'], ['baz', 'qux']);
20062008
`,
20072009
};
20082010
loadTestFiles([file]);
@@ -2018,6 +2020,7 @@ exports.MissingClass2 = MissingClass2;
20182020
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign);
20192021
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread);
20202022
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays);
2023+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray);
20212024
});
20222025

20232026
it('should recognize suffixed TypeScript helpers (as function declarations)', () => {
@@ -2027,10 +2030,12 @@ exports.MissingClass2 = MissingClass2;
20272030
function __assign$1(t, ...sources) { /* ... */ }
20282031
function __spread$2(...args) { /* ... */ }
20292032
function __spreadArrays$3(...args) { /* ... */ }
2033+
function __spreadArray$3(to, from) { /* ... */ }
20302034
20312035
var a = __assign$1({foo: 'bar'}, {baz: 'qux'});
20322036
var b = __spread$2(['foo', 'bar'], ['baz', 'qux']);
20332037
var c = __spreadArrays$3(['foo', 'bar'], ['baz', 'qux']);
2038+
var d = __spreadArray$3(['foo', 'bar'], ['baz', 'qux']);
20342039
`,
20352040
};
20362041
loadTestFiles([file]);
@@ -2046,6 +2051,7 @@ exports.MissingClass2 = MissingClass2;
20462051
testForHelper('a', '__assign$1', KnownDeclaration.TsHelperAssign);
20472052
testForHelper('b', '__spread$2', KnownDeclaration.TsHelperSpread);
20482053
testForHelper('c', '__spreadArrays$3', KnownDeclaration.TsHelperSpreadArrays);
2054+
testForHelper('d', '__spreadArray$3', KnownDeclaration.TsHelperSpreadArray);
20492055
});
20502056

20512057
it('should recognize TypeScript helpers (as variable declarations)', () => {
@@ -2055,10 +2061,12 @@ exports.MissingClass2 = MissingClass2;
20552061
var __assign = (this && this.__assign) || function (t, ...sources) { /* ... */ }
20562062
var __spread = (this && this.__spread) || function (...args) { /* ... */ }
20572063
var __spreadArrays = (this && this.__spreadArrays) || function (...args) { /* ... */ }
2064+
var __spreadArray = (this && this.__spreadArray) || function (to, from) { /* ... */ }
20582065
20592066
var a = __assign({foo: 'bar'}, {baz: 'qux'});
20602067
var b = __spread(['foo', 'bar'], ['baz', 'qux']);
20612068
var c = __spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2069+
var d = __spreadArray(['foo', 'bar'], ['baz', 'qux']);
20622070
`,
20632071
};
20642072
loadTestFiles([file]);
@@ -2074,6 +2082,7 @@ exports.MissingClass2 = MissingClass2;
20742082
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign);
20752083
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread);
20762084
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays);
2085+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray);
20772086
});
20782087

20792088
it('should recognize suffixed TypeScript helpers (as variable declarations)', () => {
@@ -2083,10 +2092,12 @@ exports.MissingClass2 = MissingClass2;
20832092
var __assign$1 = (this && this.__assign$1) || function (t, ...sources) { /* ... */ }
20842093
var __spread$2 = (this && this.__spread$2) || function (...args) { /* ... */ }
20852094
var __spreadArrays$3 = (this && this.__spreadArrays$3) || function (...args) { /* ... */ }
2095+
var __spreadArray$3 = (this && this.__spreadArray$3) || function (to, from) { /* ... */ }
20862096
20872097
var a = __assign$1({foo: 'bar'}, {baz: 'qux'});
20882098
var b = __spread$2(['foo', 'bar'], ['baz', 'qux']);
20892099
var c = __spreadArrays$3(['foo', 'bar'], ['baz', 'qux']);
2100+
var d = __spreadArray$3(['foo', 'bar'], ['baz', 'qux']);
20902101
`,
20912102
};
20922103
loadTestFiles([file]);
@@ -2102,6 +2113,7 @@ exports.MissingClass2 = MissingClass2;
21022113
testForHelper('a', '__assign$1', KnownDeclaration.TsHelperAssign);
21032114
testForHelper('b', '__spread$2', KnownDeclaration.TsHelperSpread);
21042115
testForHelper('c', '__spreadArrays$3', KnownDeclaration.TsHelperSpreadArrays);
2116+
testForHelper('d', '__spreadArray$3', KnownDeclaration.TsHelperSpreadArray);
21052117
});
21062118

21072119
it('should recognize imported TypeScript helpers', () => {
@@ -2114,6 +2126,7 @@ exports.MissingClass2 = MissingClass2;
21142126
var a = tslib_1.__assign({foo: 'bar'}, {baz: 'qux'});
21152127
var b = tslib_1.__spread(['foo', 'bar'], ['baz', 'qux']);
21162128
var c = tslib_1.__spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2129+
var d = tslib_1.__spreadArray(['foo', 'bar'], ['baz', 'qux']);
21172130
`,
21182131
},
21192132
{
@@ -2122,6 +2135,7 @@ exports.MissingClass2 = MissingClass2;
21222135
export declare function __assign(t: any, ...sources: any[]): any;
21232136
export declare function __spread(...args: any[][]): any[];
21242137
export declare function __spreadArrays(...args: any[][]): any[];
2138+
export declare function __spreadArray(to: any[], from: any[]): any[];
21252139
`,
21262140
},
21272141
];
@@ -2140,6 +2154,7 @@ exports.MissingClass2 = MissingClass2;
21402154
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign, 'tslib');
21412155
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread, 'tslib');
21422156
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays, 'tslib');
2157+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray, 'tslib');
21432158
});
21442159

21452160
it('should recognize undeclared, unimported TypeScript helpers (by name)', () => {
@@ -2149,6 +2164,7 @@ exports.MissingClass2 = MissingClass2;
21492164
var a = __assign({foo: 'bar'}, {baz: 'qux'});
21502165
var b = __spread(['foo', 'bar'], ['baz', 'qux']);
21512166
var c = __spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2167+
var d = __spreadArray(['foo', 'bar'], ['baz', 'qux']);
21522168
`,
21532169
};
21542170
loadTestFiles([file]);
@@ -2174,6 +2190,7 @@ exports.MissingClass2 = MissingClass2;
21742190
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign);
21752191
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread);
21762192
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays);
2193+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray);
21772194
});
21782195

21792196
it('should recognize suffixed, undeclared, unimported TypeScript helpers (by name)', () => {
@@ -2183,6 +2200,7 @@ exports.MissingClass2 = MissingClass2;
21832200
var a = __assign$1({foo: 'bar'}, {baz: 'qux'});
21842201
var b = __spread$2(['foo', 'bar'], ['baz', 'qux']);
21852202
var c = __spreadArrays$3(['foo', 'bar'], ['baz', 'qux']);
2203+
var d = __spreadArray$3(['foo', 'bar'], ['baz', 'qux']);
21862204
`,
21872205
};
21882206
loadTestFiles([file]);
@@ -2208,6 +2226,7 @@ exports.MissingClass2 = MissingClass2;
22082226
testForHelper('a', '__assign$1', KnownDeclaration.TsHelperAssign);
22092227
testForHelper('b', '__spread$2', KnownDeclaration.TsHelperSpread);
22102228
testForHelper('c', '__spreadArrays$3', KnownDeclaration.TsHelperSpreadArrays);
2229+
testForHelper('d', '__spreadArray$3', KnownDeclaration.TsHelperSpreadArray);
22112230
});
22122231

22132232
it('should recognize enum declarations with string values', () => {
@@ -2441,6 +2460,7 @@ exports.MissingClass2 = MissingClass2;
24412460
export declare function __assign(t: any, ...sources: any[]): any;
24422461
export declare function __spread(...args: any[][]): any[];
24432462
export declare function __spreadArrays(...args: any[][]): any[];
2463+
export declare function __spreadArray(to: any[], from: any[]): any[];
24442464
export declare function __unknownHelper(...args: any[]): any;
24452465
`,
24462466
};
@@ -2456,6 +2476,7 @@ exports.MissingClass2 = MissingClass2;
24562476
['__assign', KnownDeclaration.TsHelperAssign],
24572477
['__spread', KnownDeclaration.TsHelperSpread],
24582478
['__spreadArrays', KnownDeclaration.TsHelperSpreadArrays],
2479+
['__spreadArray', KnownDeclaration.TsHelperSpreadArray],
24592480
['__unknownHelper', null],
24602481
]);
24612482
});

packages/compiler-cli/ngcc/test/host/esm5_host_spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,10 +2059,12 @@ runInEachFileSystem(() => {
20592059
function __assign(t, ...sources) { /* ... */ }
20602060
function __spread(...args) { /* ... */ }
20612061
function __spreadArrays(...args) { /* ... */ }
2062+
function __spreadArray(to, from) { /* ... */ }
20622063
20632064
var a = __assign({foo: 'bar'}, {baz: 'qux'});
20642065
var b = __spread(['foo', 'bar'], ['baz', 'qux']);
20652066
var c = __spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2067+
var d = __spreadArray(['foo', 'bar'], ['baz', 'qux']);
20662068
`,
20672069
};
20682070
loadTestFiles([file]);
@@ -2077,6 +2079,7 @@ runInEachFileSystem(() => {
20772079
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign);
20782080
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread);
20792081
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays);
2082+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray);
20802083
});
20812084

20822085
it('should recognize suffixed TypeScript helpers (as function declarations)', () => {
@@ -2086,10 +2089,12 @@ runInEachFileSystem(() => {
20862089
function __assign$1(t, ...sources) { /* ... */ }
20872090
function __spread$2(...args) { /* ... */ }
20882091
function __spreadArrays$3(...args) { /* ... */ }
2092+
function __spreadArray$3(to, from) { /* ... */ }
20892093
20902094
var a = __assign$1({foo: 'bar'}, {baz: 'qux'});
20912095
var b = __spread$2(['foo', 'bar'], ['baz', 'qux']);
20922096
var c = __spreadArrays$3(['foo', 'bar'], ['baz', 'qux']);
2097+
var d = __spreadArray$3(['foo', 'bar'], ['baz', 'qux']);
20932098
`,
20942099
};
20952100
loadTestFiles([file]);
@@ -2104,6 +2109,7 @@ runInEachFileSystem(() => {
21042109
testForHelper('a', '__assign$1', KnownDeclaration.TsHelperAssign);
21052110
testForHelper('b', '__spread$2', KnownDeclaration.TsHelperSpread);
21062111
testForHelper('c', '__spreadArrays$3', KnownDeclaration.TsHelperSpreadArrays);
2112+
testForHelper('d', '__spreadArray$3', KnownDeclaration.TsHelperSpreadArray);
21072113
});
21082114

21092115
it('should recognize TypeScript helpers (as variable declarations)', () => {
@@ -2113,11 +2119,13 @@ runInEachFileSystem(() => {
21132119
var __assign = (this && this.__assign) || function (t, ...sources) { /* ... */ }
21142120
var __spread = (this && this.__spread) || function (...args) { /* ... */ }
21152121
var __spreadArrays = (this && this.__spreadArrays) || function (...args) { /* ... */ }
2122+
var __spreadArray = (this && this.__spreadArray) || function (to, from) { /* ... */ }
21162123
21172124
var a = __assign({foo: 'bar'}, {baz: 'qux'});
21182125
var b = __spread(['foo', 'bar'], ['baz', 'qux']);
21192126
var c = __spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2120-
`,
2127+
var d = __spreadArray(['foo', 'bar'], ['baz', 'qux']);
2128+
`,
21212129
};
21222130
loadTestFiles([file]);
21232131
const bundle = makeTestBundleProgram(file.name);
@@ -2131,6 +2139,7 @@ runInEachFileSystem(() => {
21312139
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign);
21322140
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread);
21332141
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays);
2142+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray);
21342143
});
21352144

21362145
it('should recognize suffixed TypeScript helpers (as variable declarations)', () => {
@@ -2140,10 +2149,12 @@ runInEachFileSystem(() => {
21402149
var __assign$1 = (this && this.__assign$1) || function (t, ...sources) { /* ... */ }
21412150
var __spread$2 = (this && this.__spread$2) || function (...args) { /* ... */ }
21422151
var __spreadArrays$3 = (this && this.__spreadArrays$3) || function (...args) { /* ... */ }
2152+
var __spreadArray$3 = (this && this.__spreadArray$3) || function (to, from) { /* ... */ }
21432153
21442154
var a = __assign$1({foo: 'bar'}, {baz: 'qux'});
21452155
var b = __spread$2(['foo', 'bar'], ['baz', 'qux']);
21462156
var c = __spreadArrays$3(['foo', 'bar'], ['baz', 'qux']);
2157+
var d = __spreadArray$3(['foo', 'bar'], ['baz', 'qux']);
21472158
`,
21482159
};
21492160
loadTestFiles([file]);
@@ -2158,18 +2169,20 @@ runInEachFileSystem(() => {
21582169
testForHelper('a', '__assign$1', KnownDeclaration.TsHelperAssign);
21592170
testForHelper('b', '__spread$2', KnownDeclaration.TsHelperSpread);
21602171
testForHelper('c', '__spreadArrays$3', KnownDeclaration.TsHelperSpreadArrays);
2172+
testForHelper('d', '__spreadArray$3', KnownDeclaration.TsHelperSpreadArray);
21612173
});
21622174

21632175
it('should recognize imported TypeScript helpers (named imports)', () => {
21642176
const files: TestFile[] = [
21652177
{
21662178
name: _('/test.js'),
21672179
contents: `
2168-
import {__assign, __spread, __spreadArrays} from 'tslib';
2180+
import {__assign, __spread, __spreadArrays, __spreadArray} from 'tslib';
21692181
21702182
var a = __assign({foo: 'bar'}, {baz: 'qux'});
21712183
var b = __spread(['foo', 'bar'], ['baz', 'qux']);
21722184
var c = __spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2185+
var d = __spreadArray(['foo', 'bar'], ['baz', 'qux']);
21732186
`,
21742187
},
21752188
{
@@ -2178,6 +2191,7 @@ runInEachFileSystem(() => {
21782191
export declare function __assign(t: any, ...sources: any[]): any;
21792192
export declare function __spread(...args: any[][]): any[];
21802193
export declare function __spreadArrays(...args: any[][]): any[];
2194+
export declare function __spreadArray(to: any[], from: any[]): any[];
21812195
`,
21822196
},
21832197
];
@@ -2195,6 +2209,7 @@ runInEachFileSystem(() => {
21952209
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign, 'tslib');
21962210
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread, 'tslib');
21972211
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays, 'tslib');
2212+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray, 'tslib');
21982213
});
21992214

22002215
it('should recognize imported TypeScript helpers (star import)', () => {
@@ -2207,6 +2222,7 @@ runInEachFileSystem(() => {
22072222
var a = tslib_1.__assign({foo: 'bar'}, {baz: 'qux'});
22082223
var b = tslib_1.__spread(['foo', 'bar'], ['baz', 'qux']);
22092224
var c = tslib_1.__spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2225+
var d = tslib_1.__spreadArray(['foo', 'bar'], ['baz', 'qux']);
22102226
`,
22112227
},
22122228
{
@@ -2215,6 +2231,7 @@ runInEachFileSystem(() => {
22152231
export declare function __assign(t: any, ...sources: any[]): any;
22162232
export declare function __spread(...args: any[][]): any[];
22172233
export declare function __spreadArrays(...args: any[][]): any[];
2234+
export declare function __spreadArray(to: any[], from: any[]): any[];
22182235
`,
22192236
},
22202237
];
@@ -2232,6 +2249,7 @@ runInEachFileSystem(() => {
22322249
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign, 'tslib');
22332250
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread, 'tslib');
22342251
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays, 'tslib');
2252+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray, 'tslib');
22352253
});
22362254

22372255
it('should recognize undeclared, unimported TypeScript helpers (by name)', () => {
@@ -2241,6 +2259,7 @@ runInEachFileSystem(() => {
22412259
var a = __assign({foo: 'bar'}, {baz: 'qux'});
22422260
var b = __spread(['foo', 'bar'], ['baz', 'qux']);
22432261
var c = __spreadArrays(['foo', 'bar'], ['baz', 'qux']);
2262+
var d = __spreadArray(['foo', 'bar'], ['baz', 'qux']);
22442263
`,
22452264
};
22462265
loadTestFiles([file]);
@@ -2263,6 +2282,7 @@ runInEachFileSystem(() => {
22632282
testForHelper('a', '__assign', KnownDeclaration.TsHelperAssign);
22642283
testForHelper('b', '__spread', KnownDeclaration.TsHelperSpread);
22652284
testForHelper('c', '__spreadArrays', KnownDeclaration.TsHelperSpreadArrays);
2285+
testForHelper('d', '__spreadArray', KnownDeclaration.TsHelperSpreadArray);
22662286
});
22672287

22682288
it('should recognize suffixed, undeclared, unimported TypeScript helpers (by name)', () => {
@@ -2272,6 +2292,7 @@ runInEachFileSystem(() => {
22722292
var a = __assign$1({foo: 'bar'}, {baz: 'qux'});
22732293
var b = __spread$2(['foo', 'bar'], ['baz', 'qux']);
22742294
var c = __spreadArrays$3(['foo', 'bar'], ['baz', 'qux']);
2295+
var d = __spreadArray$3(['foo', 'bar'], ['baz', 'qux']);
22752296
`,
22762297
};
22772298
loadTestFiles([file]);
@@ -2294,6 +2315,7 @@ runInEachFileSystem(() => {
22942315
testForHelper('a', '__assign$1', KnownDeclaration.TsHelperAssign);
22952316
testForHelper('b', '__spread$2', KnownDeclaration.TsHelperSpread);
22962317
testForHelper('c', '__spreadArrays$3', KnownDeclaration.TsHelperSpreadArrays);
2318+
testForHelper('d', '__spreadArray$3', KnownDeclaration.TsHelperSpreadArray);
22972319
});
22982320

22992321
it('should recognize enum declarations with string values', () => {
@@ -2456,6 +2478,7 @@ runInEachFileSystem(() => {
24562478
export declare function __assign(t: any, ...sources: any[]): any;
24572479
export declare function __spread(...args: any[][]): any[];
24582480
export declare function __spreadArrays(...args: any[][]): any[];
2481+
export declare function __spreadArray(to: any[], from: any[]): any[];
24592482
export declare function __unknownHelper(...args: any[]): any;
24602483
`,
24612484
};
@@ -2470,6 +2493,7 @@ runInEachFileSystem(() => {
24702493
['__assign', KnownDeclaration.TsHelperAssign],
24712494
['__spread', KnownDeclaration.TsHelperSpread],
24722495
['__spreadArrays', KnownDeclaration.TsHelperSpreadArrays],
2496+
['__spreadArray', KnownDeclaration.TsHelperSpreadArray],
24732497
['__unknownHelper', null],
24742498
]);
24752499
});

0 commit comments

Comments
 (0)