Skip to content

Commit c8dd04b

Browse files
committed
Merge pull request #3209 from Microsoft/merge3137
Merge PR #3137 into release-1.5
2 parents 79ab85e + b355412 commit c8dd04b

File tree

6 files changed

+20
-17
lines changed

6 files changed

+20
-17
lines changed

src/compiler/emitter.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
13661366
return true;
13671367
}
13681368

1369-
function emitListWithSpread(elements: Expression[], multiLine: boolean, trailingComma: boolean) {
1369+
function emitListWithSpread(elements: Expression[], alwaysCopy: boolean, multiLine: boolean, trailingComma: boolean) {
13701370
let pos = 0;
13711371
let group = 0;
13721372
let length = elements.length;
@@ -1383,6 +1383,9 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
13831383
e = (<SpreadElementExpression>e).expression;
13841384
emitParenthesizedIf(e, /*parenthesized*/ group === 0 && needsParenthesisForPropertyAccessOrInvocation(e));
13851385
pos++;
1386+
if (pos === length && group === 0 && alwaysCopy && e.kind !== SyntaxKind.ArrayLiteralExpression) {
1387+
write(".slice()");
1388+
}
13861389
}
13871390
else {
13881391
let i = pos;
@@ -1422,7 +1425,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
14221425
write("]");
14231426
}
14241427
else {
1425-
emitListWithSpread(elements, /*multiLine*/(node.flags & NodeFlags.MultiLine) !== 0,
1428+
emitListWithSpread(elements, /*alwaysCopy*/ true, /*multiLine*/(node.flags & NodeFlags.MultiLine) !== 0,
14261429
/*trailingComma*/ elements.hasTrailingComma);
14271430
}
14281431
}
@@ -1847,7 +1850,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
18471850
write("void 0");
18481851
}
18491852
write(", ");
1850-
emitListWithSpread(node.arguments, /*multiLine*/ false, /*trailingComma*/ false);
1853+
emitListWithSpread(node.arguments, /*alwaysCopy*/ false, /*multiLine*/ false, /*trailingComma*/ false);
18511854
write(")");
18521855
}
18531856

tests/baselines/reference/arrayLiteralSpread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function f2() {
2626
//// [arrayLiteralSpread.js]
2727
function f0() {
2828
var a = [1, 2, 3];
29-
var a1 = a;
29+
var a1 = a.slice();
3030
var a2 = [1].concat(a);
3131
var a3 = [1, 2].concat(a);
3232
var a4 = a.concat([1]);

tests/baselines/reference/arrayLiterals2ES5.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ var temp2 = [[1, 2, 3], ["hello", "string"]];
9393
var temp3 = [undefined, null, undefined];
9494
var temp4 = [];
9595
var d0 = [1, true].concat(temp); // has type (string|number|boolean)[]
96-
var d1 = temp; // has type string[]
97-
var d2 = temp1;
98-
var d3 = temp1;
96+
var d1 = temp.slice(); // has type string[]
97+
var d2 = temp1.slice();
98+
var d3 = temp1.slice();
9999
var d4 = temp.concat(temp1);
100-
var d5 = temp3;
101-
var d6 = temp4;
102-
var d7 = temp1;
103-
var d8 = [temp1];
104-
var d9 = [temp1].concat(["hello"]);
100+
var d5 = temp3.slice();
101+
var d6 = temp4.slice();
102+
var d7 = temp1.slice();
103+
var d8 = [temp1.slice()];
104+
var d9 = [temp1.slice()].concat(["hello"]);

tests/baselines/reference/arrayLiterals3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ var _a = [1, 2, "string", true], b1 = _a[0], b2 = _a[1];
5555
var temp = ["s", "t", "r"];
5656
var temp1 = [1, 2, 3];
5757
var temp2 = [[1, 2, 3], ["hello", "string"]];
58-
var c0 = temp2; // Error
59-
var c1 = temp1; // Error cannot assign number[] to [number, number, number]
58+
var c0 = temp2.slice(); // Error
59+
var c1 = temp1.slice(); // Error cannot assign number[] to [number, number, number]
6060
var c2 = temp1.concat(temp); // Error cannot assign (number|string)[] to number[]

tests/baselines/reference/destructuringArrayBindingPatternAndAssignment1ES5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var _e = foo(), b6 = _e[0], b7 = _e[1];
8888
var b8 = foo().slice(0);
8989
// S is not a tuple- like type and the numeric index signature type of S is assignable to the target given in E.
9090
var temp = [1, 2, 3];
91-
var _f = temp, c0 = _f[0], c1 = _f[1];
91+
var _f = temp.slice(), c0 = _f[0], c1 = _f[1];
9292
var c2 = [][0];
9393
var _g = [[[]], [[[[]]]]], c3 = _g[0][0][0], c4 = _g[1][0][0][0][0];
9494
var _h = [[1], true], c5 = _h[0][0], c6 = _h[1];

tests/baselines/reference/destructuringArrayBindingPatternAndAssignment2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ var _c = bar(), _d = _c[0], b3 = _d === void 0 ? "string" : _d, b4 = _c[1], b5 =
5050
// V is an array assignment pattern, S is the type Any or an array-like type (section 3.3.2), and, for each assignment element E in V,
5151
// S is not a tuple- like type and the numeric index signature type of S is assignable to the target given in E.
5252
var temp = [1, 2, 3];
53-
var _e = temp, c0 = _e[0], c1 = _e[1]; // Error
54-
var _f = temp, c2 = _f[0], c3 = _f[1]; // Error
53+
var _e = temp.slice(), c0 = _e[0], c1 = _e[1]; // Error
54+
var _f = temp.slice(), c2 = _f[0], c3 = _f[1]; // Error
5555
function foo(idx) {
5656
return {
5757
2: true

0 commit comments

Comments
 (0)