Skip to content
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

unexpected code generated with class expression #4630

Closed
vvakame opened this issue Sep 3, 2015 · 5 comments
Closed

unexpected code generated with class expression #4630

vvakame opened this issue Sep 3, 2015 · 5 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@vvakame
Copy link
Contributor

vvakame commented Sep 3, 2015

tested on 690b87c

source code.

new class {
    hi() {
        return "Hi!";
    }
}().hi();

expected.

new ((function () {
    function class_1() {
    }
    class_1.prototype.hi = function () {
        return "Hi!";
    };
    return class_1;
})())().hi();

actual.

new (function () {
    function class_1() {
    }
    class_1.prototype.hi = function () {
        return "Hi!";
    };
    return class_1;
})()().hi();
$ tsc --target es5 test.ts
$ node test.js
/private/tmp/tsc-1.6/test.js:8
})()().hi();
      ^

TypeError: Cannot read property 'hi' of undefined
    at Object.<anonymous> (/private/tmp/tsc-1.6/test.js:8:7)
    at Module._compile (module.js:430:26)
    at Object.Module._extensions..js (module.js:448:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:471:10)
    at startup (node.js:117:18)
    at node.js:951:3
@mhegazy
Copy link
Contributor

mhegazy commented Sep 3, 2015

that is similar to function expressions (#4472). i will need to double check the ES6 grammar to be sure though.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 3, 2015

@DanielRosenwasser can you take a look.

@vladima
Copy link
Contributor

vladima commented Sep 3, 2015

absence of parens effectively make generated code looks like this:

var x = new (function () {
    function class_1() {
    }
    class_1.prototype.hi = function () {
        return "Hi!";
    };
    return class_1;
})()

x().hi();

@mhegazy mhegazy added the Bug A bug in TypeScript label Sep 3, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Sep 3, 2015

my bad. we should parenthesize the output.

@DanielRosenwasser
Copy link
Member

Should be fixed for 1.8. Thanks for the heads up @vvakame

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

No branches or pull requests

4 participants