Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix super in class fields. #7659

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 4 additions & 42 deletions packages/babel-plugin-proposal-class-properties/src/index.js
Expand Up @@ -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();
}
},
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

//

Expand Down
Expand Up @@ -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";
};
@@ -0,0 +1,6 @@
class A {
force = force;
foo = super.method();

constructor(force) {}
}
@@ -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)
});
};
Expand Up @@ -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";
};
Expand Up @@ -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
});
}

}
Expand All @@ -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 () {
Expand Down