From 8edd72ed73eeca2353348daf50cf0d9117c2c1ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 10 Nov 2021 16:15:03 +0900 Subject: [PATCH] fix(es/transforms/compat): Use `_createSuper` for super classes (#2699) swc_ecma_transforms_base: - Add `_createSuper` helper. - Add `_isNativeReflectConstruct` helper. swc_ecma_transforms_compat: - `classes`: Apply `_createSuper` to the super classes. --- .../fixture/issue-1490/case1/output/index.js | 28 +- .../fixture/issue-1490/full/output/index.js | 34 +- .../fixture/issue-1505/case1/output/index.ts | 28 +- .../fixture/issue-1505/case2/output/index.ts | 28 +- .../fixture/issue-1505/case3/output/index.tsx | 28 +- .../fixture/issue-1505/case4/output/index.tsx | 28 +- .../fixture/issue-1505/case5/output/index.tsx | 28 +- .../issue-1505/tsx-default/output/index.ts | 28 +- .../fixture/issue-846/case1/output/index.js | 28 +- .../new-target/output/class-extends-error.ts | 28 +- .../full/react-autowhatever/1/output/index.js | 83 ++- .../full/react-autowhatever/2/output/index.js | 79 ++- .../react-autowhatever/1/output/index.js | 28 +- .../base/src/helpers/_create_super.js | 14 + .../helpers/_is_native_reflect_construct.js | 11 + ecmascript/transforms/base/src/helpers/mod.rs | 8 + .../compat/src/es2015/classes/constructor.rs | 260 +++++++- .../compat/src/es2015/classes/mod.rs | 46 +- .../transforms/compat/src/es2015/mod.rs | 26 +- .../transforms/compat/tests/es2015_classes.rs | 581 ++++++++++-------- .../compat/tests/es2015_parameters.rs | 4 +- .../compat/tests/es2022_class_properties.rs | 89 +-- .../fixture/classes/create-super-1/exec.js | 9 + .../fixture/classes/create-super-1/input.js | 2 + .../fixture/classes/create-super-1/output.js | 10 + .../transforms/tests/es2015_function_name.rs | 4 +- 26 files changed, 1116 insertions(+), 424 deletions(-) create mode 100644 ecmascript/transforms/base/src/helpers/_create_super.js create mode 100644 ecmascript/transforms/base/src/helpers/_is_native_reflect_construct.js create mode 100644 ecmascript/transforms/compat/tests/fixture/classes/create-super-1/exec.js create mode 100644 ecmascript/transforms/compat/tests/fixture/classes/create-super-1/input.js create mode 100644 ecmascript/transforms/compat/tests/fixture/classes/create-super-1/output.js diff --git a/crates/swc/tests/fixture/issue-1490/case1/output/index.js b/crates/swc/tests/fixture/issue-1490/case1/output/index.js index 39f04fa7be0..30d8feb4d11 100644 --- a/crates/swc/tests/fixture/issue-1490/case1/output/index.js +++ b/crates/swc/tests/fixture/issue-1490/case1/output/index.js @@ -82,12 +82,38 @@ function _superPropBase(object, property) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var ColouredCanvasElement = /*#__PURE__*/ function(CanvasElement) { "use strict"; _inherits(ColouredCanvasElement, CanvasElement); + var _super = _createSuper(ColouredCanvasElement); function ColouredCanvasElement() { _classCallCheck(this, ColouredCanvasElement); - return _possibleConstructorReturn(this, _getPrototypeOf(ColouredCanvasElement).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(ColouredCanvasElement, [ { diff --git a/crates/swc/tests/fixture/issue-1490/full/output/index.js b/crates/swc/tests/fixture/issue-1490/full/output/index.js index 512210e7831..39780ffba4a 100644 --- a/crates/swc/tests/fixture/issue-1490/full/output/index.js +++ b/crates/swc/tests/fixture/issue-1490/full/output/index.js @@ -140,6 +140,31 @@ function _wrapNativeSuper(Class) { }; return _wrapNativeSuper(Class); } +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var Element = /*#__PURE__*/ function() { function Element() { _classCallCheck(this, Element); @@ -156,9 +181,10 @@ var Element = /*#__PURE__*/ function() { }(); var CanvasElement = /*#__PURE__*/ function(Element) { _inherits(CanvasElement, Element); + var _super = _createSuper(CanvasElement); function CanvasElement() { _classCallCheck(this, CanvasElement); - return _possibleConstructorReturn(this, _getPrototypeOf(CanvasElement).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(CanvasElement, [ { @@ -173,9 +199,10 @@ var CanvasElement = /*#__PURE__*/ function(Element) { }(_wrapNativeSuper(Element)); var ColouredCanvasElement = /*#__PURE__*/ function(CanvasElement) { _inherits(ColouredCanvasElement, CanvasElement); + var _super = _createSuper(ColouredCanvasElement); function ColouredCanvasElement() { _classCallCheck(this, ColouredCanvasElement); - return _possibleConstructorReturn(this, _getPrototypeOf(ColouredCanvasElement).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(ColouredCanvasElement, [ { @@ -190,9 +217,10 @@ var ColouredCanvasElement = /*#__PURE__*/ function(CanvasElement) { }(CanvasElement); var ColouredSquare = /*#__PURE__*/ function(ColouredCanvasElement) { _inherits(ColouredSquare, ColouredCanvasElement); + var _super = _createSuper(ColouredSquare); function ColouredSquare() { _classCallCheck(this, ColouredSquare); - return _possibleConstructorReturn(this, _getPrototypeOf(ColouredSquare).apply(this, arguments)); + return _super.apply(this, arguments); } return ColouredSquare; }(ColouredCanvasElement); diff --git a/crates/swc/tests/fixture/issue-1505/case1/output/index.ts b/crates/swc/tests/fixture/issue-1505/case1/output/index.ts index 2e63d962b13..2ac95832424 100644 --- a/crates/swc/tests/fixture/issue-1505/case1/output/index.ts +++ b/crates/swc/tests/fixture/issue-1505/case1/output/index.ts @@ -44,6 +44,31 @@ function _setPrototypeOf(o, p) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var MyClass = function MyClass() { "use strict"; _classCallCheck(this, MyClass); @@ -52,9 +77,10 @@ export var fn = function() { var _class = /*#__PURE__*/ function(MyClass) { "use strict"; _inherits(_class, MyClass); + var _super = _createSuper(_class); function _class() { _classCallCheck(this, _class); - return _possibleConstructorReturn(this, _getPrototypeOf(_class).apply(this, arguments)); + return _super.apply(this, arguments); } return _class; }(MyClass); diff --git a/crates/swc/tests/fixture/issue-1505/case2/output/index.ts b/crates/swc/tests/fixture/issue-1505/case2/output/index.ts index fff9b005c2d..3241620dd79 100644 --- a/crates/swc/tests/fixture/issue-1505/case2/output/index.ts +++ b/crates/swc/tests/fixture/issue-1505/case2/output/index.ts @@ -44,6 +44,31 @@ function _setPrototypeOf(o, p) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var MyClass = function MyClass() { "use strict"; _classCallCheck(this, MyClass); @@ -52,9 +77,10 @@ export var fn = function() { var _class = /*#__PURE__*/ function(MyClass) { "use strict"; _inherits(_class, MyClass); + var _super = _createSuper(_class); function _class() { _classCallCheck(this, _class); - return _possibleConstructorReturn(this, _getPrototypeOf(_class).apply(this, arguments)); + return _super.apply(this, arguments); } return _class; }(MyClass); diff --git a/crates/swc/tests/fixture/issue-1505/case3/output/index.tsx b/crates/swc/tests/fixture/issue-1505/case3/output/index.tsx index becdf8bb709..ab83bf1c11a 100644 --- a/crates/swc/tests/fixture/issue-1505/case3/output/index.tsx +++ b/crates/swc/tests/fixture/issue-1505/case3/output/index.tsx @@ -44,6 +44,31 @@ function _setPrototypeOf(o, p) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var Component = function Component() { "use strict"; _classCallCheck(this, Component); @@ -56,9 +81,10 @@ var withTeamsForUser = function(_WrappedComponent) { var _class = /*#__PURE__*/ function(Component) { "use strict"; _inherits(_class, Component); + var _super = _createSuper(_class); function _class() { _classCallCheck(this, _class); - return _possibleConstructorReturn(this, _getPrototypeOf(_class).apply(this, arguments)); + return _super.apply(this, arguments); } return _class; }(Component); diff --git a/crates/swc/tests/fixture/issue-1505/case4/output/index.tsx b/crates/swc/tests/fixture/issue-1505/case4/output/index.tsx index 220ce3a2a83..e282ed1f0f4 100644 --- a/crates/swc/tests/fixture/issue-1505/case4/output/index.tsx +++ b/crates/swc/tests/fixture/issue-1505/case4/output/index.tsx @@ -44,12 +44,38 @@ function _setPrototypeOf(o, p) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} export var a = /*#__PURE__*/ function(Component) { "use strict"; _inherits(_class, Component); + var _super = _createSuper(_class); function _class() { _classCallCheck(this, _class); - return _possibleConstructorReturn(this, _getPrototypeOf(_class).apply(this, arguments)); + return _super.apply(this, arguments); } return _class; }(Component); diff --git a/crates/swc/tests/fixture/issue-1505/case5/output/index.tsx b/crates/swc/tests/fixture/issue-1505/case5/output/index.tsx index f457c667914..d20b6ed77bc 100644 --- a/crates/swc/tests/fixture/issue-1505/case5/output/index.tsx +++ b/crates/swc/tests/fixture/issue-1505/case5/output/index.tsx @@ -44,13 +44,39 @@ function _setPrototypeOf(o, p) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var withTeamsForUser = function() { var _class = /*#__PURE__*/ function(Component) { "use strict"; _inherits(_class, Component); + var _super = _createSuper(_class); function _class() { _classCallCheck(this, _class); - return _possibleConstructorReturn(this, _getPrototypeOf(_class).apply(this, arguments)); + return _super.apply(this, arguments); } return _class; }(Component); diff --git a/crates/swc/tests/fixture/issue-1505/tsx-default/output/index.ts b/crates/swc/tests/fixture/issue-1505/tsx-default/output/index.ts index fff9b005c2d..3241620dd79 100644 --- a/crates/swc/tests/fixture/issue-1505/tsx-default/output/index.ts +++ b/crates/swc/tests/fixture/issue-1505/tsx-default/output/index.ts @@ -44,6 +44,31 @@ function _setPrototypeOf(o, p) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var MyClass = function MyClass() { "use strict"; _classCallCheck(this, MyClass); @@ -52,9 +77,10 @@ export var fn = function() { var _class = /*#__PURE__*/ function(MyClass) { "use strict"; _inherits(_class, MyClass); + var _super = _createSuper(_class); function _class() { _classCallCheck(this, _class); - return _possibleConstructorReturn(this, _getPrototypeOf(_class).apply(this, arguments)); + return _super.apply(this, arguments); } return _class; }(MyClass); diff --git a/crates/swc/tests/fixture/issue-846/case1/output/index.js b/crates/swc/tests/fixture/issue-846/case1/output/index.js index 32449a4cda8..d3366782542 100644 --- a/crates/swc/tests/fixture/issue-846/case1/output/index.js +++ b/crates/swc/tests/fixture/issue-846/case1/output/index.js @@ -429,6 +429,31 @@ function _toPropertyKey(arg) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var SomeClass1 = _decorate([], function(_initialize) { var SomeClass = function SomeClass() { "use strict"; @@ -454,10 +479,11 @@ var OtherClass1 = _decorate([], function(_initialize, _SomeClass1) { var OtherClass = /*#__PURE__*/ function(_SomeClass) { "use strict"; _inherits(OtherClass, _SomeClass); + var _super = _createSuper(OtherClass); function OtherClass() { _classCallCheck(this, OtherClass); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(OtherClass).apply(this, arguments)); + _this = _super.apply(this, arguments); _initialize(_assertThisInitialized(_this)); return _this; } diff --git a/crates/swc/tests/fixture/new-target/output/class-extends-error.ts b/crates/swc/tests/fixture/new-target/output/class-extends-error.ts index 0f91cbc9446..c01ef359d79 100644 --- a/crates/swc/tests/fixture/new-target/output/class-extends-error.ts +++ b/crates/swc/tests/fixture/new-target/output/class-extends-error.ts @@ -102,13 +102,39 @@ function _wrapNativeSuper(Class) { }; return _wrapNativeSuper(Class); } +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var CustomError = /*#__PURE__*/ function(Error) { "use strict"; _inherits(CustomError, Error); + var _super = _createSuper(CustomError); function CustomError(message) { _classCallCheck(this, CustomError); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(CustomError).call(this, message)); // 'Error' breaks prototype chain here + _this = _super.call(this, message); // 'Error' breaks prototype chain here Object.setPrototypeOf(_assertThisInitialized(_this), _this.constructor.prototype); // restore prototype chain return _this; } diff --git a/crates/swc/tests/vercel/full/react-autowhatever/1/output/index.js b/crates/swc/tests/vercel/full/react-autowhatever/1/output/index.js index 4384b782388..703df1579f5 100644 --- a/crates/swc/tests/vercel/full/react-autowhatever/1/output/index.js +++ b/crates/swc/tests/vercel/full/react-autowhatever/1/output/index.js @@ -2,20 +2,20 @@ Object.defineProperty(exports, "__esModule", { value: !0 }), exports.default = void 0; -var g, b = require("react/jsx-runtime"), c = function(g) { - if (g && g.__esModule) return g; +var a, b = require("react/jsx-runtime"), c = function(a) { + if (a && a.__esModule) return a; var b = { }; - if (null != g) { - for(var b in g)if (Object.prototype.hasOwnProperty.call(g, b)) { - var d = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(g, b) : { + if (null != a) { + for(var b in a)if (Object.prototype.hasOwnProperty.call(a, b)) { + var d = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(a, b) : { }; - d.get || d.set ? Object.defineProperty(b, b, d) : b[b] = g[b]; + d.get || d.set ? Object.defineProperty(b, b, d) : b[b] = a[b]; } } - return b.default = g, b; -}(require("react")), d = (g = require("prop-types")) && g.__esModule ? g : { - default: g + return b.default = a, b; +}(require("react")), d = (a = require("prop-types")) && a.__esModule ? a : { + default: a }; function e(a, b) { for(var c = 0; c < b.length; c++){ @@ -23,13 +23,13 @@ function e(a, b) { d.enumerable = d.enumerable || !1, d.configurable = !0, "value" in d && (d.writable = !0), Object.defineProperty(a, d.key, d); } } -function f(g, b, c) { - return b in g ? Object.defineProperty(g, b, { +function f(a, b, c) { + return b in a ? Object.defineProperty(a, b, { value: c, enumerable: !0, configurable: !0, writable: !0 - }) : g[b] = c, g; + }) : a[b] = c, a; } function b(d) { return b = Object.setPrototypeOf ? Object.getPrototypeOf : function b(d) { @@ -53,21 +53,9 @@ function c(d, e) { return d.__proto__ = e, d; }, c(d, e); } -var j = function(d) { +var l = function(d) { "use strict"; - var b, g, i; - function j() { - var c, a, e, g; - return !function(a, b) { - if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); - }(this, j), a = this, e = b(j).apply(this, arguments), (c = e && ("object" == ((g = e) && "undefined" != typeof Symbol && g.constructor === Symbol ? "symbol" : typeof g) || "function" == typeof e) ? e : (function(a) { - if (void 0 === a) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); - return a; - })(a)).storeHighlightedItemReference = function(a) { - c.props.onHighlightedItemChange(null === a ? null : a.item); - }, c; - } - return !function(a, b) { + !function(a, b) { if ("function" != typeof b && null !== b) throw new TypeError("Super expression must either be null or a function"); a.prototype = Object.create(b && b.prototype, { constructor: { @@ -76,7 +64,40 @@ var j = function(d) { configurable: !0 } }), b && c(a, b); - }(j, d), b = j, g = [ + }(l, d); + var b, g, i, k = function(c) { + var d = function() { + if ("undefined" == typeof Reflect || !Reflect.construct) return !1; + if (Reflect.construct.sham) return !1; + if ("function" == typeof Proxy) return !0; + try { + return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })), !0; + } catch (a) { + return !1; + } + }(); + return function() { + var a, a, f, g, h = b(c); + if (d) { + var i = b(this).constructor; + g = Reflect.construct(h, arguments, i); + } else g = h.apply(this, arguments); + return a = this, (f = g) && ("object" == ((a = f) && "undefined" != typeof Symbol && a.constructor === Symbol ? "symbol" : typeof a) || "function" == typeof f) ? f : (function(a) { + if (void 0 === a) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); + return a; + })(a); + }; + }(l); + function l() { + var a; + return !function(a, b) { + if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); + }(this, l), a = k.apply(this, arguments), a.storeHighlightedItemReference = function(b) { + a.props.onHighlightedItemChange(null === b ? null : b.item); + }, a; + } + return b = l, g = [ { key: "shouldComponentUpdate", value: function(a) { @@ -111,9 +132,9 @@ var j = function(d) { })); } } - ], e(b.prototype, g), i && e(b, i), j; + ], e(b.prototype, g), i && e(b, i), l; }(c.Component); -j.propTypes = { +l.propTypes = { items: d.default.array.isRequired, itemProps: d.default.oneOfType([ d.default.object, @@ -127,6 +148,6 @@ j.propTypes = { getItemId: d.default.func.isRequired, theme: d.default.func.isRequired, keyPrefix: d.default.string.isRequired -}, j.defaultProps = { +}, l.defaultProps = { sectionIndex: null -}, exports.default = j, new j(); +}, exports.default = l, new l(); diff --git a/crates/swc/tests/vercel/full/react-autowhatever/2/output/index.js b/crates/swc/tests/vercel/full/react-autowhatever/2/output/index.js index 45cef719b89..3a4c481d521 100644 --- a/crates/swc/tests/vercel/full/react-autowhatever/2/output/index.js +++ b/crates/swc/tests/vercel/full/react-autowhatever/2/output/index.js @@ -2,18 +2,18 @@ Object.defineProperty(exports, "__esModule", { value: !0 }), exports.default = void 0; -var a = require("react/jsx-runtime"), b = function(g) { - if (g && g.__esModule) return g; +var a = require("react/jsx-runtime"), b = function(a) { + if (a && a.__esModule) return a; var b = { }; - if (null != g) { - for(var b in g)if (Object.prototype.hasOwnProperty.call(g, b)) { - var d = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(g, b) : { + if (null != a) { + for(var b in a)if (Object.prototype.hasOwnProperty.call(a, b)) { + var d = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(a, b) : { }; - d.get || d.set ? Object.defineProperty(b, b, d) : b[b] = g[b]; + d.get || d.set ? Object.defineProperty(b, b, d) : b[b] = a[b]; } } - return b.default = g, b; + return b.default = a, b; }(require("react")); function c(a, b) { for(var c = 0; c < b.length; c++){ @@ -21,13 +21,13 @@ function c(a, b) { d.enumerable = d.enumerable || !1, d.configurable = !0, "value" in d && (d.writable = !0), Object.defineProperty(a, d.key, d); } } -function d(g, b, c) { - return b in g ? Object.defineProperty(g, b, { +function d(a, b, c) { + return b in a ? Object.defineProperty(a, b, { value: c, enumerable: !0, configurable: !0, writable: !0 - }) : g[b] = c, g; + }) : a[b] = c, a; } function b(d) { return b = Object.setPrototypeOf ? Object.getPrototypeOf : function b(d) { @@ -51,21 +51,9 @@ function c(d, e) { return d.__proto__ = e, d; }, c(d, e); } -var j = function(d) { +var k = function(d) { "use strict"; - var b, g, i; - function j() { - var c, a, f, g; - return !function(a, b) { - if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); - }(this, j), a = this, f = b(j).apply(this, arguments), (c = f && ("object" == ((g = f) && "undefined" != typeof Symbol && g.constructor === Symbol ? "symbol" : typeof g) || "function" == typeof f) ? f : (function(a) { - if (void 0 === a) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); - return a; - })(a)).storeHighlightedItemReference = function(a) { - c.props.onHighlightedItemChange(null === a ? null : a.item); - }, c; - } - return !function(a, b) { + !function(a, b) { if ("function" != typeof b && null !== b) throw new TypeError("Super expression must either be null or a function"); a.prototype = Object.create(b && b.prototype, { constructor: { @@ -74,7 +62,40 @@ var j = function(d) { configurable: !0 } }), b && c(a, b); - }(j, d), b = j, g = [ + }(k, d); + var b, g, i, j = function(c) { + var d = function() { + if ("undefined" == typeof Reflect || !Reflect.construct) return !1; + if (Reflect.construct.sham) return !1; + if ("function" == typeof Proxy) return !0; + try { + return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })), !0; + } catch (a) { + return !1; + } + }(); + return function() { + var a, a, f, g, h = b(c); + if (d) { + var i = b(this).constructor; + g = Reflect.construct(h, arguments, i); + } else g = h.apply(this, arguments); + return a = this, (f = g) && ("object" == ((a = f) && "undefined" != typeof Symbol && a.constructor === Symbol ? "symbol" : typeof a) || "function" == typeof f) ? f : (function(a) { + if (void 0 === a) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); + return a; + })(a); + }; + }(k); + function k() { + var a; + return !function(a, b) { + if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); + }(this, k), a = j.apply(this, arguments), a.storeHighlightedItemReference = function(b) { + a.props.onHighlightedItemChange(null === b ? null : b.item); + }, a; + } + return b = k, g = [ { key: "shouldComponentUpdate", value: function(a) { @@ -111,10 +132,10 @@ var j = function(d) { })); } } - ], c(b.prototype, g), i && c(b, i), j; + ], c(b.prototype, g), i && c(b, i), k; }(b.Component); -j.propTypes = { +k.propTypes = { items: 500 -}, j.defaultProps = { +}, k.defaultProps = { sectionIndex: null -}, exports.default = j, new j(); +}, exports.default = k, new k(); diff --git a/crates/swc/tests/vercel/loader-only/react-autowhatever/1/output/index.js b/crates/swc/tests/vercel/loader-only/react-autowhatever/1/output/index.js index 4f0c22d153f..c5dd5110a9d 100644 --- a/crates/swc/tests/vercel/loader-only/react-autowhatever/1/output/index.js +++ b/crates/swc/tests/vercel/loader-only/react-autowhatever/1/output/index.js @@ -92,13 +92,39 @@ function _setPrototypeOf(o, p) { var _typeof = function(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() { + })); + return true; + } catch (e) { + return false; + } +} +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} var ItemsList = /*#__PURE__*/ function(Component) { "use strict"; _inherits(ItemsList, Component); + var _super = _createSuper(ItemsList); function ItemsList() { _classCallCheck(this, ItemsList); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(ItemsList).apply(this, arguments)); + _this = _super.apply(this, arguments); _this.storeHighlightedItemReference = function(highlightedItem) { _this.props.onHighlightedItemChange(highlightedItem === null ? null : highlightedItem.item); }; diff --git a/ecmascript/transforms/base/src/helpers/_create_super.js b/ecmascript/transforms/base/src/helpers/_create_super.js new file mode 100644 index 00000000000..9a97d3071a6 --- /dev/null +++ b/ecmascript/transforms/base/src/helpers/_create_super.js @@ -0,0 +1,14 @@ +function _createSuper(Derived) { + var hasNativeReflectConstruct = _isNativeReflectConstruct(); + return function _createSuperInternal() { + var Super = _getPrototypeOf(Derived), + result; + if (hasNativeReflectConstruct) { + var NewTarget = _getPrototypeOf(this).constructor; + result = Reflect.construct(Super, arguments, NewTarget); + } else { + result = Super.apply(this, arguments); + } + return _possibleConstructorReturn(this, result); + }; +} \ No newline at end of file diff --git a/ecmascript/transforms/base/src/helpers/_is_native_reflect_construct.js b/ecmascript/transforms/base/src/helpers/_is_native_reflect_construct.js new file mode 100644 index 00000000000..b554344d00f --- /dev/null +++ b/ecmascript/transforms/base/src/helpers/_is_native_reflect_construct.js @@ -0,0 +1,11 @@ +function _isNativeReflectConstruct() { + if (typeof Reflect === "undefined" || !Reflect.construct) return false; + if (Reflect.construct.sham) return false; + if (typeof Proxy === "function") return true; + try { + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () { })); + return true; + } catch (e) { + return false; + } +} \ No newline at end of file diff --git a/ecmascript/transforms/base/src/helpers/mod.rs b/ecmascript/transforms/base/src/helpers/mod.rs index 59107ea1250..ab933897e4b 100644 --- a/ecmascript/transforms/base/src/helpers/mod.rs +++ b/ecmascript/transforms/base/src/helpers/mod.rs @@ -244,6 +244,14 @@ define_helpers!(Helpers { class_static_private_method_get: (class_check_private_static_access), class_check_private_static_access: (), + + is_native_reflect_construct: (), + + create_super: ( + get_prototype_of, + is_native_reflect_construct, + possible_constructor_return + ), }); pub fn inject_helpers() -> impl Fold + VisitMut { diff --git a/ecmascript/transforms/compat/src/es2015/classes/constructor.rs b/ecmascript/transforms/compat/src/es2015/classes/constructor.rs index 1439f0e60fd..b5f2976b655 100644 --- a/ecmascript/transforms/compat/src/es2015/classes/constructor.rs +++ b/ecmascript/transforms/compat/src/es2015/classes/constructor.rs @@ -4,7 +4,7 @@ use swc_common::{Mark, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::helper; use swc_ecma_transforms_classes::{fold_only_key, get_prototype_of}; -use swc_ecma_utils::{quote_ident, ExprFactory}; +use swc_ecma_utils::{private_ident, quote_ident, ExprFactory}; use swc_ecma_visit::{noop_fold_type, noop_visit_type, Fold, FoldWith, Node, Visit, VisitWith}; pub(super) struct SuperCallFinder { @@ -163,14 +163,152 @@ pub(super) fn constructor_fn(c: Constructor) -> Function { pub(super) struct ConstructorFolder<'a> { pub class_name: &'a Ident, pub mode: Option, + /// Variables named `thisSuper` will be added if `super.foo` or + /// `super.foo()` is used in constructor. + pub vars: &'a mut Vec, + + pub cur_this_super: Option, + /// Mark for `_this` pub mark: Mark, pub is_constructor_default: bool, + pub super_var: Option, /// True when recursing into other function or class. pub ignore_return: bool, pub in_injected_define_property_call: bool, } +impl ConstructorFolder<'_> { + /// `Ok()` means it's processed and children are folded. + fn handle_super_assignment(&mut self, e: Expr) -> Result { + match e { + Expr::Assign(AssignExpr { + span, + left, + op: op!("="), + right, + }) => { + let left = left.normalize_expr(); + match left { + PatOrExpr::Expr(left_expr) => match &*left_expr { + Expr::Member(MemberExpr { + obj: ExprOrSuper::Super(..), + .. + }) => { + let right = right.fold_children_with(self); + + return Ok(self.handle_super_access(*left_expr, Some(right))); + } + _ => Err(Expr::Assign(AssignExpr { + span, + left: PatOrExpr::Expr(left_expr), + op: op!("="), + right, + })), + }, + _ => Err(Expr::Assign(AssignExpr { + span, + left, + op: op!("="), + right, + })), + } + } + _ => Err(e), + } + } + + fn handle_super_access(&mut self, e: Expr, set_to: Option>) -> Expr { + match e { + Expr::Member(MemberExpr { + span, + obj: ExprOrSuper::Super(..), + prop, + computed, + }) => { + let this_var = quote_ident!(DUMMY_SP.apply_mark(self.mark), "_this"); + let this_super = private_ident!("_thisSuper"); + self.cur_this_super = Some(this_super.clone()); + + self.vars.push(VarDeclarator { + span: DUMMY_SP, + name: Pat::Ident(this_super.clone().into()), + init: None, + definite: Default::default(), + }); + + // _thisSuper = _assertThisInitialized(_this) + let init_this_super = Box::new(Expr::Assign(AssignExpr { + span: DUMMY_SP, + op: op!("="), + left: PatOrExpr::Pat(Box::new(this_super.clone().into())), + right: Box::new(Expr::Call(CallExpr { + span: DUMMY_SP, + callee: helper!(assert_this_initialized, "assertThisInitialized"), + args: vec![this_var.as_arg()], + type_args: Default::default(), + })), + })); + // _getPrototypeOf(Foo.prototype) + let get_proto = Box::new(Expr::Call(CallExpr { + span, + callee: helper!(get_prototype_of, "getPrototypeOf"), + args: vec![self + .class_name + .clone() + .make_member(quote_ident!("prototype")) + .as_arg()], + type_args: Default::default(), + })); + + Expr::Call(CallExpr { + span: DUMMY_SP, + callee: if set_to.is_some() { + helper!(set, "set") + } else { + helper!(get, "get") + }, + args: { + let mut args = vec![ + Expr::Seq(SeqExpr { + span: DUMMY_SP, + exprs: vec![init_this_super, get_proto], + }) + .as_arg(), + if computed { + prop.as_arg() + } else { + let prop = prop.expect_ident(); + + Str { + span: prop.span, + value: prop.sym, + has_escape: false, + kind: Default::default(), + } + .as_arg() + }, + ]; + + let is_set = set_to.is_some(); + args.extend(set_to.map(|v| v.as_arg())); + + args.push(this_super.clone().as_arg()); + + if is_set { + args.push(true.as_arg()); + } + args + }, + type_args: Default::default(), + }) + } + + _ => e, + } + } +} + /// `None`: `return _possibleConstructorReturn` #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(super) enum SuperFoldingMode { @@ -190,10 +328,51 @@ impl Fold for ConstructorFolder<'_> { ignore_return!(fold_arrow_expr, ArrowExpr); ignore_return!(fold_constructor, Constructor); + fn fold_call_expr(&mut self, e: CallExpr) -> CallExpr { + match &e.callee { + ExprOrSuper::Expr(callee) => match &**callee { + Expr::Member(MemberExpr { + obj: ExprOrSuper::Super(..), + .. + }) => { + let old = self.cur_this_super.take(); + + let mut e = e.fold_children_with(self); + + let this_super = self.cur_this_super.take().unwrap(); + self.cur_this_super = old; + + e.args.insert(0, this_super.as_arg()); + + CallExpr { + span: e.span, + callee: e + .callee + .expect_expr() + .make_member(quote_ident!("call")) + .as_callee(), + args: e.args, + type_args: Default::default(), + } + } + + _ => return e.fold_children_with(self), + }, + + _ => return e.fold_children_with(self), + } + } + fn fold_expr(&mut self, expr: Expr) -> Expr { match self.mode { Some(SuperFoldingMode::Assign) => {} - _ => return expr, + _ => { + let expr = match self.handle_super_assignment(expr) { + Ok(v) => v, + Err(v) => v.fold_children_with(self), + }; + return self.handle_super_access(expr, None); + } } // We pretend method folding mode for while folding injected `_defineProperty` @@ -218,7 +397,10 @@ impl Fold for ConstructorFolder<'_> { _ => {} } - let expr = expr.fold_children_with(self); + let expr = match self.handle_super_assignment(expr) { + Ok(v) => v, + Err(v) => v.fold_children_with(self), + }; match expr { Expr::This(e) => Expr::Ident(Ident::new("_this".into(), e.span.apply_mark(self.mark))), @@ -227,11 +409,34 @@ impl Fold for ConstructorFolder<'_> { args, .. }) => { - let right = Box::new(make_possible_return_value(ReturningMode::Prototype { - class_name: self.class_name.clone(), - args: Some(args), - is_constructor_default: self.is_constructor_default, - })); + let right = match self.super_var.clone() { + Some(super_var) => Box::new(Expr::Call(CallExpr { + span: DUMMY_SP, + callee: if self.is_constructor_default { + super_var.make_member(quote_ident!("apply")).as_callee() + } else { + super_var.make_member(quote_ident!("call")).as_callee() + }, + args: if self.is_constructor_default { + vec![ + ThisExpr { span: DUMMY_SP }.as_arg(), + quote_ident!("arguments").as_arg(), + ] + } else { + let mut call_args = vec![ThisExpr { span: DUMMY_SP }.as_arg()]; + call_args.extend(args); + + call_args + }, + type_args: Default::default(), + })), + + None => Box::new(make_possible_return_value(ReturningMode::Prototype { + class_name: self.class_name.clone(), + args: Some(args), + is_constructor_default: self.is_constructor_default, + })), + }; Expr::Assign(AssignExpr { span: DUMMY_SP, @@ -242,7 +447,8 @@ impl Fold for ConstructorFolder<'_> { right, }) } - _ => expr, + + _ => self.handle_super_access(expr, None), } } @@ -273,11 +479,33 @@ impl Fold for ConstructorFolder<'_> { args, .. }) => { - let expr = make_possible_return_value(ReturningMode::Prototype { - is_constructor_default: self.is_constructor_default, - class_name: self.class_name.clone(), - args: Some(args), - }); + let expr = match self.super_var.clone() { + Some(super_var) => Box::new(Expr::Call(CallExpr { + span: DUMMY_SP, + callee: if self.is_constructor_default { + super_var.make_member(quote_ident!("apply")).as_callee() + } else { + super_var.make_member(quote_ident!("call")).as_callee() + }, + args: if self.is_constructor_default { + vec![ + ThisExpr { span: DUMMY_SP }.as_arg(), + quote_ident!("arguments").as_arg(), + ] + } else { + let mut call_args = vec![ThisExpr { span: DUMMY_SP }.as_arg()]; + call_args.extend(args); + + call_args + }, + type_args: Default::default(), + })), + None => Box::new(make_possible_return_value(ReturningMode::Prototype { + is_constructor_default: self.is_constructor_default, + class_name: self.class_name.clone(), + args: Some(args), + })), + }; match self.mode { Some(SuperFoldingMode::Assign) => AssignExpr { @@ -286,7 +514,7 @@ impl Fold for ConstructorFolder<'_> { quote_ident!(DUMMY_SP.apply_mark(self.mark), "_this").into(), ))), op: op!("="), - right: expr.into(), + right: expr, } .into_stmt(), Some(SuperFoldingMode::Var) => Stmt::Decl(Decl::Var(VarDecl { @@ -298,7 +526,7 @@ impl Fold for ConstructorFolder<'_> { name: Pat::Ident( quote_ident!(DUMMY_SP.apply_mark(self.mark), "_this").into(), ), - init: Some(expr.into()), + init: Some(expr), definite: false, }], })), diff --git a/ecmascript/transforms/compat/src/es2015/classes/mod.rs b/ecmascript/transforms/compat/src/es2015/classes/mod.rs index 9bd0e8a3896..22969192dc4 100644 --- a/ecmascript/transforms/compat/src/es2015/classes/mod.rs +++ b/ecmascript/transforms/compat/src/es2015/classes/mod.rs @@ -427,6 +427,29 @@ where ); } + let super_var = super_class_ident.as_ref().map(|_| { + let var = private_ident!("_super"); + + stmts.push(Stmt::Decl(Decl::Var(VarDecl { + span: DUMMY_SP, + kind: VarDeclKind::Var, + declare: Default::default(), + decls: vec![VarDeclarator { + span: DUMMY_SP, + name: Pat::Ident(var.clone().into()), + init: Some(Box::new(Expr::Call(CallExpr { + span: DUMMY_SP, + callee: helper!(create_super, "createSuper"), + args: vec![class_name.clone().as_arg()], + type_args: Default::default(), + }))), + definite: Default::default(), + }], + }))); + + var + }); + // Marker for `_this` let this_mark = Mark::fresh(Mark::root()); @@ -457,6 +480,7 @@ where insert_this |= inserted_this; } + let mut vars = vec![]; let mut body = constructor.body.unwrap().stmts; // should we insert `var _this`? @@ -486,15 +510,18 @@ where // We should fold body instead of constructor itself. // Handle `super()` body = body.fold_with(&mut ConstructorFolder { - is_constructor_default, class_name: &class_name, - // This if expression is required to handle super() call in all case mode: if insert_this { Some(SuperFoldingMode::Assign) } else { mode }, + vars: &mut vars, + // This if expression is required to handle super() call in all case + cur_this_super: None, mark: this_mark, + is_constructor_default, + super_var, ignore_return: false, in_injected_define_property_call: false, }); @@ -503,18 +530,21 @@ where || mode == Some(SuperFoldingMode::Assign); if insert_this { + vars.push(VarDeclarator { + span: DUMMY_SP, + name: Pat::Ident(this.clone().into()), + init: None, + definite: false, + }); + } + if !vars.is_empty() { prepend( &mut body, Stmt::Decl(Decl::Var(VarDecl { span: DUMMY_SP, declare: false, kind: VarDeclKind::Var, - decls: vec![VarDeclarator { - span: DUMMY_SP, - name: Pat::Ident(this.clone().into()), - init: None, - definite: false, - }], + decls: vars, })), ); } diff --git a/ecmascript/transforms/compat/src/es2015/mod.rs b/ecmascript/transforms/compat/src/es2015/mod.rs index d1fc3f3b43f..eeb3e561d0a 100644 --- a/ecmascript/transforms/compat/src/es2015/mod.rs +++ b/ecmascript/transforms/compat/src/es2015/mod.rs @@ -135,15 +135,20 @@ export var Foo = function() { class HomePage extends React.Component {} "#, r#" -var HomePage = function(_Component) { - 'use strict'; - _inherits(HomePage, _Component); - function HomePage() { - _classCallCheck(this, HomePage); - return _possibleConstructorReturn(this, _getPrototypeOf(HomePage).apply(this, arguments)); - } - return HomePage; -}(React.Component); + var HomePage = /*#__PURE__*/function (_Component) { + "use strict"; + _inherits(HomePage, _Component); + + var _super = _createSuper(HomePage); + + function HomePage() { + _classCallCheck(this, HomePage); + + return _super.apply(this, arguments); + } + + return HomePage; + }(React.Component); "# ); @@ -285,10 +290,11 @@ class B extends A { var B = function(A) { 'use strict'; _inherits(B, A); + var _super = _createSuper(B); function B(num) { _classCallCheck(this, B); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(B).call(this)); + _this = _super.call(this); _this.b_num = num; return _this; } diff --git a/ecmascript/transforms/compat/tests/es2015_classes.rs b/ecmascript/transforms/compat/tests/es2015_classes.rs index 58c4ca94c16..a103fc1308c 100644 --- a/ecmascript/transforms/compat/tests/es2015_classes.rs +++ b/ecmascript/transforms/compat/tests/es2015_classes.rs @@ -1,5 +1,4 @@ use std::{fs::read_to_string, path::PathBuf}; - use swc_common::{chain, Mark}; use swc_ecma_parser::Syntax; use swc_ecma_transforms_base::resolver::resolver; @@ -9,7 +8,7 @@ use swc_ecma_transforms_compat::{ es2016, es2017, es2018, es2022, es2022::class_properties, }; -use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, Tester}; +use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, test_fixture, Tester}; use swc_ecma_visit::Fold; fn syntax() -> Syntax { @@ -42,9 +41,10 @@ class HomePage extends React.Component {} let HomePage = function(_Component) { 'use strict'; _inherits(HomePage, _Component); + var _super = _createSuper(HomePage); function HomePage() { _classCallCheck(this, HomePage); - return _possibleConstructorReturn(this, _getPrototypeOf(HomePage).apply(this, arguments)); + return _super.apply(this, arguments); } return HomePage; }(React.Component); @@ -71,13 +71,16 @@ let singleton; let Sub = function(Foo) { 'use strict'; _inherits(Sub, Foo); + + var _super = _createSuper(Sub); + function Sub() { _classCallCheck(this, Sub); var _this; if (singleton) { return _possibleConstructorReturn(_this, singleton); } - singleton = _this = _possibleConstructorReturn(this, _getPrototypeOf(Sub).call(this)); + singleton = _this = _super.call(this); return _possibleConstructorReturn(_this); } return Sub; @@ -96,9 +99,10 @@ test!( let List = function(Array) { 'use strict'; _inherits(List, Array); + var _super = _createSuper(List); function List() { _classCallCheck(this, List); - return _possibleConstructorReturn(this, _getPrototypeOf(List).apply(this, arguments)); + return _super.apply(this, arguments); } return List; }(_wrapNativeSuper(Array)); @@ -335,11 +339,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -391,6 +395,8 @@ function (Bar) { _inherits(Foo, Bar); + var _super = _createSuper(Foo); + function Foo() { _classCallCheck(this, Foo); @@ -443,11 +449,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -506,9 +512,10 @@ var constructor; let CustomElement = function(HTMLElement) { 'use strict'; _inherits(CustomElement, HTMLElement); + var _super = _createSuper(CustomElement); function CustomElement() { _classCallCheck(this, CustomElement); - return _possibleConstructorReturn(this, _getPrototypeOf(CustomElement).apply(this, arguments)); + return _super.apply(this, arguments); } return CustomElement; }(_wrapNativeSuper(HTMLElement)); @@ -539,10 +546,11 @@ class A extends B { let A = function(B) { 'use strict'; _inherits(A, B); + var _super = _createSuper(A); function A() { _classCallCheck(this, A); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(A).call(this)); + _this = _super.call(this); _this.arrow1 = function(x) { return x; }; @@ -605,11 +613,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -693,11 +701,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -741,12 +749,15 @@ class Foo extends Bar { var Foo = function(Bar) { 'use strict'; _inherits(Foo, Bar); + + var _super = _createSuper(Foo); + function Foo() { _classCallCheck(this, Foo); var _this; var fn = ()=>_assertThisInitialized(_this) ; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); fn(); return _this; } @@ -775,29 +786,34 @@ class Test extends Foo { "#, r#" -var Test = -/*#__PURE__*/ -function (Foo) { - 'use strict'; -_inherits(Test, Foo); - - function Test() { - _classCallCheck(this, Test); - var _this = _possibleConstructorReturn(this, _getPrototypeOf(Test).call(this)); - _get(_getPrototypeOf(Test.prototype), "test", _assertThisInitialized(_this)).whatever(); - _get(_getPrototypeOf(Test.prototype), "test", _assertThisInitialized(_this)).call(_this); - return _this; - } - - _createClass(Test, null, [{ - key: "test", - value: function test() { - return _get(_getPrototypeOf(Test), "wow", this).call(this); - } - }]); - return Test; -}(Foo); - + var Test = /*#__PURE__*/function (Foo) { + "use strict"; + _inherits(Test, Foo); + + var _super = _createSuper(Test); + + function Test() { + _classCallCheck(this, Test); + var _thisSuper, _thisSuper1; + + var _this = _super.call(this); + + _get((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(Test.prototype)), "test", _thisSuper).whatever(); + + _get((_thisSuper1 = _assertThisInitialized(_this), _getPrototypeOf(Test.prototype)), "test", _thisSuper1).call(_thisSuper1); + + return _this; + } + + _createClass(Test, null, [{ + key: "test", + value: function test() { + return _get(_getPrototypeOf(Test), "wow", this).call(this); + } + }]); + + return Test; + }(Foo); "# ); @@ -892,10 +908,10 @@ var x = { function (Foo) { 'use strict'; _inherits(_class, Foo); - + var _super = _createSuper(_class); function _class() { _classCallCheck(this, _class); - return _possibleConstructorReturn(this, _getPrototypeOf(_class).apply(this, arguments)); + return _super.apply(this, arguments); } return _class; @@ -1088,11 +1104,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -1137,11 +1153,12 @@ class Foo extends Bar { var Foo = function(Bar) { 'use strict'; _inherits(Foo, Bar); + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; _this.foo = 'bar'; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); return _this; } return Foo; @@ -1217,11 +1234,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -1353,10 +1370,12 @@ function (Base) { 'use strict'; _inherits(Obj, Base); + var _super = _createSuper(Obj); + function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -1513,11 +1532,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -1604,26 +1623,26 @@ function () { var Outer = function (Hello) { 'use strict'; _inherits(Outer, Hello); - + var _super = _createSuper(Outer); function Outer() { _classCallCheck(this, Outer); - var _this = _possibleConstructorReturn(this, _getPrototypeOf(Outer).call(this)); + var _thisSuper; + var _this = _super.call(this); - var Inner = - /*#__PURE__*/ - function () { + var Inner = /*#__PURE__*/function () { function Inner() { _classCallCheck(this, Inner); } _createClass(Inner, [{ - key: _get(_getPrototypeOf(Outer.prototype), "toString", _assertThisInitialized(_this)).call(_this), + key: _get((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper), value: function () { return 'hello'; } }]); - return Inner; - }(); + + return Inner; + }(); return _possibleConstructorReturn(_this, new Inner()); } @@ -1650,17 +1669,24 @@ class Foo extends Bar { "#, r#" -var Foo = function(Bar) { - 'use strict'; - _inherits(Foo, Bar); - function Foo() { + var Foo = /*#__PURE__*/function (Bar) { + "use strict"; + _inherits(Foo, Bar); + + var _super = _createSuper(Foo); + + function Foo() { _classCallCheck(this, Foo); - var _this; - _get(_getPrototypeOf(Foo.prototype), (_this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this))).method, _assertThisInitialized(_this)).call(_this); + + var _thisSuper, _this; + + _get((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(Foo.prototype)), (_this = _super.call(this)).method, _thisSuper).call(_thisSuper); + return _possibleConstructorReturn(_this); - } - return Foo; -}(Bar); + } + + return Foo; + }(Bar); "# @@ -1763,10 +1789,12 @@ function (Bar) { _inherits(Foo, Bar); + var _super = _createSuper(Foo); + function Foo() { _classCallCheck(this, Foo); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this, _assertThisInitialized(_this))); + _this = _super.call(this, _assertThisInitialized(_this)) return _this; } @@ -1792,22 +1820,25 @@ class Test extends Foo { "#, r#" -var Test = -/*#__PURE__*/ -function (Foo) { - 'use strict'; -_inherits(Test, Foo); - - function Test() { - _classCallCheck(this, Test); - var _this = _possibleConstructorReturn(this, _getPrototypeOf(Test).call(this)); - _get(_getPrototypeOf(Test.prototype), "test", _assertThisInitialized(_this)); - _get(_getPrototypeOf(Test.prototype), "test", _assertThisInitialized(_this)).whatever; - return _this; - } - - return Test; -}(Foo); + var Test = /*#__PURE__*/function (Foo) { + "use strict"; + _inherits(Test, Foo); + + var _super = _createSuper(Test); + + function Test() { + _classCallCheck(this, Test); + var _thisSuper, _thisSuper1; + var _this = _super.call(this); + + _get((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(Test.prototype)), "test", _thisSuper); + + _get((_thisSuper1 = _assertThisInitialized(_this), _getPrototypeOf(Test.prototype)), "test", _thisSuper1).whatever; + return _this; + } + + return Test; + }(Foo); "# ); @@ -1910,11 +1941,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -1979,11 +2010,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -2133,17 +2164,20 @@ let ColorPoint = function (Point) { 'use strict'; _inherits(ColorPoint, Point); - +var _super = _createSuper(ColorPoint); function ColorPoint() { _classCallCheck(this, ColorPoint); - var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(ColorPoint).call(this)); + var _thisSuper, _thisSuper1, _this; + + _this = _super.call(this); _this.x = 2; - _set(_getPrototypeOf(ColorPoint.prototype), 'x', 3, this, true); + + _set((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(ColorPoint.prototype)), "x", 3, _thisSuper, true); + expect(_this.x).toBe(3); // A - expect(_get(_getPrototypeOf(ColorPoint.prototype), "x", _assertThisInitialized(_this))).toBeUndefined(); // B + expect(_get((_thisSuper1 = _assertThisInitialized(_this), _getPrototypeOf(ColorPoint.prototype)), "x", _thisSuper1)).toBeUndefined(); // B return _this; } @@ -2360,13 +2394,13 @@ var Hello = function Hello() { var Outer = function (Hello) { 'use strict'; _inherits(Outer, Hello); - + var _super = _createSuper(Outer); function Outer() { _classCallCheck(this, Outer); var _this; var Inner = { - [_this = _possibleConstructorReturn(this, _getPrototypeOf(Outer).call(this))]() { + [_this = _super.call(this)]() { return 'hello'; } @@ -2418,11 +2452,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -2461,6 +2495,7 @@ class Child extends Base { var Child = function(Base) { 'use strict'; _inherits(Child, Base); + var _super = _createSuper(Child); function Child() { _classCallCheck(this, Child); var _this; @@ -2492,6 +2527,7 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); @@ -2551,11 +2587,11 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); _this.state = "test"; return _this; } @@ -2655,6 +2691,7 @@ class Child extends Base { var Child = function(Base) { 'use strict'; _inherits(Child, Base); + var _super = _createSuper(Child); function Child() { _classCallCheck(this, Child); var _this; @@ -2773,12 +2810,12 @@ let B = function (A) { 'use strict'; _inherits(B, A); - +var _super = _createSuper(B); function B() { _classCallCheck(this, B); var _this; - return _possibleConstructorReturn(_this, _this = _possibleConstructorReturn(this, _getPrototypeOf(B).call(this))); + return _possibleConstructorReturn(_this, _this = _super.call(this)); } return B; @@ -2843,131 +2880,106 @@ class TestMultipleMethods extends (class { "#, r#" -var TestEmpty = -/*#__PURE__*/ -function (_super) { - 'use strict'; - - _inherits(TestEmpty, _super); - - function TestEmpty() { - _classCallCheck(this, TestEmpty); - return _possibleConstructorReturn(this, _getPrototypeOf(TestEmpty).apply(this, arguments)); - } - - return TestEmpty; -}(function _class() { - 'use strict'; - _classCallCheck(this, _class); -}); - -var TestConstructorOnly = -/*#__PURE__*/ -function (_super) { - 'use strict'; - - _inherits(TestConstructorOnly, _super); - - function TestConstructorOnly() { - _classCallCheck(this, TestConstructorOnly); - return _possibleConstructorReturn(this, _getPrototypeOf(TestConstructorOnly).apply(this, arguments)); - } - - return TestConstructorOnly; -}(function _class() { - 'use strict'; - _classCallCheck(this, _class); -}); - -var TestMethodOnly = -/*#__PURE__*/ -function (_super) { - 'use strict'; - - _inherits(TestMethodOnly, _super); - - function TestMethodOnly() { - _classCallCheck(this, TestMethodOnly); - return _possibleConstructorReturn(this, _getPrototypeOf(TestMethodOnly).apply(this, arguments)); - } - - return TestMethodOnly; -}( -/*#__PURE__*/ -function () { - 'use strict'; - - function _class() { - _classCallCheck(this, _class); - } - - _createClass(_class, [{ - key: "method", - value: function method() {} - }]); - return _class; -}()); - -var TestConstructorAndMethod = -/*#__PURE__*/ -function (_super) { - 'use strict'; - - _inherits(TestConstructorAndMethod, _super); - - function TestConstructorAndMethod() { - _classCallCheck(this, TestConstructorAndMethod); - return _possibleConstructorReturn(this, _getPrototypeOf(TestConstructorAndMethod).apply(this, arguments)); - } - - return TestConstructorAndMethod; -}( -/*#__PURE__*/ -function () { - 'use strict'; - - function _class() { - _classCallCheck(this, _class); - } - - _createClass(_class, [{ - key: "method", - value: function method() {} - }]); - return _class; -}()); - -var TestMultipleMethods = -/*#__PURE__*/ -function (_super) { - 'use strict'; - - _inherits(TestMultipleMethods, _super); - - function TestMultipleMethods() { - _classCallCheck(this, TestMultipleMethods); - return _possibleConstructorReturn(this, _getPrototypeOf(TestMultipleMethods).apply(this, arguments)); - } - - return TestMultipleMethods; -}( -/*#__PURE__*/ -function () { - 'use strict'; - - function _class() { - _classCallCheck(this, _class); - } - - _createClass(_class, [{ - key: "m1", - value: function m1() {} - }, { - key: "m2", - value: function m2() {} - }]); - return _class; -}()); + var TestEmpty = function(_super) { + "use strict"; + _inherits(TestEmpty, _super); + var _super1 = _createSuper(TestEmpty); + function TestEmpty() { + _classCallCheck(this, TestEmpty); + return _super1.apply(this, arguments); + } + return TestEmpty; + }(function _class() { + "use strict"; + _classCallCheck(this, _class); + }); + var TestConstructorOnly = function(_super) { + "use strict"; + _inherits(TestConstructorOnly, _super); + var _super2 = _createSuper(TestConstructorOnly); + function TestConstructorOnly() { + _classCallCheck(this, TestConstructorOnly); + return _super2.apply(this, arguments); + } + return TestConstructorOnly; + }(function _class() { + "use strict"; + _classCallCheck(this, _class); + }); + var TestMethodOnly = function(_super) { + "use strict"; + _inherits(TestMethodOnly, _super); + var _super3 = _createSuper(TestMethodOnly); + function TestMethodOnly() { + _classCallCheck(this, TestMethodOnly); + return _super3.apply(this, arguments); + } + return TestMethodOnly; + }(function() { + "use strict"; + function _class() { + _classCallCheck(this, _class); + } + _createClass(_class, [ + { + key: "method", + value: function method() { + } + } + ]); + return _class; + }()); + var TestConstructorAndMethod = function(_super) { + "use strict"; + _inherits(TestConstructorAndMethod, _super); + var _super4 = _createSuper(TestConstructorAndMethod); + function TestConstructorAndMethod() { + _classCallCheck(this, TestConstructorAndMethod); + return _super4.apply(this, arguments); + } + return TestConstructorAndMethod; + }(function() { + "use strict"; + function _class() { + _classCallCheck(this, _class); + } + _createClass(_class, [ + { + key: "method", + value: function method() { + } + } + ]); + return _class; + }()); + var TestMultipleMethods = function(_super) { + "use strict"; + _inherits(TestMultipleMethods, _super); + var _super5 = _createSuper(TestMultipleMethods); + function TestMultipleMethods() { + _classCallCheck(this, TestMultipleMethods); + return _super5.apply(this, arguments); + } + return TestMultipleMethods; + }(function() { + "use strict"; + function _class() { + _classCallCheck(this, _class); + } + _createClass(_class, [ + { + key: "m1", + value: function m1() { + } + }, + { + key: "m2", + value: function m2() { + } + } + ]); + return _class; + }()); "# ); @@ -3023,6 +3035,7 @@ var Hello = function Hello() { var Outer = function (Hello) { 'use strict'; _inherits(Outer, Hello); + var _super = _createSuper(Outer); function Outer() { _classCallCheck(this, Outer); @@ -3037,7 +3050,7 @@ var Outer = function (Hello) { } _createClass(Inner, [{ - key: _this1 = _possibleConstructorReturn(_this, _getPrototypeOf(Outer).call(_this)), + key: _this1 = _super.call(_this), value: function () { return 'hello'; } @@ -3258,13 +3271,14 @@ class Foo extends Bar { var Foo = function(Bar) { 'use strict'; _inherits(Foo, Bar); + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; var fn = ()=>_assertThisInitialized(_this) ; fn(); - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); return _this; } return Foo; @@ -3507,11 +3521,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -3581,11 +3595,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -3687,12 +3701,13 @@ var Outer = function (Hello) { 'use strict'; _inherits(Outer, Hello); - + var _super = _createSuper(Outer); function Outer() { _classCallCheck(this, Outer); - var _this = _possibleConstructorReturn(this, _getPrototypeOf(Outer).call(this)); + var _thisSuper; + var _this = _super.call(this); var Inner = { - [_get(_getPrototypeOf(Outer.prototype), 'toString', _assertThisInitialized(_this)).call(_this)] () { + [_get((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper)] () { return 'hello'; } @@ -3837,11 +3852,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -3884,10 +3899,11 @@ function (Foo) { 'use strict'; _inherits(Test, Foo); + var _super = _createSuper(Test); function Test() { _classCallCheck(this, Test); - return _possibleConstructorReturn(this, _getPrototypeOf(Test).apply(this, arguments)); + return _super.apply(this, arguments); } return Test; @@ -3952,12 +3968,12 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; - if (eval("false")) _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + if (eval("false")) _this = _super.call(this); return _possibleConstructorReturn(_this); } @@ -3988,13 +4004,13 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; - var fn = () => _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + var fn = () => _this = _super.call(this) fn(); return _possibleConstructorReturn(_this); @@ -4068,11 +4084,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -4180,11 +4196,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -4355,13 +4371,13 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; - var fn = () => _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + var fn = () => _this = _super.call(this) return _possibleConstructorReturn(_this); } @@ -4394,10 +4410,10 @@ function (_Controller) { 'use strict'; _inherits(BaseController, _Controller); - + var _super = _createSuper(BaseController); function BaseController() { _classCallCheck(this, BaseController); - return _possibleConstructorReturn(this, _getPrototypeOf(BaseController).apply(this, arguments)); + return _super.apply(this, arguments); } return BaseController; @@ -4409,10 +4425,10 @@ function (_Another) { 'use strict'; _inherits(BaseController2, _Another); - + var _super = _createSuper(BaseController2); function BaseController2() { _classCallCheck(this, BaseController2); - return _possibleConstructorReturn(this,_getPrototypeOf(BaseController2).apply(this, arguments)); + return _super.apply(this, arguments); } return BaseController2; @@ -4443,15 +4459,15 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this, () => { + _this = _super.call(this, () => { _this.test; - })); + }); return _this; } @@ -4554,11 +4570,11 @@ let Obj = function (Base) { 'use strict'; _inherits(Obj, Base); - + var _super = _createSuper(Obj); function Obj() { _classCallCheck(this, Obj); - return _possibleConstructorReturn(this, _getPrototypeOf(Obj).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Obj, [{ @@ -4683,10 +4699,10 @@ var _default = function (A) { 'use strict'; _inherits(_default, A); - + var _super = _createSuper(_default); function _default() { _classCallCheck(this, _default); - return _possibleConstructorReturn(this, _getPrototypeOf(_default).apply(this, arguments)); + return _super.apply(this, arguments); } return _default; @@ -4734,6 +4750,8 @@ expect(t2).toBeInstanceOf(Array); // spec_super_reference_in_prop_exression_exec test_exec!( + // babel also fails on this + ignore, syntax(), |t| spec_tr(t), spec_super_reference_in_prop_exression_exec, @@ -4813,9 +4831,10 @@ test!( var Foo = function(Bar) { 'use strict'; _inherits(Foo, Bar); + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); - return _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, arguments)); + return _super.apply(this, arguments); } return Foo; }(Bar); @@ -4837,10 +4856,10 @@ test!( var Foo = function(Bar) { 'use strict'; _inherits(Foo, Bar); + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); - return _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, \ - arguments)); + return _super.apply(this, arguments); } return Foo; }(Bar); @@ -6081,10 +6100,10 @@ test!( let ColouredCanvasElement = function (CanvasElement) { 'use strict'; _inherits(ColouredCanvasElement, CanvasElement); + var _super = _createSuper(ColouredCanvasElement); function ColouredCanvasElement() { _classCallCheck(this, ColouredCanvasElement); - return _possibleConstructorReturn(this, \ - _getPrototypeOf(ColouredCanvasElement).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(ColouredCanvasElement, [ { @@ -6115,10 +6134,10 @@ test!( let ColouredCanvasElement = function (CanvasElement) { 'use strict'; _inherits(ColouredCanvasElement, CanvasElement); + var _super = _createSuper(ColouredCanvasElement); function ColouredCanvasElement() { _classCallCheck(this, ColouredCanvasElement); - return _possibleConstructorReturn(this, \ - _getPrototypeOf(ColouredCanvasElement).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(ColouredCanvasElement, [ { @@ -6154,9 +6173,10 @@ test!( let Test = function(Foo1) { 'use strict'; _inherits(Test, Foo1); + var _super = _createSuper(Test); function Test() { _classCallCheck(this, Test); - return _possibleConstructorReturn(this, _getPrototypeOf(Test).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Test, [ { @@ -6202,9 +6222,10 @@ test!( let A = function(B) { "use strict"; _inherits(A, B); + var _super = _createSuper(A); function A() { _classCallCheck(this, A); - return _possibleConstructorReturn(this, _getPrototypeOf(A).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(A, [ { @@ -6234,9 +6255,10 @@ test!( let A = function(B) { "use strict"; _inherits(A, B); + var _super = _createSuper(A); function A() { _classCallCheck(this, A); - return _possibleConstructorReturn(this, _getPrototypeOf(A).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(A, [ { @@ -6470,9 +6492,10 @@ test!( var Extended = function(Base) { 'use strict'; _inherits(Extended, Base); + var _super = _createSuper(Extended); function Extended() { _classCallCheck(this, Extended); - return _possibleConstructorReturn(this, _getPrototypeOf(Extended).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Extended, [ { @@ -6516,9 +6539,10 @@ test!( var Extended = function(Base) { 'use strict'; _inherits(Extended, Base); + var _super = _createSuper(Extended); function Extended() { _classCallCheck(this, Extended); - return _possibleConstructorReturn(this, _getPrototypeOf(Extended).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Extended, [ { @@ -6548,3 +6572,20 @@ fn exec(input: PathBuf) { &src, ); } + +#[testing::fixture("tests/fixture/classes/**/input.js")] +fn fixture(input: PathBuf) { + let output = input.parent().unwrap().join("output.js"); + + test_fixture( + Default::default(), + &|t| { + chain!( + class_properties(class_properties::Config { loose: false }), + classes(Some(t.comments.clone())) + ) + }, + &input, + &output, + ); +} diff --git a/ecmascript/transforms/compat/tests/es2015_parameters.rs b/ecmascript/transforms/compat/tests/es2015_parameters.rs index 61de432e3a7..9a2ce7a6b63 100644 --- a/ecmascript/transforms/compat/tests/es2015_parameters.rs +++ b/ecmascript/transforms/compat/tests/es2015_parameters.rs @@ -1228,10 +1228,10 @@ test!( let Foo = function(Bar) { 'use strict'; _inherits(Foo, Bar); + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); - return _possibleConstructorReturn(this, - _getPrototypeOf(Foo).apply(this, arguments)); + return _super.apply(this, arguments); } return Foo; }(Bar); diff --git a/ecmascript/transforms/compat/tests/es2022_class_properties.rs b/ecmascript/transforms/compat/tests/es2022_class_properties.rs index 113d7b6abfb..7b956c36d15 100644 --- a/ecmascript/transforms/compat/tests/es2022_class_properties.rs +++ b/ecmascript/transforms/compat/tests/es2022_class_properties.rs @@ -144,12 +144,12 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); _defineProperty(_assertThisInitialized(_this), "bar", "foo"); return _this; } @@ -183,12 +183,12 @@ function (Parent) { 'use strict'; _inherits(Child, Parent); - + var _super = _createSuper(Child); function Child() { _classCallCheck(this, Child); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Child).call(this)); + _this = _super.call(this); _scopedFunctionWithThis.set(_assertThisInitialized(_this), { writable: true, @@ -257,17 +257,17 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; if (condition) { - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); _defineProperty(_assertThisInitialized(_this), "bar", "foo"); } else { - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); _defineProperty(_assertThisInitialized(_this), "bar", "foo"); } @@ -391,9 +391,10 @@ function withContext(ComposedComponent) { var WithContext = function(Component) { 'use strict'; _inherits(WithContext, Component); + var _super = _createSuper(WithContext); function WithContext() { _classCallCheck(this, WithContext); - return _possibleConstructorReturn(this, _getPrototypeOf(WithContext).apply(this, arguments)); + return _super.apply(this, arguments); } return WithContext; }(Component); @@ -584,12 +585,12 @@ var Hello = function Hello() { var Outer = function (Hello) { 'use strict'; _inherits(Outer, Hello); - + var _super = _createSuper(Outer); function Outer() { _classCallCheck(this, Outer); var _this; - var _ref = _this = _possibleConstructorReturn(this, _getPrototypeOf(Outer).call(this)); + var _ref = _this = _super.call(this); var Inner = function Inner() { _classCallCheck(this, Inner); @@ -653,21 +654,21 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; if (condition) { - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); _bar.set(_assertThisInitialized(_this), { writable: true, value: "foo" }); } else { - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); _bar.set(_assertThisInitialized(_this), { writable: true, @@ -870,7 +871,7 @@ class B extends A { } "#, - r#" + " var A = /*#__PURE__*/ function () { @@ -881,9 +882,9 @@ function () { } _createClass(A, [{ - key: "foo", + key: 'foo', value: function foo() { - return "bar"; + return 'bar'; } }]); return A; @@ -895,20 +896,22 @@ function (A) { 'use strict'; _inherits(B, A); - + var _super = _createSuper(B); function B() { _classCallCheck(this, B); - var _this; + var _thisSuper, _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(B).apply(this, arguments)); - _defineProperty(_assertThisInitialized(_this), "foo", _get(_getPrototypeOf(B.prototype), "foo", _assertThisInitialized(_this)).call(_this)); + _this = _super.apply(this, arguments); + _defineProperty(_assertThisInitialized(_this), 'foo', _get((_thisSuper = \ + _assertThisInitialized(_this), _getPrototypeOf(B.prototype)), 'foo', \ + _thisSuper).call(_thisSuper)); return _this; } return B; }(A); -"# +" ); test!( @@ -1390,12 +1393,12 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)); + _this = _super.call(this); _bar.set(_assertThisInitialized(_this), { writable: true, @@ -1441,10 +1444,10 @@ var Outer = function Outer() { var Test = function (_super) { _inherits(Test, _super); - + var _super1 = _createSuper(Test); function Test() { _classCallCheck(this, Test); - return _possibleConstructorReturn(this, _getPrototypeOf(Test).apply(this, arguments)); + return _super1.apply(this, arguments); } return Test; @@ -1526,13 +1529,13 @@ var Foo = function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; var _temp; - foo((_temp = _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)), _defineProperty(_assertThisInitialized(_this), "bar", "foo"), _temp)); + foo((_temp = _this = _super.call(this), _defineProperty(_assertThisInitialized(_this), "bar", "foo"), _temp)); return _possibleConstructorReturn(_this); } @@ -1720,12 +1723,12 @@ function (Foo) { 'use strict'; _inherits(Bar, Foo); - + var _super = _createSuper(Bar); function Bar() { _classCallCheck(this, Bar); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Bar).apply(this, arguments)); + _this = _super.apply(this, arguments); _prop1.set(_assertThisInitialized(_this), { writable: true, @@ -1782,16 +1785,16 @@ function (A) { 'use strict'; _inherits(B, A); - + var _super = _createSuper(B); function B() { _classCallCheck(this, B); - var _this; + var _thisSuper, _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(B).apply(this, arguments)); + _this = _super.apply(this, arguments); _foo.set(_assertThisInitialized(_this), { writable: true, - value: _get(_getPrototypeOf(B.prototype), "foo", _assertThisInitialized(_this)).call(_this) + value: _get((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(B.prototype)), "foo", _thisSuper).call(_thisSuper) }); return _this; @@ -1900,12 +1903,13 @@ var Hello = function () { var Outer = function (Hello) { 'use strict'; _inherits(Outer, Hello); - + var _super = _createSuper(Outer); function Outer() { _classCallCheck(this, Outer); - var _this = _possibleConstructorReturn(this, _getPrototypeOf(Outer).call(this)); + var _thisSuper; + var _this = _super.call(this); - var _ref = _get(_getPrototypeOf(Outer.prototype), 'toString', _assertThisInitialized(_this)).call(_this); + var _ref = _get((_thisSuper = _assertThisInitialized(_this), _getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper); var Inner = function Inner() { _classCallCheck(this, Inner); @@ -2167,12 +2171,12 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; - _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).apply(this, arguments)) + _this = _super.apply(this, arguments); _defineProperty(_assertThisInitialized(_this), "bar", "foo"); return _this; } @@ -2658,14 +2662,13 @@ function (Bar) { 'use strict'; _inherits(Foo, Bar); - + var _super = _createSuper(Foo); function Foo() { _classCallCheck(this, Foo); var _this; var _temp; - foo((_temp = _this = _possibleConstructorReturn(this, _getPrototypeOf(Foo).call(this)), - _bar.set(_assertThisInitialized(_this), { + foo((_temp = _this = _super.call(this), _bar.set(_assertThisInitialized(_this), { writable: true, value: "foo" }), _temp)); @@ -4704,10 +4707,10 @@ function (A) { "use strict"; _inherits(B, A); - + var _super = _createSuper(B); function B() { _classCallCheck(this, B); - return _possibleConstructorReturn(this, _getPrototypeOf(B).apply(this, arguments)); + return _super.apply(this, arguments); } return B; diff --git a/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/exec.js b/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/exec.js new file mode 100644 index 00000000000..26140eee8b1 --- /dev/null +++ b/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/exec.js @@ -0,0 +1,9 @@ + +const UnknownNativeClass = Array; + + +class Foo extends UnknownNativeClass { +} + +new Foo() +console.log('PASS') \ No newline at end of file diff --git a/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/input.js b/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/input.js new file mode 100644 index 00000000000..479c34396f6 --- /dev/null +++ b/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/input.js @@ -0,0 +1,2 @@ +class Foo extends UnknownNativeClass { +} \ No newline at end of file diff --git a/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/output.js b/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/output.js new file mode 100644 index 00000000000..4e378d87436 --- /dev/null +++ b/ecmascript/transforms/compat/tests/fixture/classes/create-super-1/output.js @@ -0,0 +1,10 @@ +let Foo = function(UnknownNativeClass) { + "use strict"; + _inherits(Foo, UnknownNativeClass); + var _super = _createSuper(Foo); + function Foo() { + _classCallCheck(this, Foo); + return _super.apply(this, arguments); + } + return Foo; +}(UnknownNativeClass); diff --git a/ecmascript/transforms/tests/es2015_function_name.rs b/ecmascript/transforms/tests/es2015_function_name.rs index 7e5e048deb1..f5e61bc45ad 100644 --- a/ecmascript/transforms/tests/es2015_function_name.rs +++ b/ecmascript/transforms/tests/es2015_function_name.rs @@ -304,10 +304,10 @@ let Login = function (_Component) { 'use strict'; _inherits(Login, _Component); - + var _super = _createSuper(Login); function Login() { _classCallCheck(this, Login); - return _possibleConstructorReturn(this, _getPrototypeOf(Login).apply(this, arguments)); + return _super.apply(this, arguments); } _createClass(Login, [{