Skip to content

Block scope support is broken #6462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
eekboom opened this issue Jan 13, 2016 · 1 comment · Fixed by #6553
Closed

Block scope support is broken #6462

eekboom opened this issue Jan 13, 2016 · 1 comment · Fixed by #6553
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@eekboom
Copy link

eekboom commented Jan 13, 2016

TypeScript 1.7.5 does not support the block scope for y here when emitting code for ES5:

function test() {
    let x = 42;
    {
        let x = 43;
        console.log(x);
    }

    {
        let y = 77;
        console.log(y);
    }
    {
        let y = 88;
        console.log(y);
    }
}

Compilation result:

function test() {
    var x = 42;
    {
        var x_1 = 43;
        console.log(x_1);
    }
    {
        var y = 77;
        console.log(y);
    }
    {
        var y = 88;
        console.log(y);
    }
}

At least one of the y's should also have been renamed.

(In my "real" code instead of "console.log()" there are local functions that use y and are registered as callbacks. The first function incorrectly uses the value from the second block.)

Related to #1690

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 13, 2016
@RyanCavanaugh
Copy link
Member

function test() {
    let f1, f2;
    {
        let y = 77;
        f1 = () => console.log(y);
    }
    {
        let y = 88;
        f2 = () => console.log(y);
    }
    f1(); // Should print 77, actual: 88
    f2(); // Should print 88, does
}

@mhegazy mhegazy added this to the TypeScript 1.8 milestone Jan 13, 2016
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 28, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants