diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index a24e69e3005..b1dcbfbcd95 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1693,8 +1693,37 @@ module ts { } function isExistingName(location: Node, name: string) { - return !resolver.isUnknownIdentifier(location, name) || - (currentScopeNames && hasProperty(currentScopeNames, name)); + // check if resolver is aware of this name (if name was seen during the typecheck) + if (!resolver.isUnknownIdentifier(location, name)) { + return true; + } + + // check if name is present in generated names that were introduced by the emitter + if (currentScopeNames && hasProperty(currentScopeNames, name)) { + return true; + } + + // check generated names in outer scopes + // var x; + // function foo() { + // let x; // 1 + // function bar() { + // { + // let x; // 2 + // } + // console.log(x); // 3 + // } + //} + // here both x(1) and x(2) should be renamed and their names should be different + // so x in (3) will refer to x(1) + var frame = lastFrame; + while (frame) { + if (hasProperty(frame.names, name)) { + return true; + } + frame = frame.previous; + } + return false; } function initializeEmitterWithSourceMaps() { diff --git a/tests/baselines/reference/downlevelLetConst19.js b/tests/baselines/reference/downlevelLetConst19.js new file mode 100644 index 00000000000..adcc2e256a8 --- /dev/null +++ b/tests/baselines/reference/downlevelLetConst19.js @@ -0,0 +1,39 @@ +//// [downlevelLetConst19.ts] +'use strict' +declare function use(a: any); +var x; +function a() { + { + let x; + use(x); + + function b() { + { + let x; + use(x); + } + use(x); + } + } + use(x) +} +use(x) + +//// [downlevelLetConst19.js] +'use strict'; +var x; +function a() { + { + var _x; + use(_x); + function b() { + { + var _x_1; + use(_x_1); + } + use(_x); + } + } + use(x); +} +use(x); diff --git a/tests/baselines/reference/downlevelLetConst19.types b/tests/baselines/reference/downlevelLetConst19.types new file mode 100644 index 00000000000..492d10b59c9 --- /dev/null +++ b/tests/baselines/reference/downlevelLetConst19.types @@ -0,0 +1,47 @@ +=== tests/cases/compiler/downlevelLetConst19.ts === +'use strict' +declare function use(a: any); +>use : (a: any) => any +>a : any + +var x; +>x : any + +function a() { +>a : () => void + { + let x; +>x : any + + use(x); +>use(x) : any +>use : (a: any) => any +>x : any + + function b() { +>b : () => void + { + let x; +>x : any + + use(x); +>use(x) : any +>use : (a: any) => any +>x : any + } + use(x); +>use(x) : any +>use : (a: any) => any +>x : any + } + } + use(x) +>use(x) : any +>use : (a: any) => any +>x : any +} +use(x) +>use(x) : any +>use : (a: any) => any +>x : any + diff --git a/tests/cases/compiler/downlevelLetConst19.ts b/tests/cases/compiler/downlevelLetConst19.ts new file mode 100644 index 00000000000..e7aa48421f7 --- /dev/null +++ b/tests/cases/compiler/downlevelLetConst19.ts @@ -0,0 +1,19 @@ +'use strict' +declare function use(a: any); +var x; +function a() { + { + let x; + use(x); + + function b() { + { + let x; + use(x); + } + use(x); + } + } + use(x) +} +use(x) \ No newline at end of file