Skip to content

Emit function name for inner downlevel async functions #13522

@kpreisser

Description

@kpreisser

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    ES6Relates to the ES6 SpecWon't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions