diff --git a/.changeset/popular-fireants-hang.md b/.changeset/popular-fireants-hang.md new file mode 100644 index 000000000000..eab7d793eef5 --- /dev/null +++ b/.changeset/popular-fireants-hang.md @@ -0,0 +1,7 @@ +--- +swc_ecma_compat_es2015: patch +swc_ecma_transforms_compat: patch +swc_core: patch +--- + +fix(es/compat): Handle label block in constructor diff --git a/crates/swc/tests/exec/issues-9xxx/9527/.swcrc b/crates/swc/tests/exec/issues-9xxx/9527/.swcrc new file mode 100644 index 000000000000..2ac251cd12a7 --- /dev/null +++ b/crates/swc/tests/exec/issues-9xxx/9527/.swcrc @@ -0,0 +1,10 @@ +{ + "jsc": { + "parser": { + "syntax": "ecmascript" + }, + "externalHelpers": false, + "target": "es5" + }, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/exec/issues-9xxx/9527/1/exec.js b/crates/swc/tests/exec/issues-9xxx/9527/1/exec.js new file mode 100644 index 000000000000..ed6fca1b75b1 --- /dev/null +++ b/crates/swc/tests/exec/issues-9xxx/9527/1/exec.js @@ -0,0 +1,50 @@ +class Base { }; + +expect(() => new class extends Base { + constructor() { + x: { break x; super(); } + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + try { } catch { super(); } + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + try { throw 0; super(); } catch { } + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + true || super(); + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + ({}) ?? super(); + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + false && super(); + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + null?.(super()); + } +}).toThrow(); + + +expect(() => new class extends Base { + constructor() { + null?.[super()]; + } +}).toThrow(); \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/input/.swcrc b/crates/swc/tests/fixture/issues-9xxx/9527/input/.swcrc new file mode 100644 index 000000000000..2ac251cd12a7 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/input/.swcrc @@ -0,0 +1,10 @@ +{ + "jsc": { + "parser": { + "syntax": "ecmascript" + }, + "externalHelpers": false, + "target": "es5" + }, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/input/2.js b/crates/swc/tests/fixture/issues-9xxx/9527/input/2.js new file mode 100644 index 000000000000..343f42426b03 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/input/2.js @@ -0,0 +1,17 @@ +class Bar { } +class Foo extends Bar { + constructor() { + switch (0) { + case 0: + break; + default: + super(); + } + } +} + +try { + new Foo(); +} catch { + console.log("catched"); +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/input/exec.js b/crates/swc/tests/fixture/issues-9xxx/9527/input/exec.js new file mode 100644 index 000000000000..ed6fca1b75b1 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/input/exec.js @@ -0,0 +1,50 @@ +class Base { }; + +expect(() => new class extends Base { + constructor() { + x: { break x; super(); } + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + try { } catch { super(); } + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + try { throw 0; super(); } catch { } + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + true || super(); + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + ({}) ?? super(); + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + false && super(); + } +}).toThrow(); + +expect(() => new class extends Base { + constructor() { + null?.(super()); + } +}).toThrow(); + + +expect(() => new class extends Base { + constructor() { + null?.[super()]; + } +}).toThrow(); \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/input/index.js b/crates/swc/tests/fixture/issues-9xxx/9527/input/index.js new file mode 100644 index 000000000000..40864ceb7a8d --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/input/index.js @@ -0,0 +1,15 @@ +class Bar { } +class Foo extends Bar { + constructor() { + x: { + break x; + super(); + } + } +} + +try { + new Foo(); +} catch { + console.log("catched"); +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/input/test.js b/crates/swc/tests/fixture/issues-9xxx/9527/input/test.js new file mode 100644 index 000000000000..f92a5721e3b4 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/input/test.js @@ -0,0 +1,25 @@ +class Base { }; + +new class extends Base { + constructor() { + super() || null; + } +} + +new class extends Base { + constructor() { + super()?.(); + } +} + +new class extends Base { + constructor() { + super()?.[null]; + } +} + +new class extends Base { + constructor() { + 1 + super(); + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/output/2.js b/crates/swc/tests/fixture/issues-9xxx/9527/output/2.js new file mode 100644 index 000000000000..a041323736c5 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/output/2.js @@ -0,0 +1,29 @@ +var _assert_this_initialized = require("@swc/helpers/_/_assert_this_initialized"); +var _call_super = require("@swc/helpers/_/_call_super"); +var _class_call_check = require("@swc/helpers/_/_class_call_check"); +var _inherits = require("@swc/helpers/_/_inherits"); +var Bar = function Bar() { + "use strict"; + _class_call_check._(this, Bar); +}; +var Foo = /*#__PURE__*/ function(Bar) { + "use strict"; + _inherits._(Foo, Bar); + function Foo() { + _class_call_check._(this, Foo); + var _this; + switch(0){ + case 0: + break; + default: + _this = _call_super._(this, Foo); + } + return _assert_this_initialized._(_this); + } + return Foo; +}(Bar); +try { + new Foo(); +} catch (e) { + console.log("catched"); +} diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/output/exec.js b/crates/swc/tests/fixture/issues-9xxx/9527/output/exec.js new file mode 100644 index 000000000000..7a323996f378 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/output/exec.js @@ -0,0 +1,124 @@ +var _assert_this_initialized = require("@swc/helpers/_/_assert_this_initialized"); +var _call_super = require("@swc/helpers/_/_call_super"); +var _class_call_check = require("@swc/helpers/_/_class_call_check"); +var _inherits = require("@swc/helpers/_/_inherits"); +var Base = function Base() { + "use strict"; + _class_call_check._(this, Base); +}; +; +expect(function() { + return new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + x: { + break x; + _this = _call_super._(this, _class); + } + return _assert_this_initialized._(_this); + } + return _class; + }(Base)); +}).toThrow(); +expect(function() { + return new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + try {} catch (e) { + _this = _call_super._(this, _class); + } + return _assert_this_initialized._(_this); + } + return _class; + }(Base)); +}).toThrow(); +expect(function() { + return new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + try { + throw 0; + _this = _call_super._(this, _class); + } catch (e) {} + return _assert_this_initialized._(_this); + } + return _class; + }(Base)); +}).toThrow(); +expect(function() { + return new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + true || (_this = _call_super._(this, _class)); + return _assert_this_initialized._(_this); + } + return _class; + }(Base)); +}).toThrow(); +expect(function() { + return new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + var _ref; + (_ref = {}) !== null && _ref !== void 0 ? _ref : _this = _call_super._(this, _class); + return _assert_this_initialized._(_this); + } + return _class; + }(Base)); +}).toThrow(); +expect(function() { + return new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + false && (_this = _call_super._(this, _class)); + return _assert_this_initialized._(_this); + } + return _class; + }(Base)); +}).toThrow(); +expect(function() { + return new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + var _this1; + (_this1 = null) === null || _this1 === void 0 ? void 0 : _this1(_this = _call_super._(this, _class)); + return _assert_this_initialized._(_this); + } + return _class; + }(Base)); +}).toThrow(); +expect(function() { + return new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + var _this1; + (_this1 = null) === null || _this1 === void 0 ? void 0 : _this1[_this = _call_super._(this, _class)]; + return _assert_this_initialized._(_this); + } + return _class; + }(Base)); +}).toThrow(); diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/output/index.js b/crates/swc/tests/fixture/issues-9xxx/9527/output/index.js new file mode 100644 index 000000000000..811c0845cd78 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/output/index.js @@ -0,0 +1,27 @@ +var _assert_this_initialized = require("@swc/helpers/_/_assert_this_initialized"); +var _call_super = require("@swc/helpers/_/_call_super"); +var _class_call_check = require("@swc/helpers/_/_class_call_check"); +var _inherits = require("@swc/helpers/_/_inherits"); +var Bar = function Bar() { + "use strict"; + _class_call_check._(this, Bar); +}; +var Foo = /*#__PURE__*/ function(Bar) { + "use strict"; + _inherits._(Foo, Bar); + function Foo() { + _class_call_check._(this, Foo); + var _this; + x: { + break x; + _this = _call_super._(this, Foo); + } + return _assert_this_initialized._(_this); + } + return Foo; +}(Bar); +try { + new Foo(); +} catch (e) { + console.log("catched"); +} diff --git a/crates/swc/tests/fixture/issues-9xxx/9527/output/test.js b/crates/swc/tests/fixture/issues-9xxx/9527/output/test.js new file mode 100644 index 000000000000..ad860c3e6932 --- /dev/null +++ b/crates/swc/tests/fixture/issues-9xxx/9527/output/test.js @@ -0,0 +1,54 @@ +var _call_super = require("@swc/helpers/_/_call_super"); +var _class_call_check = require("@swc/helpers/_/_class_call_check"); +var _inherits = require("@swc/helpers/_/_inherits"); +var Base = function Base() { + "use strict"; + _class_call_check._(this, Base); +}; +; +new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + (_this = _call_super._(this, _class)) || null; + return _this; + } + return _class; +}(Base)); +new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + var _this1; + (_this1 = _this = _call_super._(this, _class)) === null || _this1 === void 0 ? void 0 : _this1(); + return _this; + } + return _class; +}(Base)); +new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + var _this1; + (_this1 = _this = _call_super._(this, _class)) === null || _this1 === void 0 ? void 0 : _this1[null]; + return _this; + } + return _class; +}(Base)); +new /*#__PURE__*/ (function(Base) { + "use strict"; + _inherits._(_class, Base); + function _class() { + _class_call_check._(this, _class); + var _this; + 1 + (_this = _call_super._(this, _class)); + return _this; + } + return _class; +}(Base)); diff --git a/crates/swc/tests/tsc-references/constructorFunctionTypeIsAssignableToBaseType2.1.normal.js b/crates/swc/tests/tsc-references/constructorFunctionTypeIsAssignableToBaseType2.1.normal.js index 3b46f9c7cb82..665d37e93647 100644 --- a/crates/swc/tests/tsc-references/constructorFunctionTypeIsAssignableToBaseType2.1.normal.js +++ b/crates/swc/tests/tsc-references/constructorFunctionTypeIsAssignableToBaseType2.1.normal.js @@ -29,7 +29,6 @@ var Derived2 = /*#__PURE__*/ function(Base) { x ]); return _possible_constructor_return(_this, 1); - return _this; } return Derived2; }(Base); diff --git a/crates/swc/tests/tsc-references/derivedClassConstructorWithoutSuperCall.1.normal.js b/crates/swc/tests/tsc-references/derivedClassConstructorWithoutSuperCall.1.normal.js index 8fbb83302db9..04124cf84b27 100644 --- a/crates/swc/tests/tsc-references/derivedClassConstructorWithoutSuperCall.1.normal.js +++ b/crates/swc/tests/tsc-references/derivedClassConstructorWithoutSuperCall.1.normal.js @@ -1,9 +1,9 @@ //// [derivedClassConstructorWithoutSuperCall.ts] // derived class constructors must contain a super call +import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized"; import { _ as _call_super } from "@swc/helpers/_/_call_super"; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; var Base = function Base() { "use strict"; _class_call_check(this, Base); @@ -13,7 +13,7 @@ var Derived = /*#__PURE__*/ function(Base) { _inherits(Derived, Base); function Derived() { _class_call_check(this, Derived); - return _possible_constructor_return(void 0); + return _assert_this_initialized(void 0); } return Derived; }(Base); @@ -31,7 +31,7 @@ var Derived2 = /*#__PURE__*/ function(Base2) { var r2 = function() { return _this1 = _call_super(_this, Derived2); }; // error for misplaced super call (nested function) - return _possible_constructor_return(_this1); + return _assert_this_initialized(_this1); } return Derived2; }(Base2); @@ -44,7 +44,7 @@ var Derived3 = /*#__PURE__*/ function(Base2) { super(); } // error ; - return _possible_constructor_return(void 0); + return _assert_this_initialized(void 0); } return Derived3; }(Base2); diff --git a/crates/swc/tests/tsc-references/derivedClassConstructorWithoutSuperCall.2.minified.js b/crates/swc/tests/tsc-references/derivedClassConstructorWithoutSuperCall.2.minified.js index 07cdd7bda98d..80183f879ed0 100644 --- a/crates/swc/tests/tsc-references/derivedClassConstructorWithoutSuperCall.2.minified.js +++ b/crates/swc/tests/tsc-references/derivedClassConstructorWithoutSuperCall.2.minified.js @@ -1,5 +1,5 @@ //// [derivedClassConstructorWithoutSuperCall.ts] +import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized"; import { _ as _call_super } from "@swc/helpers/_/_call_super"; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; diff --git a/crates/swc/tests/tsc-references/jsDeclarationsClasses.1.normal.js b/crates/swc/tests/tsc-references/jsDeclarationsClasses.1.normal.js index 54a209165fb2..0d1c02f1c2ad 100644 --- a/crates/swc/tests/tsc-references/jsDeclarationsClasses.1.normal.js +++ b/crates/swc/tests/tsc-references/jsDeclarationsClasses.1.normal.js @@ -4,7 +4,6 @@ import { _ as _call_super } from "@swc/helpers/_/_call_super"; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _create_class } from "@swc/helpers/_/_create_class"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; export var A = function A() { "use strict"; _class_call_check(this, A); @@ -155,7 +154,7 @@ export var M = /*#__PURE__*/ function(_superClass) { _class_call_check(this, M); var _this; _assert_this_initialized(_this).prop = 12; - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return M; }(null); diff --git a/crates/swc/tests/tsc-references/jsDeclarationsClasses.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsClasses.2.minified.js index 7d20b5adbeca..bebd4b782f06 100644 --- a/crates/swc/tests/tsc-references/jsDeclarationsClasses.2.minified.js +++ b/crates/swc/tests/tsc-references/jsDeclarationsClasses.2.minified.js @@ -4,7 +4,6 @@ import { _ as _call_super } from "@swc/helpers/_/_call_super"; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _create_class } from "@swc/helpers/_/_create_class"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; export var A = function A() { _class_call_check(this, A); }; @@ -101,7 +100,7 @@ export var L = /*#__PURE__*/ function(K) { export var M = /*#__PURE__*/ function(_superClass) { function M() { var _this; - return _class_call_check(this, M), _assert_this_initialized(_this).prop = 12, _possible_constructor_return(_this); + return _class_call_check(this, M), _assert_this_initialized(_this).prop = 12, _assert_this_initialized(_this); } return _inherits(M, null), M; }(0); diff --git a/crates/swc/tests/tsc-references/superCallBeforeThisAccessing5.1.normal.js b/crates/swc/tests/tsc-references/superCallBeforeThisAccessing5.1.normal.js index 16f747888413..8b4c20a6097d 100644 --- a/crates/swc/tests/tsc-references/superCallBeforeThisAccessing5.1.normal.js +++ b/crates/swc/tests/tsc-references/superCallBeforeThisAccessing5.1.normal.js @@ -2,7 +2,6 @@ import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized"; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; var D = /*#__PURE__*/ function(_superClass) { "use strict"; _inherits(D, _superClass); @@ -10,7 +9,7 @@ var D = /*#__PURE__*/ function(_superClass) { _class_call_check(this, D); var _this; _assert_this_initialized(_this)._t; // No error - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return D; }(null); diff --git a/crates/swc/tests/tsc-references/superCallBeforeThisAccessing5.2.minified.js b/crates/swc/tests/tsc-references/superCallBeforeThisAccessing5.2.minified.js index 757ef7bf333c..8a8da27b5358 100644 --- a/crates/swc/tests/tsc-references/superCallBeforeThisAccessing5.2.minified.js +++ b/crates/swc/tests/tsc-references/superCallBeforeThisAccessing5.2.minified.js @@ -2,4 +2,3 @@ import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized"; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; diff --git a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).1.normal.js b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).1.normal.js index c856325d6f18..ce351b7003e8 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).1.normal.js @@ -1,4 +1,5 @@ //// [usingDeclarations.1.ts] +import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized"; import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; import { _ as _await_async_generator } from "@swc/helpers/_/_await_async_generator"; import { _ as _call_super } from "@swc/helpers/_/_call_super"; @@ -6,7 +7,6 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _create_class } from "@swc/helpers/_/_create_class"; import { _ as _define_property } from "@swc/helpers/_/_define_property"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator"; import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; @@ -396,7 +396,7 @@ try { } finally{ _usingCtx.d(); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return C2; }(C1); @@ -415,7 +415,7 @@ try { } finally{ _usingCtx.d(); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return C3; }(C1); diff --git a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).2.minified.js b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).2.minified.js index 64390ce1dfd0..e5b18e63bbb0 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).2.minified.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.1(target=es5).2.minified.js @@ -1,4 +1,5 @@ //// [usingDeclarations.1.ts] +import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized"; import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator"; import { _ as _await_async_generator } from "@swc/helpers/_/_await_async_generator"; import { _ as _call_super } from "@swc/helpers/_/_call_super"; @@ -6,7 +7,6 @@ import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _create_class } from "@swc/helpers/_/_create_class"; import { _ as _define_property } from "@swc/helpers/_/_define_property"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; import { _ as _wrap_async_generator } from "@swc/helpers/_/_wrap_async_generator"; import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator"; import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; diff --git a/crates/swc/tests/tsc-references/usingDeclarations.11.1.normal.js b/crates/swc/tests/tsc-references/usingDeclarations.11.1.normal.js index b183e079f192..bde17922a3ad 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.11.1.normal.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.11.1.normal.js @@ -1,8 +1,8 @@ //// [usingDeclarations.11.ts] +import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized"; import { _ as _call_super } from "@swc/helpers/_/_call_super"; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; var A = function A() { "use strict"; @@ -23,7 +23,7 @@ var C1 = /*#__PURE__*/ function(A) { } finally{ _usingCtx.d(); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return C1; }(A); @@ -42,7 +42,7 @@ var C2 = /*#__PURE__*/ function(A) { } finally{ _usingCtx.d(); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return C2; }(A); @@ -61,7 +61,7 @@ var C3 = /*#__PURE__*/ function(A) { } finally{ _usingCtx.d(); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return C3; }(A); @@ -80,7 +80,7 @@ var C4 = /*#__PURE__*/ function(A) { } finally{ _usingCtx.d(); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return C4; }(A); @@ -99,7 +99,7 @@ var C5 = /*#__PURE__*/ function(A) { } finally{ _usingCtx.d(); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return C5; }(A); diff --git a/crates/swc/tests/tsc-references/usingDeclarations.11.2.minified.js b/crates/swc/tests/tsc-references/usingDeclarations.11.2.minified.js index 9f068c3518e5..f381e8ae83a7 100644 --- a/crates/swc/tests/tsc-references/usingDeclarations.11.2.minified.js +++ b/crates/swc/tests/tsc-references/usingDeclarations.11.2.minified.js @@ -1,6 +1,6 @@ //// [usingDeclarations.11.ts] +import { _ as _assert_this_initialized } from "@swc/helpers/_/_assert_this_initialized"; import { _ as _call_super } from "@swc/helpers/_/_call_super"; import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check"; import { _ as _inherits } from "@swc/helpers/_/_inherits"; -import { _ as _possible_constructor_return } from "@swc/helpers/_/_possible_constructor_return"; import { _ as _using_ctx } from "@swc/helpers/_/_using_ctx"; diff --git a/crates/swc_ecma_compat_es2015/src/classes/constructor.rs b/crates/swc_ecma_compat_es2015/src/classes/constructor.rs index 08edb050b535..bcb24920c536 100644 --- a/crates/swc_ecma_compat_es2015/src/classes/constructor.rs +++ b/crates/swc_ecma_compat_es2015/src/classes/constructor.rs @@ -58,6 +58,7 @@ pub(super) fn fold_constructor( let mut body = constructor.body.take().unwrap(); if let Some(class_super_name) = class_super_name { let is_last_super = (&*body.stmts).is_super_last_call(); + let is_last_return = body.stmts.last().map_or(false, Stmt::is_return_stmt); let mut constructor_folder = ConstructorFolder { class_key_init: vec![], @@ -134,7 +135,7 @@ pub(super) fn fold_constructor( stmts.push(var.into()); } - if !is_last_super { + if !is_last_super && !is_last_return { let this = if constructor_folder.super_found { Expr::Ident(constructor_folder.this.unwrap()) } else { @@ -142,7 +143,7 @@ pub(super) fn fold_constructor( .this .map_or_else(|| Expr::undefined(DUMMY_SP).as_arg(), |this| this.as_arg()); - helper_expr!(possible_constructor_return).as_call(DUMMY_SP, vec![this]) + helper_expr!(assert_this_initialized).as_call(DUMMY_SP, vec![this]) }; let return_this = ReturnStmt { @@ -303,6 +304,13 @@ impl VisitMut for ConstructorFolder { self.super_found = super_found; } + fn visit_mut_switch_stmt(&mut self, node: &mut SwitchStmt) { + node.discriminant.visit_mut_with(self); + let super_found = self.super_found; + node.cases.visit_mut_with(self); + self.super_found = super_found; + } + fn visit_mut_try_stmt(&mut self, node: &mut TryStmt) { let super_found = self.super_found; node.block.visit_mut_with(self); @@ -311,6 +319,16 @@ impl VisitMut for ConstructorFolder { node.finalizer.visit_mut_with(self); } + fn visit_mut_labeled_stmt(&mut self, node: &mut LabeledStmt) { + if node.body.is_block() { + let super_found = self.super_found; + node.body.visit_mut_with(self); + self.super_found = super_found; + } else { + node.body.visit_mut_with(self); + } + } + fn visit_mut_bin_expr(&mut self, node: &mut BinExpr) { match node.op { op!("&&") | op!("||") => { diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/constant_super_update.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/constant_super_update.js index 942b654dab15..b54aefc27eb3 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/constant_super_update.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/constant_super_update.js @@ -8,7 +8,7 @@ let A = /*#__PURE__*/ function(B) { _update(A.prototype, "bar", _this, true)._ += 123; _update(A.prototype, baz, _this, true)._--; _update(A.prototype, quz, _this, true)._ -= 456; - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return A; }(B); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/constructor_super_update.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/constructor_super_update.js index 345d53a4addf..2dcb3f76dc50 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/constructor_super_update.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/constructor_super_update.js @@ -8,7 +8,7 @@ let A = /*#__PURE__*/ function(B) { _update((_assert_this_initialized(_this), _get_prototype_of(A.prototype)), "bar", _this, true)._ += 123; _update((_assert_this_initialized(_this), _get_prototype_of(A.prototype)), baz, _this, true)._--; _update((_assert_this_initialized(_this), _get_prototype_of(A.prototype)), quz, _this, true)._ -= 456; - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return A; }(B); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/duplicate_ident.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/duplicate_ident.js index a8265b951611..3b7a94303ecb 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/duplicate_ident.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/duplicate_ident.js @@ -5,7 +5,7 @@ let Foo = /*#__PURE__*/ function(Bar1) { _class_call_check(this, Foo); var Foo1 = 123; console.log(Foo1); - return _possible_constructor_return(void 0); + return _assert_this_initialized(void 0); } return Foo; }(Bar); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/nested_this_in_key.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/nested_this_in_key.js index 097e2e1899b6..320513f3edd0 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/nested_this_in_key.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/nested_this_in_key.js @@ -22,7 +22,6 @@ let Outer = /*#__PURE__*/ function(B) { return this; } return _possible_constructor_return(_this, new Inner()); - return _possible_constructor_return(_this); } return Outer; }(B); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/no_class_call_super.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/no_class_call_super.js index 378daabea501..41dfe88cd998 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/no_class_call_super.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/no_class_call_super.js @@ -10,7 +10,7 @@ let A = /*#__PURE__*/ function(B) { track ]); else _this = _call_super(this, A); - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return A; }(B); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super.js index e556e2b92291..33a04ef3007a 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super.js @@ -3,7 +3,7 @@ var Foo = /*#__PURE__*/ function(Bar1) { _inherits(Foo, Bar1); function Foo() { _class_call_check(this, Foo); - return _possible_constructor_return(void 0); + return _assert_this_initialized(void 0); } return Foo; }(Bar); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_2.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_2.js index cfe2ab1bd26c..fee7ca36a3f8 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_2.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_2.js @@ -5,7 +5,7 @@ var Foo = /*#__PURE__*/ function(Bar1) { _class_call_check(this, Foo); var _this; if (eval("false")) _this = _call_super(this, Foo); - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return Foo; }(Bar); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_3.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_3.js index 82ba45e05cf1..1115ba7d7563 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_3.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_3.js @@ -6,7 +6,7 @@ var Foo = /*#__PURE__*/ function(Bar1) { var _this; var fn = ()=>_this = _call_super(this, Foo); fn(); - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return Foo; }(Bar); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_4.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_4.js index 56db03fcf4a3..f220528a63d3 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_4.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_must_call_super_4.js @@ -5,7 +5,7 @@ var Foo = /*#__PURE__*/ function(Bar1) { _class_call_check(this, Foo); var _this; var fn = ()=>_this = _call_super(this, Foo); - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return Foo; }(Bar); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_no_super_return_falsey.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_no_super_return_falsey.js index cadb4cb4f2ba..8cfec44470b0 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_no_super_return_falsey.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_no_super_return_falsey.js @@ -5,7 +5,6 @@ var Child = /*#__PURE__*/ function(Base1) { _class_call_check(this, Child); var _this; return _possible_constructor_return(_this, false); - return _possible_constructor_return(_this); } return Child; }(Base); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_no_super_return_object.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_no_super_return_object.js index a3db0929a71f..8f7cba137202 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_no_super_return_object.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_derived_constructor_no_super_return_object.js @@ -5,7 +5,6 @@ var Child = /*#__PURE__*/ function(Base1) { _class_call_check(this, Child); var _this; return _possible_constructor_return(_this, {}); - return _possible_constructor_return(_this); } return Child; }(Base); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_class_super_call_in_key.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_class_super_call_in_key.js index 851733c0b240..c5d1cdc8454a 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_class_super_call_in_key.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_class_super_call_in_key.js @@ -29,7 +29,6 @@ var Outer = /*#__PURE__*/ function(Hello) { return Inner; }(); return _possible_constructor_return(_this, new Inner()); - return _this; } return Outer; }(Hello); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_class_super_property_in_key.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_class_super_property_in_key.js index b47df8ae3ce9..682fc77a29a1 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_class_super_property_in_key.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_class_super_property_in_key.js @@ -35,7 +35,6 @@ var Outer = /*#__PURE__*/ function(Hello) { return Inner; }(); return _possible_constructor_return(_this, new Inner()); - return _this; } return Outer; }(Hello); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_object_super_call_in_key.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_object_super_call_in_key.js index 096cf324df85..d6c0cba7eacb 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_object_super_call_in_key.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_object_super_call_in_key.js @@ -19,7 +19,6 @@ var Outer = /*#__PURE__*/ function(Hello) { } }; return _possible_constructor_return(_this, Inner); - return _this; } return Outer; }(Hello); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_object_super_property_in_key.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_object_super_property_in_key.js index 736c05b3eaad..7d96b2bbdfcb 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_object_super_property_in_key.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_nested_object_super_property_in_key.js @@ -26,7 +26,6 @@ var Outer = /*#__PURE__*/ function(Hello) { } }; return _possible_constructor_return(_this, Inner); - return _this; } return Outer; }(Hello); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_this_not_allowed_before_super_in_derived_classes_5.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_this_not_allowed_before_super_in_derived_classes_5.js index 56591a4325d4..cddf49aadbe8 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_this_not_allowed_before_super_in_derived_classes_5.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2015_classes.rs/spec_this_not_allowed_before_super_in_derived_classes_5.js @@ -5,7 +5,7 @@ var Foo = /*#__PURE__*/ function(Bar1) { _class_call_check(this, Foo); var _this; Foo[_assert_this_initialized(_this)]; - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return Foo; }(Bar); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/nested_class_super_call_in_key.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/nested_class_super_call_in_key.js index 7cb1924626ee..6caac50102df 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/nested_class_super_call_in_key.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/nested_class_super_call_in_key.js @@ -19,7 +19,6 @@ var Outer = /*#__PURE__*/ function(Hello) { _define_property(this, _ref, "hello"); }; return _possible_constructor_return(_this, new Inner()); - return _this; } return Outer; }(Hello); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/nested_class_super_property_in_key.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/nested_class_super_property_in_key.js index ccfa5d922b07..c227b78f7efa 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/nested_class_super_property_in_key.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/nested_class_super_property_in_key.js @@ -26,7 +26,6 @@ var Outer = /*#__PURE__*/ function(Hello) { _define_property(this, _super_toString, 'hello'); }; return _possible_constructor_return(_this, new Inner()); - return _this; } return Outer; }(Hello); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/private_derived_multiple_supers.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/private_derived_multiple_supers.js index 99b96b6518f3..b177840d3832 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/private_derived_multiple_supers.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/private_derived_multiple_supers.js @@ -16,7 +16,7 @@ var Foo = /*#__PURE__*/ function(Bar1) { value: "foo" }); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return Foo; }(Bar); diff --git a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/public_derived_multiple_supers.js b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/public_derived_multiple_supers.js index 6fddb996f80b..c67aef63c839 100644 --- a/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/public_derived_multiple_supers.js +++ b/crates/swc_ecma_transforms_compat/tests/__swc_snapshots__/tests/es2022_class_properties.rs/public_derived_multiple_supers.js @@ -9,7 +9,7 @@ var Foo = /*#__PURE__*/ function(Bar1) { } else { _this = _call_super(this, Foo), _define_property(_this, "bar", "foo"); } - return _possible_constructor_return(_this); + return _assert_this_initialized(_this); } return Foo; }(Bar);