Don't fold class property calls (#7814)

This undoes the property call folding from #6656.

It complicates the private property transforms, since they boil down to `map.set(this, vlaue)` and we definitely don't want the next call define a property on the map.

/cc @Andarist
This commit is contained in:
Justin Ridgewell 2018-04-26 16:06:18 -04:00 committed by Brian Ng
parent 224ce38882
commit 725e6a01c0
5 changed files with 32 additions and 33 deletions

View File

@ -51,26 +51,19 @@ export default declare((api, options) => {
environmentVisitor, environmentVisitor,
]); ]);
const foldDefinePropertyCalls = nodes =>
t.expressionStatement(
nodes.reduce((folded, node) => {
// update defineProperty's obj argument
node.arguments[0] = folded;
return node;
}),
);
const buildClassPropertySpec = ( const buildClassPropertySpec = (
ref, ref,
{ key, value, computed }, { key, value, computed },
scope, scope,
state, state,
) => { ) => {
return t.callExpression(state.addHelper("defineProperty"), [ return t.expressionStatement(
ref, t.callExpression(state.addHelper("defineProperty"), [
t.isIdentifier(key) && !computed ? t.stringLiteral(key.name) : key, ref,
value || scope.buildUndefinedNode(), t.isIdentifier(key) && !computed ? t.stringLiteral(key.name) : key,
]); value || scope.buildUndefinedNode(),
]),
);
}; };
const buildClassPropertyLoose = (ref, { key, value, computed }, scope) => { const buildClassPropertyLoose = (ref, { key, value, computed }, scope) => {
@ -168,16 +161,7 @@ export default declare((api, options) => {
} }
} }
const afterNodes =
!loose && staticNodes.length
? foldDefinePropertyCalls(staticNodes)
: staticNodes;
if (instanceBody.length) { if (instanceBody.length) {
const assignments = loose
? instanceBody
: foldDefinePropertyCalls(instanceBody);
if (!constructor) { if (!constructor) {
const newConstructor = t.classMethod( const newConstructor = t.classMethod(
"constructor", "constructor",
@ -210,10 +194,10 @@ export default declare((api, options) => {
const bareSupers = []; const bareSupers = [];
constructor.traverse(findBareSupers, bareSupers); constructor.traverse(findBareSupers, bareSupers);
for (const bareSuper of bareSupers) { for (const bareSuper of bareSupers) {
bareSuper.insertAfter(assignments); bareSuper.insertAfter(instanceBody);
} }
} else { } else {
constructor.get("body").unshiftContainer("body", assignments); constructor.get("body").unshiftContainer("body", instanceBody);
} }
} }
@ -221,7 +205,7 @@ export default declare((api, options) => {
prop.remove(); prop.remove();
} }
if (computedNodes.length === 0 && afterNodes.length === 0) return; if (computedNodes.length === 0 && staticNodes.length === 0) return;
if (path.isClassExpression()) { if (path.isClassExpression()) {
path.scope.push({ id: ref }); path.scope.push({ id: ref });
@ -234,7 +218,7 @@ export default declare((api, options) => {
} }
path.insertBefore(computedNodes); path.insertBefore(computedNodes);
path.insertAfter(afterNodes); path.insertAfter(staticNodes);
}, },
}, },
}; };

View File

@ -3,7 +3,9 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
class C { class C {
// Output should not use `_initialiseProps` // Output should not use `_initialiseProps`
constructor(T) { constructor(T) {
_defineProperty(_defineProperty(this, "x", void 0), "y", 0); _defineProperty(this, "x", void 0);
_defineProperty(this, "y", 0);
} }
} }

View File

@ -28,7 +28,15 @@ function () {
function MyClass() { function MyClass() {
babelHelpers.classCallCheck(this, MyClass); babelHelpers.classCallCheck(this, MyClass);
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.defineProperty(this, null, "null");
babelHelpers.defineProperty(this, _undefined, "undefined");
babelHelpers.defineProperty(this, void 0, "void 0");
babelHelpers.defineProperty(this, _ref3, "regex");
babelHelpers.defineProperty(this, foo, "foo");
babelHelpers.defineProperty(this, _bar, "bar");
babelHelpers.defineProperty(this, _baz, "baz");
babelHelpers.defineProperty(this, `template`, "template");
babelHelpers.defineProperty(this, _ref4, "template-with-expression");
} }
babelHelpers.createClass(MyClass, [{ babelHelpers.createClass(MyClass, [{
@ -51,4 +59,7 @@ function () {
return MyClass; return MyClass;
}(); }();
babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(babelHelpers.defineProperty(MyClass, _one, "test"), 2 * 4 + 7, "247"), 2 * four + 7, "247"), _ref, "247"); babelHelpers.defineProperty(MyClass, _one, "test");
babelHelpers.defineProperty(MyClass, 2 * 4 + 7, "247");
babelHelpers.defineProperty(MyClass, 2 * four + 7, "247");
babelHelpers.defineProperty(MyClass, _ref, "247");

View File

@ -2,5 +2,6 @@ var A = function A(_force) {
"use strict"; "use strict";
babelHelpers.classCallCheck(this, A); babelHelpers.classCallCheck(this, A);
babelHelpers.defineProperty(babelHelpers.defineProperty(this, "force", force), "foo", babelHelpers.get(babelHelpers.getPrototypeOf(A.prototype), "method", this).call(this)); babelHelpers.defineProperty(this, "force", force);
babelHelpers.defineProperty(this, "foo", babelHelpers.get(babelHelpers.getPrototypeOf(A.prototype), "method", this).call(this));
}; };

View File

@ -39,9 +39,10 @@ var _this = this;
constructor(_force) { constructor(_force) {
var _this4 = this; var _this4 = this;
babelHelpers.defineProperty(babelHelpers.defineProperty(this, "fn", function () { babelHelpers.defineProperty(this, "fn", function () {
return console.log(_this4); return console.log(_this4);
}), "force", force); });
babelHelpers.defineProperty(this, "force", force);
} }
} }