Skip to content

Commit

Permalink
fix(es/compat): Handle label block in constructor (#9528)
Browse files Browse the repository at this point in the history
**Related issue:**
- Closes #9527
  • Loading branch information
magic-akari authored Sep 4, 2024
1 parent 8513816 commit c43dbad
Show file tree
Hide file tree
Showing 44 changed files with 468 additions and 46 deletions.
7 changes: 7 additions & 0 deletions .changeset/popular-fireants-hang.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions crates/swc/tests/exec/issues-9xxx/9527/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"externalHelpers": false,
"target": "es5"
},
"isModule": true
}
50 changes: 50 additions & 0 deletions crates/swc/tests/exec/issues-9xxx/9527/1/exec.js
Original file line number Diff line number Diff line change
@@ -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();
10 changes: 10 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9527/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript"
},
"externalHelpers": false,
"target": "es5"
},
"isModule": true
}
17 changes: 17 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9527/input/2.js
Original file line number Diff line number Diff line change
@@ -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");
}
50 changes: 50 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9527/input/exec.js
Original file line number Diff line number Diff line change
@@ -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();
15 changes: 15 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9527/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Bar { }
class Foo extends Bar {
constructor() {
x: {
break x;
super();
}
}
}

try {
new Foo();
} catch {
console.log("catched");
}
25 changes: 25 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9527/input/test.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
29 changes: 29 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9527/output/2.js
Original file line number Diff line number Diff line change
@@ -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");
}
124 changes: 124 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9527/output/exec.js
Original file line number Diff line number Diff line change
@@ -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();
27 changes: 27 additions & 0 deletions crates/swc/tests/fixture/issues-9xxx/9527/output/index.js
Original file line number Diff line number Diff line change
@@ -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");
}
Loading

0 comments on commit c43dbad

Please sign in to comment.