Skip to content

Commit 29107e6

Browse files
authored
Fix 9363: Object destructuring broken-variables are bound to the wrong object (#9383)
* Fix emit incorrect destructuring mapping in var declaration * Add tests and baselines * Add additional tests and baselines
1 parent 50177b1 commit 29107e6

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

src/compiler/emitter.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4545,8 +4545,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
45454545
}
45464546

45474547
write(";");
4548-
tempIndex++;
45494548
}
4549+
// Regardless of whether we will emit a var declaration for the binding pattern, we generate the temporary
4550+
// variable for the parameter (see: emitParameter)
4551+
tempIndex++;
45504552
}
45514553
else if (initializer) {
45524554
writeLine();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [destructuringParameterDeclaration7ES5.ts]
2+
3+
interface ISomething {
4+
foo: string,
5+
bar: string
6+
}
7+
8+
function foo({}, {foo, bar}: ISomething) {}
9+
10+
function baz([], {foo, bar}: ISomething) {}
11+
12+
function one([], {}) {}
13+
14+
function two([], [a, b, c]: number[]) {}
15+
16+
17+
//// [destructuringParameterDeclaration7ES5.js]
18+
function foo(_a, _b) {
19+
var foo = _b.foo, bar = _b.bar;
20+
}
21+
function baz(_a, _b) {
22+
var foo = _b.foo, bar = _b.bar;
23+
}
24+
function one(_a, _b) { }
25+
function two(_a, _b) {
26+
var a = _b[0], b = _b[1], c = _b[2];
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration7ES5.ts ===
2+
3+
interface ISomething {
4+
>ISomething : Symbol(ISomething, Decl(destructuringParameterDeclaration7ES5.ts, 0, 0))
5+
6+
foo: string,
7+
>foo : Symbol(ISomething.foo, Decl(destructuringParameterDeclaration7ES5.ts, 1, 22))
8+
9+
bar: string
10+
>bar : Symbol(ISomething.bar, Decl(destructuringParameterDeclaration7ES5.ts, 2, 16))
11+
}
12+
13+
function foo({}, {foo, bar}: ISomething) {}
14+
>foo : Symbol(foo, Decl(destructuringParameterDeclaration7ES5.ts, 4, 1))
15+
>foo : Symbol(foo, Decl(destructuringParameterDeclaration7ES5.ts, 6, 18))
16+
>bar : Symbol(bar, Decl(destructuringParameterDeclaration7ES5.ts, 6, 22))
17+
>ISomething : Symbol(ISomething, Decl(destructuringParameterDeclaration7ES5.ts, 0, 0))
18+
19+
function baz([], {foo, bar}: ISomething) {}
20+
>baz : Symbol(baz, Decl(destructuringParameterDeclaration7ES5.ts, 6, 43))
21+
>foo : Symbol(foo, Decl(destructuringParameterDeclaration7ES5.ts, 8, 18))
22+
>bar : Symbol(bar, Decl(destructuringParameterDeclaration7ES5.ts, 8, 22))
23+
>ISomething : Symbol(ISomething, Decl(destructuringParameterDeclaration7ES5.ts, 0, 0))
24+
25+
function one([], {}) {}
26+
>one : Symbol(one, Decl(destructuringParameterDeclaration7ES5.ts, 8, 43))
27+
28+
function two([], [a, b, c]: number[]) {}
29+
>two : Symbol(two, Decl(destructuringParameterDeclaration7ES5.ts, 10, 23))
30+
>a : Symbol(a, Decl(destructuringParameterDeclaration7ES5.ts, 12, 18))
31+
>b : Symbol(b, Decl(destructuringParameterDeclaration7ES5.ts, 12, 20))
32+
>c : Symbol(c, Decl(destructuringParameterDeclaration7ES5.ts, 12, 23))
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration7ES5.ts ===
2+
3+
interface ISomething {
4+
>ISomething : ISomething
5+
6+
foo: string,
7+
>foo : string
8+
9+
bar: string
10+
>bar : string
11+
}
12+
13+
function foo({}, {foo, bar}: ISomething) {}
14+
>foo : ({}: {}, {foo, bar}: ISomething) => void
15+
>foo : string
16+
>bar : string
17+
>ISomething : ISomething
18+
19+
function baz([], {foo, bar}: ISomething) {}
20+
>baz : ([]: any[], {foo, bar}: ISomething) => void
21+
>foo : string
22+
>bar : string
23+
>ISomething : ISomething
24+
25+
function one([], {}) {}
26+
>one : ([]: any[], {}: {}) => void
27+
28+
function two([], [a, b, c]: number[]) {}
29+
>two : ([]: any[], [a, b, c]: number[]) => void
30+
>a : number
31+
>b : number
32+
>c : number
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @target: es5
2+
3+
interface ISomething {
4+
foo: string,
5+
bar: string
6+
}
7+
8+
function foo({}, {foo, bar}: ISomething) {}
9+
10+
function baz([], {foo, bar}: ISomething) {}
11+
12+
function one([], {}) {}
13+
14+
function two([], [a, b, c]: number[]) {}

0 commit comments

Comments
 (0)