Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Invalid catch param identifier replacement #154

Closed
sebmck opened this issue Dec 15, 2014 · 2 comments
Closed

Invalid catch param identifier replacement #154

sebmck opened this issue Dec 15, 2014 · 2 comments
Labels

Comments

@sebmck
Copy link
Contributor

sebmck commented Dec 15, 2014

babel/babel#301

Following code incorrectly compiles because the property key identifier err is turned into context$1$0.t0:

Input:

function* foo() {
    try {
        yield someValue;
    } catch (err) {
        var obj = {err: err};
    }
}

Output:

var foo = regeneratorRuntime.mark(function foo() {
    var obj;

    return regeneratorRuntime.wrap(function foo$(context$1$0) {
        while (1) switch (context$1$0.prev = context$1$0.next) {
        case 0:
            context$1$0.prev = 0;
            context$1$0.next = 3;
            return someValue;
        case 3:
            context$1$0.next = 8;
            break;
        case 5:
            context$1$0.prev = 5;
            context$1$0.t0 = context$1$0["catch"](0);
            obj = {context$1$0.t0: context$1$0.t0};
        case 8:
        case "end":
            return context$1$0.stop();
        }
    }, foo, this, [[0, 5]]);
});
@sebmck
Copy link
Contributor Author

sebmck commented Dec 15, 2014

The current replacement implementation is pretty naive. It doesn't take into account MemberExpression properties etc.

This:

function* foo() {
    try {
        yield someValue;
    } catch (err) {
        test.err
    }
}

Gets turned into:

var foo = regeneratorRuntime.mark(function foo() {
    return regeneratorRuntime.wrap(function foo$(context$1$0) {
        while (1) switch (context$1$0.prev = context$1$0.next) {
        case 0:
            context$1$0.prev = 0;
            context$1$0.next = 3;
            return someValue;
        case 3:
            context$1$0.next = 8;
            break;
        case 5:
            context$1$0.prev = 5;
            context$1$0.t4 = context$1$0["catch"](0);
            test.context$1$0.t4;
        case 8:
        case "end":
            return context$1$0.stop();
        }
    }, foo, this, [[0, 5]]);
});

@benjamn benjamn added the bug label Dec 15, 2014
@benjamn
Copy link
Contributor

benjamn commented Dec 15, 2014

Definitely a bug. We probably need something like isReference to do this right: https://github.com/eventualbuddha/ast-util/blob/9bf91c5ce8/lib/index.js#L454-L507

@sebmck sebmck changed the title Invalid catch param property identifier replacement Invalid catch param identifier replacement Dec 15, 2014
benjamn added a commit that referenced this issue Dec 20, 2014
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants