swc icon indicating copy to clipboard operation
swc copied to clipboard

assert_this_initialized check fired before super()

Open shredor opened this issue 1 year ago • 4 comments

Describe the bug

In some cases _assert_this_initialized fires before super() call which causes ReferenceError: this hasn't been initialised - super() hasn't been called

Input code

class Test0 {}

class Test extends Test0 {
  constructor() {
    super(),
      console.log(async (e) => {
        await this.test();
      });
  }
}

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "dynamicImport": true,
      "tsx": true,
      "jsx": true
    },
    "loose": false,
    "externalHelpers": true
  },
  "env": {
    "targets": "> 0.25%, not dead, Safari >=8",
    "mode": "entry",
    "coreJs": "3.27",
    "loose": false,
    "bugfixes": true
  }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.100&code=H4sIAAAAAAAAAz2MwQqDMBBE7%2FsVc0ygiHfRr%2BgPhHVphZAUd6UVyb9rozineTPDcAyqeIpai60Q8c2Qn0ka75IAzkltXtjy7HxNAF0%2BctCjwjnJUZqYXy7omhhOPPrhWv8VvmEy2HvSxo5r57urKtUVKrQDPuJyYpYAAAA%3D&config=H4sIAAAAAAAAA32PzQrCQAyE732KJeCtFFFEEexZvfoEcZuWlna3ZKO0lL67a%2B2PePCW%2BWYSJl2gFBROw1F1fvSiRnbEs%2FbEtUaw8QRIV%2Bg057VAOLlJa7DK9aWqLYsPCT9oNsU1v6iY0UD6jwGltY68kWLpxjBQI8QGyzOVNbFb1oYlIPNcagtyRvLOQKzW0Wa3CpWxohLCJFQ3TJFzFZ8OY3GobELDS0a4naC2TNfhxjba7OFPtfsjS%2FOGvjsF%2FQv6%2BI68TQEAAA%3D%3D

SWC Info output

No response

Expected behavior

var _this;
_this = _super.call(this);
var _this1 = _assert_this_initialized(_this);

Compiles well if i use super(); instead of super(),

Actual behavior

var _this;
var _this1 = _assert_this_initialized(_this);
_this = _super.call(this),

Version

1.3.101

Additional context

No response

shredor avatar Dec 22 '23 14:12 shredor

@shredor did you get solution for this ? I am not sure if my use case is related, for me when i use swc/jest the _callUpdate() method from the reactive-element inside lit-element is not at all getting triggered, resulting in failing tests across the codebase

gethari avatar Mar 11 '24 05:03 gethari

@gethari

class Test0 {}

class Test extends Test0 {
  constructor() {
    super();

    console.log(async (e) => {
      await this.test();
    });
  }
}

works

kdy1 avatar Mar 12 '24 01:03 kdy1

I tried fixing it, but I concluded that it's not important, as this is only about sequential expressions.

kdy1 avatar Mar 12 '24 01:03 kdy1

@gethari My use case was compiling js for legacy browsers:

  1. I have compiled js script for modern browsers
  2. I compile it again with swc for legacy browsers
  3. Use legacy js as a fallback when i detect legacy browser

My solution was to disable minification at step 1 so now it compiles well

shredor avatar Mar 12 '24 09:03 shredor