From 29107e636b39ceb12abe5eb061891a6092ab5918 Mon Sep 17 00:00:00 2001 From: Yui Date: Tue, 28 Jun 2016 13:33:11 -0700 Subject: [PATCH] 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 --- src/compiler/emitter.ts | 4 ++- .../destructuringParameterDeclaration7ES5.js | 27 +++++++++++++++ ...tructuringParameterDeclaration7ES5.symbols | 33 +++++++++++++++++++ ...estructuringParameterDeclaration7ES5.types | 33 +++++++++++++++++++ .../destructuringParameterDeclaration7ES5.ts | 14 ++++++++ 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/destructuringParameterDeclaration7ES5.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration7ES5.symbols create mode 100644 tests/baselines/reference/destructuringParameterDeclaration7ES5.types create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration7ES5.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 0879a3a9ee426..30f0e74adb434 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4545,8 +4545,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge } write(";"); - tempIndex++; } + // Regardless of whether we will emit a var declaration for the binding pattern, we generate the temporary + // variable for the parameter (see: emitParameter) + tempIndex++; } else if (initializer) { writeLine(); diff --git a/tests/baselines/reference/destructuringParameterDeclaration7ES5.js b/tests/baselines/reference/destructuringParameterDeclaration7ES5.js new file mode 100644 index 0000000000000..f9dac1ae2fabe --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration7ES5.js @@ -0,0 +1,27 @@ +//// [destructuringParameterDeclaration7ES5.ts] + +interface ISomething { + foo: string, + bar: string +} + +function foo({}, {foo, bar}: ISomething) {} + +function baz([], {foo, bar}: ISomething) {} + +function one([], {}) {} + +function two([], [a, b, c]: number[]) {} + + +//// [destructuringParameterDeclaration7ES5.js] +function foo(_a, _b) { + var foo = _b.foo, bar = _b.bar; +} +function baz(_a, _b) { + var foo = _b.foo, bar = _b.bar; +} +function one(_a, _b) { } +function two(_a, _b) { + var a = _b[0], b = _b[1], c = _b[2]; +} diff --git a/tests/baselines/reference/destructuringParameterDeclaration7ES5.symbols b/tests/baselines/reference/destructuringParameterDeclaration7ES5.symbols new file mode 100644 index 0000000000000..44709f18e1b2b --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration7ES5.symbols @@ -0,0 +1,33 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration7ES5.ts === + +interface ISomething { +>ISomething : Symbol(ISomething, Decl(destructuringParameterDeclaration7ES5.ts, 0, 0)) + + foo: string, +>foo : Symbol(ISomething.foo, Decl(destructuringParameterDeclaration7ES5.ts, 1, 22)) + + bar: string +>bar : Symbol(ISomething.bar, Decl(destructuringParameterDeclaration7ES5.ts, 2, 16)) +} + +function foo({}, {foo, bar}: ISomething) {} +>foo : Symbol(foo, Decl(destructuringParameterDeclaration7ES5.ts, 4, 1)) +>foo : Symbol(foo, Decl(destructuringParameterDeclaration7ES5.ts, 6, 18)) +>bar : Symbol(bar, Decl(destructuringParameterDeclaration7ES5.ts, 6, 22)) +>ISomething : Symbol(ISomething, Decl(destructuringParameterDeclaration7ES5.ts, 0, 0)) + +function baz([], {foo, bar}: ISomething) {} +>baz : Symbol(baz, Decl(destructuringParameterDeclaration7ES5.ts, 6, 43)) +>foo : Symbol(foo, Decl(destructuringParameterDeclaration7ES5.ts, 8, 18)) +>bar : Symbol(bar, Decl(destructuringParameterDeclaration7ES5.ts, 8, 22)) +>ISomething : Symbol(ISomething, Decl(destructuringParameterDeclaration7ES5.ts, 0, 0)) + +function one([], {}) {} +>one : Symbol(one, Decl(destructuringParameterDeclaration7ES5.ts, 8, 43)) + +function two([], [a, b, c]: number[]) {} +>two : Symbol(two, Decl(destructuringParameterDeclaration7ES5.ts, 10, 23)) +>a : Symbol(a, Decl(destructuringParameterDeclaration7ES5.ts, 12, 18)) +>b : Symbol(b, Decl(destructuringParameterDeclaration7ES5.ts, 12, 20)) +>c : Symbol(c, Decl(destructuringParameterDeclaration7ES5.ts, 12, 23)) + diff --git a/tests/baselines/reference/destructuringParameterDeclaration7ES5.types b/tests/baselines/reference/destructuringParameterDeclaration7ES5.types new file mode 100644 index 0000000000000..7d64383b0b668 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration7ES5.types @@ -0,0 +1,33 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration7ES5.ts === + +interface ISomething { +>ISomething : ISomething + + foo: string, +>foo : string + + bar: string +>bar : string +} + +function foo({}, {foo, bar}: ISomething) {} +>foo : ({}: {}, {foo, bar}: ISomething) => void +>foo : string +>bar : string +>ISomething : ISomething + +function baz([], {foo, bar}: ISomething) {} +>baz : ([]: any[], {foo, bar}: ISomething) => void +>foo : string +>bar : string +>ISomething : ISomething + +function one([], {}) {} +>one : ([]: any[], {}: {}) => void + +function two([], [a, b, c]: number[]) {} +>two : ([]: any[], [a, b, c]: number[]) => void +>a : number +>b : number +>c : number + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration7ES5.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration7ES5.ts new file mode 100644 index 0000000000000..97822e92e2dba --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration7ES5.ts @@ -0,0 +1,14 @@ +// @target: es5 + +interface ISomething { + foo: string, + bar: string +} + +function foo({}, {foo, bar}: ISomething) {} + +function baz([], {foo, bar}: ISomething) {} + +function one([], {}) {} + +function two([], [a, b, c]: number[]) {}