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

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
Collaborator

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants