swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.26k stars 1.23k forks source link

lowering class causes `_this is not defined` error #9660

Closed unbyte closed 1 month ago

unbyte commented 1 month ago

Describe the bug

Note: This bug has already been fixed by #9496 , but it hasn't been reported yet. I'm creating this issue for people who might encounter this problem and search for it.

In short, please upgrade swc to version >= 1.7.23.

When in a subclass constructor, the super call is at the end, and there's a branch that directly returns before the super call, swc emits a _this symbol without any bindings.

Input code

class Test extends Another {
    constructor(a, flag) {
        if (flag) {
            return a()
        }
        super()
    }
}

Config

No response

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.22&code=H4sIAAAAAAAAA0XIwQ2AIAxA0TNM0SMkLuEOLtBgURNSTFsSE8PuwsnDP7yfCqrCRmpAjxHvCitXO0ng9S5VVpOWrErABXLBI87vrgzhlxOyJgwY4lAfabtJprrvH6xF1oFjAAAA&config=H4sIAAAAAAAAA12PSw6DMAxE95wCed1tNz1BNz2ElRoU5HxkBwmEuHsDJLRll3ljjydL07YwqIFHu%2BRnFhFFSU6dic4%2B4ZQJkHGoRmxMcKvuoJvVISvtaD0cSIJeuyAuu35krhilp7SH6b2kAIegVFMKoymReOQncSTRi%2Bust93829Kh7%2FkSkrEJLgqp%2FndsSk9w4T3uW%2BX3aY6b2tZc8IPCOWn1VWfPC98aR%2Fb6AXANGa1PAQAA

SWC Info output

No response

Expected behavior

_this is declared before use

Actual behavior

var Test = /*#__PURE__*/ function(Another1) {
    "use strict";
    _inherits(Test, Another1);
    var _super = _create_super(Test);
    function Test(a, flag) {
        _class_call_check(this, Test);
        if (flag) {
            return _possible_constructor_return(_this, a());
        }
        return _super.call(this);
    }
    return Test;
}(Another);

Version

1.7.22

Additional context

No response

swc-bot commented 4 days ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.