From 5166eef103fd18bbb3f498cbf7065866d9c1ea83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 15 Apr 2018 07:03:47 +0200 Subject: [PATCH] Optimize class properties output (#6656) --- .../src/index.js | 59 +++++++++----- .../output.js | 15 +--- .../nested-class/super-call-in-key/output.js | 7 +- .../super-property-in-key/output.js | 7 +- .../test/fixtures/public/computed/output.js | 80 +------------------ .../public/constructor-collision/output.js | 7 +- .../test/fixtures/public/derived/output.js | 7 +- .../test/fixtures/public/foobar/output.js | 9 +-- .../public/instance-computed/output.js | 7 +- .../public/instance-undefined/output.js | 7 +- .../test/fixtures/public/instance/output.js | 7 +- .../public/non-block-arrow-func/output.mjs | 11 +-- .../public/regression-T2983/output.mjs | 14 +--- .../public/regression-T6719/output.js | 17 ++-- .../public/regression-T7364/output.mjs | 57 ++++--------- .../fixtures/public/static-export/output.mjs | 14 +--- .../public/static-infer-name/output.js | 7 +- .../public/static-undefined/output.js | 7 +- .../test/fixtures/public/static/output.js | 7 +- .../test/fixtures/public/super-call/output.js | 7 +- .../public/super-expression/output.js | 7 +- .../fixtures/public/super-statement/output.js | 7 +- .../public/super-with-collision/output.js | 13 +-- .../test/fixtures/regression/6153/output.js | 80 ++++--------------- .../test/fixtures/regression/6154/output.js | 20 ++--- .../test/fixtures/regression/7371/output.js | 35 ++------ .../regression/6057-expanded/output.js | 9 +-- 27 files changed, 120 insertions(+), 404 deletions(-) diff --git a/packages/babel-plugin-proposal-class-properties/src/index.js b/packages/babel-plugin-proposal-class-properties/src/index.js index f9cab17b7b..1cf2f2d093 100644 --- a/packages/babel-plugin-proposal-class-properties/src/index.js +++ b/packages/babel-plugin-proposal-class-properties/src/index.js @@ -51,19 +51,26 @@ export default declare((api, options) => { environmentVisitor, ]); - const buildClassPropertySpec = (ref, { key, value, computed }, scope) => { - return template.statement` - Object.defineProperty(REF, KEY, { - configurable: true, - enumerable: true, - writable: true, - value: VALUE - }); - `({ - REF: t.cloneNode(ref), - KEY: t.isIdentifier(key) && !computed ? t.stringLiteral(key.name) : key, - VALUE: value || scope.buildUndefinedNode(), - }); + const foldDefinePropertyCalls = nodes => + t.expressionStatement( + nodes.reduce((folded, node) => { + // update defineProperty's obj argument + node.arguments[0] = folded; + return node; + }), + ); + + const buildClassPropertySpec = ( + ref, + { key, value, computed }, + scope, + state, + ) => { + return t.callExpression(state.addHelper("defineProperty"), [ + ref, + t.isIdentifier(key) && !computed ? t.stringLiteral(key.name) : key, + value || scope.buildUndefinedNode(), + ]); }; const buildClassPropertyLoose = (ref, { key, value, computed }, scope) => { @@ -85,7 +92,7 @@ export default declare((api, options) => { inherits: syntaxClassProperties, visitor: { - Class(path) { + Class(path, state) { const isDerived = !!path.node.superClass; let constructor; const props = []; @@ -146,17 +153,31 @@ export default declare((api, options) => { if (propNode.decorators && propNode.decorators.length > 0) continue; if (propNode.static) { - staticNodes.push(buildClassProperty(ref, propNode, path.scope)); + staticNodes.push( + buildClassProperty(ref, propNode, path.scope, state), + ); } else { instanceBody.push( - buildClassProperty(t.thisExpression(), propNode, path.scope), + buildClassProperty( + t.thisExpression(), + propNode, + path.scope, + state, + ), ); } } - const afterNodes = [...staticNodes]; + const afterNodes = + !loose && staticNodes.length + ? foldDefinePropertyCalls(staticNodes) + : staticNodes; if (instanceBody.length) { + const assignments = loose + ? instanceBody + : foldDefinePropertyCalls(instanceBody); + if (!constructor) { const newConstructor = t.classMethod( "constructor", @@ -186,10 +207,10 @@ export default declare((api, options) => { const bareSupers = []; constructor.traverse(findBareSupers, bareSupers); for (const bareSuper of bareSupers) { - bareSuper.insertAfter(instanceBody); + bareSuper.insertAfter(assignments); } } else { - constructor.get("body").unshiftContainer("body", instanceBody); + constructor.get("body").unshiftContainer("body", assignments); } } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/compile-to-class/constructor-collision-ignores-types/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/compile-to-class/constructor-collision-ignores-types/output.js index cb2c951cf9..73806d8db8 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/compile-to-class/constructor-collision-ignores-types/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/compile-to-class/constructor-collision-ignores-types/output.js @@ -1,18 +1,9 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + class C { // Output should not use `_initialiseProps` constructor(T) { - Object.defineProperty(this, "x", { - configurable: true, - enumerable: true, - writable: true, - value: void 0 - }); - Object.defineProperty(this, "y", { - configurable: true, - enumerable: true, - writable: true, - value: 0 - }); + _defineProperty(_defineProperty(this, "x", void 0), "y", 0); } } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-call-in-key/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-call-in-key/output.js index f7f591a606..2ded153f1e 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-call-in-key/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-call-in-key/output.js @@ -24,12 +24,7 @@ function (_Hello) { let Inner = function Inner() { babelHelpers.classCallCheck(this, Inner); - Object.defineProperty(this, _this2, { - configurable: true, - enumerable: true, - writable: true, - value: "hello" - }); + babelHelpers.defineProperty(this, _this2, "hello"); }; return babelHelpers.possibleConstructorReturn(_this, new Inner()); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-property-in-key/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-property-in-key/output.js index 58d0250eb2..f5417c7071 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-property-in-key/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/nested-class/super-property-in-key/output.js @@ -31,12 +31,7 @@ function (_Hello) { let Inner = function Inner() { babelHelpers.classCallCheck(this, Inner); - Object.defineProperty(this, _babelHelpers$get$cal, { - configurable: true, - enumerable: true, - writable: true, - value: 'hello' - }); + babelHelpers.defineProperty(this, _babelHelpers$get$cal, 'hello'); }; return babelHelpers.possibleConstructorReturn(_this, new Inner()); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/computed/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/computed/output.js index 155ee19677..7005002d00 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/computed/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/computed/output.js @@ -28,60 +28,7 @@ function () { function MyClass() { babelHelpers.classCallCheck(this, MyClass); - Object.defineProperty(this, null, { - configurable: true, - enumerable: true, - writable: true, - value: "null" - }); - Object.defineProperty(this, _undefined, { - configurable: true, - enumerable: true, - writable: true, - value: "undefined" - }); - Object.defineProperty(this, void 0, { - configurable: true, - enumerable: true, - writable: true, - value: "void 0" - }); - Object.defineProperty(this, _ref3, { - configurable: true, - enumerable: true, - writable: true, - value: "regex" - }); - Object.defineProperty(this, foo, { - configurable: true, - enumerable: true, - writable: true, - value: "foo" - }); - Object.defineProperty(this, _bar, { - configurable: true, - enumerable: true, - writable: true, - value: "bar" - }); - Object.defineProperty(this, _baz, { - configurable: true, - enumerable: true, - writable: true, - value: "baz" - }); - Object.defineProperty(this, `template`, { - configurable: true, - enumerable: true, - writable: true, - value: "template" - }); - Object.defineProperty(this, _ref4, { - configurable: true, - enumerable: true, - writable: true, - value: "template-with-expression" - }); + babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(this, null, "null"), _undefined, "undefined"), void 0, "void 0"), _ref3, "regex"), foo, "foo"), _bar, "bar"), _baz, "baz"), `template`, "template"), _ref4, "template-with-expression"); } babelHelpers.createClass(MyClass, [{ @@ -104,27 +51,4 @@ function () { return MyClass; }(); -Object.defineProperty(MyClass, _one, { - configurable: true, - enumerable: true, - writable: true, - value: "test" -}); -Object.defineProperty(MyClass, 2 * 4 + 7, { - configurable: true, - enumerable: true, - writable: true, - value: "247" -}); -Object.defineProperty(MyClass, 2 * four + 7, { - configurable: true, - enumerable: true, - writable: true, - value: "247" -}); -Object.defineProperty(MyClass, _ref, { - configurable: true, - enumerable: true, - writable: true, - value: "247" -}); +babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(MyClass, _one, "test"), 2 * 4 + 7, "247"), 2 * four + 7, "247"), _ref, "247"); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/constructor-collision/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/constructor-collision/output.js index cd3bee9780..5565530ee7 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/constructor-collision/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/constructor-collision/output.js @@ -4,11 +4,6 @@ var Foo = function Foo() { "use strict"; babelHelpers.classCallCheck(this, Foo); - Object.defineProperty(this, "bar", { - configurable: true, - enumerable: true, - writable: true, - value: foo - }); + babelHelpers.defineProperty(this, "bar", foo); var _foo = "foo"; }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived/output.js index 29668c34fd..ac721f7012 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/derived/output.js @@ -9,12 +9,7 @@ function (_Bar) { var _temp, _this; babelHelpers.classCallCheck(this, Foo); - return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this, ...args)), Object.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", { - configurable: true, - enumerable: true, - writable: true, - value: "foo" - }), _temp)); + return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this, ...args)), babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo"), _temp)); } return Foo; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/output.js index db8ae705a9..3e89c9938b 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/foobar/output.js @@ -10,13 +10,8 @@ function (_Parent) { babelHelpers.classCallCheck(this, Child); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Child).call(this)); - Object.defineProperty(babelHelpers.assertThisInitialized(_this), "scopedFunctionWithThis", { - configurable: true, - enumerable: true, - writable: true, - value: function value() { - _this.name = {}; - } + babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "scopedFunctionWithThis", function () { + _this.name = {}; }); return _this; } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance-computed/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance-computed/output.js index 7edfd976f9..79bfad744a 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance-computed/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance-computed/output.js @@ -5,12 +5,7 @@ function test(x) { "use strict"; babelHelpers.classCallCheck(this, F); - Object.defineProperty(this, _x, { - configurable: true, - enumerable: true, - writable: true, - value: 1 - }); + babelHelpers.defineProperty(this, _x, 1); }; x = 'deadbeef'; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance-undefined/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance-undefined/output.js index e9996a624b..edbb054f5c 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance-undefined/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance-undefined/output.js @@ -2,10 +2,5 @@ var Foo = function Foo() { "use strict"; babelHelpers.classCallCheck(this, Foo); - Object.defineProperty(this, "bar", { - configurable: true, - enumerable: true, - writable: true, - value: void 0 - }); + babelHelpers.defineProperty(this, "bar", void 0); }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance/output.js index ef0a7c734e..49354de8a6 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/instance/output.js @@ -2,10 +2,5 @@ var Foo = function Foo() { "use strict"; babelHelpers.classCallCheck(this, Foo); - Object.defineProperty(this, "bar", { - configurable: true, - enumerable: true, - writable: true, - value: "foo" - }); + babelHelpers.defineProperty(this, "bar", "foo"); }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/non-block-arrow-func/output.mjs b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/non-block-arrow-func/output.mjs index ad3428a628..b5be8d5c80 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/non-block-arrow-func/output.mjs +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/non-block-arrow-func/output.mjs @@ -15,13 +15,8 @@ export default (param => { } }]); return App; - }(), Object.defineProperty(_class, "props", { - configurable: true, - enumerable: true, - writable: true, - value: { - prop1: 'prop1', - prop2: 'prop2' - } + }(), babelHelpers.defineProperty(_class, "props", { + prop1: 'prop1', + prop2: 'prop2' }), _temp; }); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T2983/output.mjs b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T2983/output.mjs index 8eadb530f3..9258b33682 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T2983/output.mjs +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T2983/output.mjs @@ -2,22 +2,12 @@ var _class, _temp; call((_temp = _class = function _class() { babelHelpers.classCallCheck(this, _class); -}, Object.defineProperty(_class, "test", { - configurable: true, - enumerable: true, - writable: true, - value: true -}), _temp)); +}, babelHelpers.defineProperty(_class, "test", true), _temp)); var _default = function _default() { babelHelpers.classCallCheck(this, _default); }; -Object.defineProperty(_default, "test", { - configurable: true, - enumerable: true, - writable: true, - value: true -}); +babelHelpers.defineProperty(_default, "test", true); export { _default as default }; ; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T6719/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T6719/output.js index 4e45d2b07f..4e17608440 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T6719/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T6719/output.js @@ -14,16 +14,11 @@ function withContext(ComposedComponent) { } return WithContext; - }(Component), Object.defineProperty(_class, "propTypes", { - configurable: true, - enumerable: true, - writable: true, - value: { - context: PropTypes.shape({ - addCss: PropTypes.func, - setTitle: PropTypes.func, - setMeta: PropTypes.func - }) - } + }(Component), babelHelpers.defineProperty(_class, "propTypes", { + context: PropTypes.shape({ + addCss: PropTypes.func, + setTitle: PropTypes.func, + setMeta: PropTypes.func + }) }), _temp; } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T7364/output.mjs b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T7364/output.mjs index 82f5a80971..29773405f1 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T7364/output.mjs +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/regression-T7364/output.mjs @@ -2,20 +2,11 @@ class MyClass { constructor() { var _this = this; - Object.defineProperty(this, "myAsyncMethod", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - var _ref = babelHelpers.asyncToGenerator(function* () { - console.log(_this); - }); - - return function value() { - return _ref.apply(this, arguments); - }; - }() - }); + babelHelpers.defineProperty(this, "myAsyncMethod", + /*#__PURE__*/ + babelHelpers.asyncToGenerator(function* () { + console.log(_this); + })); } } @@ -24,20 +15,11 @@ class MyClass { constructor() { var _this2 = this; - Object.defineProperty(this, "myAsyncMethod", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - var _ref2 = babelHelpers.asyncToGenerator(function* () { - console.log(_this2); - }); - - return function value() { - return _ref2.apply(this, arguments); - }; - }() - }); + babelHelpers.defineProperty(this, "myAsyncMethod", + /*#__PURE__*/ + babelHelpers.asyncToGenerator(function* () { + console.log(_this2); + })); } }); @@ -46,20 +28,11 @@ export default class MyClass3 { constructor() { var _this3 = this; - Object.defineProperty(this, "myAsyncMethod", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - var _ref3 = babelHelpers.asyncToGenerator(function* () { - console.log(_this3); - }); - - return function value() { - return _ref3.apply(this, arguments); - }; - }() - }); + babelHelpers.defineProperty(this, "myAsyncMethod", + /*#__PURE__*/ + babelHelpers.asyncToGenerator(function* () { + console.log(_this3); + })); } } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-export/output.mjs b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-export/output.mjs index f8c161de0b..44433cdf1d 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-export/output.mjs +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-export/output.mjs @@ -1,21 +1,11 @@ export var MyClass = function MyClass() { babelHelpers.classCallCheck(this, MyClass); }; -Object.defineProperty(MyClass, "property", { - configurable: true, - enumerable: true, - writable: true, - value: value -}); +babelHelpers.defineProperty(MyClass, "property", value); var MyClass2 = function MyClass2() { babelHelpers.classCallCheck(this, MyClass2); }; -Object.defineProperty(MyClass2, "property", { - configurable: true, - enumerable: true, - writable: true, - value: value -}); +babelHelpers.defineProperty(MyClass2, "property", value); export { MyClass2 as default }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-infer-name/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-infer-name/output.js index 2b91078682..414738b8ec 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-infer-name/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-infer-name/output.js @@ -4,9 +4,4 @@ var Foo = (_temp = _class = function Foo() { "use strict"; babelHelpers.classCallCheck(this, Foo); -}, Object.defineProperty(_class, "num", { - configurable: true, - enumerable: true, - writable: true, - value: 0 -}), _temp); +}, babelHelpers.defineProperty(_class, "num", 0), _temp); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-undefined/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-undefined/output.js index efcc95d4ed..848e33ee9e 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-undefined/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static-undefined/output.js @@ -4,9 +4,4 @@ var Foo = function Foo() { babelHelpers.classCallCheck(this, Foo); }; -Object.defineProperty(Foo, "bar", { - configurable: true, - enumerable: true, - writable: true, - value: void 0 -}); +babelHelpers.defineProperty(Foo, "bar", void 0); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static/output.js index 34b428919a..baf5bf94d4 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/static/output.js @@ -4,9 +4,4 @@ var Foo = function Foo() { babelHelpers.classCallCheck(this, Foo); }; -Object.defineProperty(Foo, "bar", { - configurable: true, - enumerable: true, - writable: true, - value: "foo" -}); +babelHelpers.defineProperty(Foo, "bar", "foo"); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-call/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-call/output.js index 526591d0ef..60b2a4e8ea 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-call/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-call/output.js @@ -27,12 +27,7 @@ function (_A) { var _temp, _this; babelHelpers.classCallCheck(this, B); - return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(B).call(this, ...args)), Object.defineProperty(babelHelpers.assertThisInitialized(_this), "foo", { - configurable: true, - enumerable: true, - writable: true, - value: babelHelpers.get(babelHelpers.getPrototypeOf(B.prototype), "foo", babelHelpers.assertThisInitialized(_this)).call(_this) - }), _temp)); + return babelHelpers.possibleConstructorReturn(_this, (_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(B).call(this, ...args)), babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "foo", babelHelpers.get(babelHelpers.getPrototypeOf(B.prototype), "foo", babelHelpers.assertThisInitialized(_this)).call(_this)), _temp)); } return B; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-expression/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-expression/output.js index c47e317152..be7f0ef682 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-expression/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-expression/output.js @@ -9,12 +9,7 @@ function (_Bar) { var _temp, _this; babelHelpers.classCallCheck(this, Foo); - foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)), Object.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", { - configurable: true, - enumerable: true, - writable: true, - value: "foo" - }), _temp)); + foo((_temp = _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)), babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo"), _temp)); return _this; } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-statement/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-statement/output.js index 87c98cac68..ef101c0bee 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-statement/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-statement/output.js @@ -10,12 +10,7 @@ function (_Bar) { babelHelpers.classCallCheck(this, Foo); _this = babelHelpers.possibleConstructorReturn(this, babelHelpers.getPrototypeOf(Foo).call(this)); - Object.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", { - configurable: true, - enumerable: true, - writable: true, - value: "foo" - }); + babelHelpers.defineProperty(babelHelpers.assertThisInitialized(_this), "bar", "foo"); return _this; } diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-with-collision/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-with-collision/output.js index 08c52c6d10..2e53db3b98 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-with-collision/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/public/super-with-collision/output.js @@ -2,16 +2,5 @@ var A = function A(_force) { "use strict"; babelHelpers.classCallCheck(this, A); - Object.defineProperty(this, "force", { - configurable: true, - enumerable: true, - writable: true, - value: force - }); - Object.defineProperty(this, "foo", { - configurable: true, - enumerable: true, - writable: true, - value: babelHelpers.get(babelHelpers.getPrototypeOf(A.prototype), "method", babelHelpers.assertThisInitialized(this)).call(this) - }); + babelHelpers.defineProperty(babelHelpers.defineProperty(this, "force", force), "foo", babelHelpers.get(babelHelpers.getPrototypeOf(A.prototype), "method", babelHelpers.assertThisInitialized(this)).call(this)); }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6153/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6153/output.js index 88dbbebbec..d3e7f955c3 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6153/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6153/output.js @@ -5,25 +5,15 @@ var _this = this; constructor() { var _this2 = this; - Object.defineProperty(this, "fn", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - return console.log(_this2); - } + babelHelpers.defineProperty(this, "fn", function () { + return console.log(_this2); }); } } - Object.defineProperty(Foo, "fn", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - return console.log(_this); - } + babelHelpers.defineProperty(Foo, "fn", function () { + return console.log(_this); }); }); @@ -34,23 +24,13 @@ var _this = this; constructor() { var _this3 = this; - Object.defineProperty(this, "fn", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - return console.log(_this3); - } + babelHelpers.defineProperty(this, "fn", function () { + return console.log(_this3); }); } - }, Object.defineProperty(_class, "fn", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - return console.log(_this); - } + }, babelHelpers.defineProperty(_class, "fn", function () { + return console.log(_this); }), _temp; }); @@ -59,31 +39,15 @@ var _this = this; constructor(_force) { var _this4 = this; - Object.defineProperty(this, "fn", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - return console.log(_this4); - } - }); - Object.defineProperty(this, "force", { - configurable: true, - enumerable: true, - writable: true, - value: force - }); + babelHelpers.defineProperty(babelHelpers.defineProperty(this, "fn", function () { + return console.log(_this4); + }), "force", force); } } - Object.defineProperty(Baz, "fn", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - return console.log(_this); - } + babelHelpers.defineProperty(Baz, "fn", function () { + return console.log(_this); }); }); @@ -94,24 +58,14 @@ var qux = function () { constructor() { var _this5 = this; - Object.defineProperty(this, "fn", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - return console.log(_this5); - } + babelHelpers.defineProperty(this, "fn", function () { + return console.log(_this5); }); } } - Object.defineProperty(Qux, "fn", { - configurable: true, - enumerable: true, - writable: true, - value: function () { - return console.log(_this6); - } + babelHelpers.defineProperty(Qux, "fn", function () { + return console.log(_this6); }); }.bind(this); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js index 448afa8d78..a3c541a9b4 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js @@ -8,6 +8,8 @@ function _inherits(subClass, superClass) { if (typeof superClass !== "function" function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); } @@ -41,25 +43,15 @@ var Test = function Test() { args[_key] = arguments[_key]; } - return _possibleConstructorReturn(_this, (_temp = _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Other)).call.apply(_getPrototypeOf2, [this].concat(args))), Object.defineProperty(_assertThisInitialized(_this), "a", { - configurable: true, - enumerable: true, - writable: true, - value: function value() { - return _get(_getPrototypeOf(Other.prototype), "test", _assertThisInitialized(_this)); - } + return _possibleConstructorReturn(_this, (_temp = _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Other)).call.apply(_getPrototypeOf2, [this].concat(args))), _defineProperty(_assertThisInitialized(_this), "a", function () { + return _get(_getPrototypeOf(Other.prototype), "test", _assertThisInitialized(_this)); }), _temp)); } return Other; }(Test); - Object.defineProperty(Other, "a", { - configurable: true, - enumerable: true, - writable: true, - value: function value() { - return _get(_getPrototypeOf(Test.prototype), "test", _assertThisInitialized(_this2)); - } + _defineProperty(Other, "a", function () { + return _get(_getPrototypeOf(Test.prototype), "test", _assertThisInitialized(_this2)); }); }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/7371/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/7371/output.js index 965640e31e..5c6ac57eeb 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/7371/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/7371/output.js @@ -5,12 +5,7 @@ class C {} class A extends C { constructor() { super(); - Object.defineProperty(this, "field", { - configurable: true, - enumerable: true, - writable: true, - value: 1 - }); + babelHelpers.defineProperty(this, "field", 1); class B extends C { constructor() { @@ -40,12 +35,7 @@ class SuperClass extends Obj { constructor() { var _temp; - class B extends ((_temp = super(), Object.defineProperty(this, "field", { - configurable: true, - enumerable: true, - writable: true, - value: 1 - }), _temp), Obj) { + class B extends ((_temp = super(), babelHelpers.defineProperty(this, "field", 1), _temp), Obj) { constructor() { super(); expect(this.field).toBeUndefined(); @@ -71,12 +61,7 @@ class ComputedMethod extends Obj { expect(this.field).toBeUndefined(); } - [(_temp2 = super(), Object.defineProperty(this, "field", { - configurable: true, - enumerable: true, - writable: true, - value: 1 - }), _temp2)]() {} + [(_temp2 = super(), babelHelpers.defineProperty(this, "field", 1), _temp2)]() {} } @@ -92,22 +77,12 @@ class ComputedField extends Obj { constructor() { var _temp3; - var _ref = (_temp3 = super(), Object.defineProperty(this, "field", { - configurable: true, - enumerable: true, - writable: true, - value: 1 - }), _temp3); + var _ref = (_temp3 = super(), babelHelpers.defineProperty(this, "field", 1), _temp3); class B extends Obj { constructor() { super(); - Object.defineProperty(this, _ref, { - configurable: true, - enumerable: true, - writable: true, - value: 1 - }); + babelHelpers.defineProperty(this, _ref, 1); expect(this.field).toBeUndefined(); } diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js index 01a7ef308a..226b64074b 100644 --- a/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js +++ b/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js @@ -29,6 +29,8 @@ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || func function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + var App = /*#__PURE__*/ function (_Component) { @@ -45,12 +47,7 @@ function (_Component) { args[_key] = arguments[_key]; } - return _possibleConstructorReturn(_this, (_temp = _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(App)).call.apply(_getPrototypeOf2, [this].concat(args))), Object.defineProperty(_assertThisInitialized(_this), "exportType", { - configurable: true, - enumerable: true, - writable: true, - value: '' - }), _temp)); + return _possibleConstructorReturn(_this, (_temp = _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(App)).call.apply(_getPrototypeOf2, [this].concat(args))), _defineProperty(_assertThisInitialized(_this), "exportType", ''), _temp)); } _createClass(App, [{