diff --git a/packages/babel-plugin-proposal-class-properties/src/index.js b/packages/babel-plugin-proposal-class-properties/src/index.js index a826b310ea19..a0708398363d 100644 --- a/packages/babel-plugin-proposal-class-properties/src/index.js +++ b/packages/babel-plugin-proposal-class-properties/src/index.js @@ -22,7 +22,7 @@ export default declare((api, options) => { }, ReferencedIdentifier(path) { if (this.scope.hasOwnBinding(path.node.name)) { - this.collision = true; + this.scope.rename(path.node.name); path.skip(); } }, @@ -115,7 +115,7 @@ export default declare((api, options) => { const computedNodes = []; const staticNodes = []; - let instanceBody = []; + const instanceBody = []; for (const computedPath of computedPaths) { const computedNode = computedPath.node; @@ -175,46 +175,8 @@ export default declare((api, options) => { [constructor] = body.unshiftContainer("body", newConstructor); } - const collisionState = { - collision: false, - scope: constructor.scope, - }; - - for (const prop of props) { - prop.traverse(referenceVisitor, collisionState); - if (collisionState.collision) break; - } - - if (collisionState.collision) { - const initialisePropsRef = path.scope.generateUidIdentifier( - "initialiseProps", - ); - - afterNodes.push( - t.variableDeclaration("var", [ - t.variableDeclarator( - initialisePropsRef, - t.functionExpression( - null, - [], - t.blockStatement(instanceBody), - ), - ), - ]), - ); - - instanceBody = [ - t.expressionStatement( - t.callExpression( - t.memberExpression( - t.cloneNode(initialisePropsRef), - t.identifier("call"), - ), - [t.thisExpression()], - ), - ), - ]; - } + const state = { scope: constructor.scope }; + for (const prop of props) prop.traverse(referenceVisitor, state); // diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/general/constructor-collision/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/general/constructor-collision/output.js index 5e865ec40215..9d80c3926cf2 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/general/constructor-collision/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/general/constructor-collision/output.js @@ -2,17 +2,11 @@ var foo = "bar"; var Foo = function Foo() { babelHelpers.classCallCheck(this, Foo); - - _initialiseProps.call(this); - - var foo = "foo"; -}; - -var _initialiseProps = function () { Object.defineProperty(this, "bar", { configurable: true, enumerable: true, writable: true, value: foo }); + var _foo = "foo"; }; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/general/super-with-collision/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/general/super-with-collision/input.js new file mode 100644 index 000000000000..a9552f3068e1 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/general/super-with-collision/input.js @@ -0,0 +1,6 @@ +class A { + force = force; + foo = super.method(); + + constructor(force) {} +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/general/super-with-collision/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/general/super-with-collision/output.js new file mode 100644 index 000000000000..3c06b4ed8cd3 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/general/super-with-collision/output.js @@ -0,0 +1,15 @@ +var A = function A(_force) { + 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(A.prototype.__proto__ || Object.getPrototypeOf(A.prototype), "method", babelHelpers.assertThisInitialized(this)).call(this) + }); +}; diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/loose/constructor-collision/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/loose/constructor-collision/output.js index 12af37359caf..b15e5d8f86c2 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/loose/constructor-collision/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/loose/constructor-collision/output.js @@ -2,12 +2,6 @@ var foo = "bar"; var Foo = function Foo() { babelHelpers.classCallCheck(this, Foo); - - _initialiseProps.call(this); - - var foo = "foo"; -}; - -var _initialiseProps = function () { this.bar = foo; + var _foo = "foo"; }; 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 12a888802d7e..88dbbebbec36 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 @@ -56,8 +56,23 @@ var _this = this; (function () { class Baz { - constructor(force) { - _initialiseProps.call(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 + }); } } @@ -70,25 +85,6 @@ var _this = this; return console.log(_this); } }); - - var _initialiseProps = function () { - 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 - }); - }; }); var qux = function () {