Skip to content

Emit function name for inner downlevel async functions #13522

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
kpreisser opened this issue Jan 16, 2017 · 4 comments
Closed

Emit function name for inner downlevel async functions #13522

kpreisser opened this issue Jan 16, 2017 · 4 comments
Labels
ES6 Relates to the ES6 Spec Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Milestone

Comments

@kpreisser
Copy link
Contributor

Hi, sorry if this is a duplicate.

TypeScript Version: 2.2.0-dev.20170116

Code
When writing an async function and compiling down to ES5:

async function foo() {
    let result = Math.random();
    if (result < 0.8)
        return "Recursion: " + await foo();
    else
        return "Result: " + result; 
}

TypeScript emits code like this:

function foo() {
    return __awaiter(this, void 0, void 0, function () {
        var result, _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    result = Math.random();
                    if (!(result < 0.8)) return [3 /*break*/, 2];
                    _a = "Recursion: ";
                    return [4 /*yield*/, foo()];
                case 1: return [2 /*return*/, _a + (_b.sent())];
                case 2: return [2 /*return*/, "Result: " + result];
            }
        });
    });
}

Notice that the generated wrapper function has the name foo, but the inner function passed to _generator (which executes the original code) does not have a name.

Would it be possible for TS to emit the function name also for this inner function? (Maybe this would need some helper variable so that the inner function can correctly reference the wrapper function, in this example _c:)

function foo() {
    return __awaiter(this, void 0, void 0, function () {
        var result, _a, _c = foo;
        return __generator(this, function foo(_b) {
            switch (_b.label) {
                case 0:
                    result = Math.random();
                    if (!(result < 0.8)) return [3 /*break*/, 2];
                    _a = "Recursion: ";
                    return [4 /*yield*/, _c()];
                case 1: return [2 /*return*/, _a + (_b.sent())];
                case 2: return [2 /*return*/, "Result: " + result];
            }
        });
    });
}

We use an embedded TypeScript Compiler in an application where the user can create and edit scripts, which are executed using Jurassic (ES5). When an error occurs, we use the stack trace and filter out frames whose line numbers cannot be mapped by the source map, or which originate from the Promise polyfill. This stacktrace is displayed to the user.

However, because the TS does not emit a name for the inner generator function, the stack looks like this for async functions:

    at anonymous (MyScript.js:7)
    at anonymous (MyScript.js:2)
    at MyScript.js:13

whereas if TS could emit the name, it would look like this:

    at bar (MyScript.js:7)
    at foo (MyScript.js:2)
    at MyScript.js:13

Would this be possible?
Thanks!

@mhegazy mhegazy added the Bug A bug in TypeScript label Apr 19, 2017
@mhegazy mhegazy added this to the TypeScript 2.4 milestone Apr 19, 2017
@mhegazy mhegazy added the ES6 Relates to the ES6 Spec label Apr 19, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Apr 19, 2017

Similar to #15088

@rbuckton
Copy link
Contributor

We may opt to emit the inner functions as foo_1 (or foo_awaiter_1) as we do for other emit where we need to give something a name. That could still help as far as identifying functions in call stacks.

@rbuckton
Copy link
Contributor

I'm redirecting the conversation from #34977 here.

@RyanCavanaugh, @DanielRosenwasser: Since #34977 is marked "Bug" and "Rescheduled" and has 4.2 for its milestone, do we want to consider fixing this for 4.2? Since this could increase file-size on async-function heavy source that downlevels to ES2015 or lower, would we want to put this behind a flag?

@RyanCavanaugh RyanCavanaugh added Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it and removed Bug A bug in TypeScript labels Feb 23, 2024
@RyanCavanaugh
Copy link
Member

This doesn't seem like an important scenario for modern JS development - folks can either target ES6 which is supported basically everywhere, or use an alternate transpiler with stricter adherence semantics if this is important for their scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ES6 Relates to the ES6 Spec Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

4 participants