fix no RHS in class properties causing undefined to be used instead of a noop - fixes #1396

This commit is contained in:
Sebastian McKenzie 2015-04-30 23:09:17 +01:00
parent 571cb4928f
commit d26e7ad577
9 changed files with 21 additions and 25 deletions

View File

@ -24,7 +24,7 @@
}
}
if (descriptor.initializer) {
if (descriptor.initializer !== undefined) {
initializers[key] = descriptor;
continue;
}

View File

@ -26,9 +26,10 @@
}
}
descriptor.value = descriptor.initializer.call(target);
Object.defineProperty(target, key, descriptor);
if (descriptor.initializer) {
descriptor.value = descriptor.initializer.call(target);
Object.defineProperty(target, key, descriptor);
}
}
return target;

View File

@ -6,6 +6,7 @@
for (var _key in _descriptor) descriptor[_key] = _descriptor[_key];
// initialize it
if (!descriptor.initializer) return;
descriptor.value = descriptor.initializer.call(target);
Object.defineProperty(target, key, descriptor);

View File

@ -550,8 +550,12 @@ class ClassTransformer {
if (node.decorators) {
var body = [];
if (node.value) body.push(t.returnStatement(node.value));
node.value = t.functionExpression(null, [], t.blockStatement(body));
if (node.value) {
body.push(t.returnStatement(node.value));
node.value = t.functionExpression(null, [], t.blockStatement(body));
} else {
node.value = t.literal(null);
}
this.pushToMap(node, true, "initializer");
var initializers;
@ -575,7 +579,7 @@ class ClassTransformer {
])
));
} else {
node.value = node.value || t.identifier("undefined");
if (!node.value && !node.decorators) return;
if (node.static) {
// can just be added to the static map

View File

@ -2,5 +2,4 @@
var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo);
this.bar = undefined;
};
};

View File

@ -2,5 +2,5 @@ class Foo {
static num;
}
assert.equal("num" in Foo, true);
assert.equal("num" in Foo, false);
assert.equal(Foo.num, undefined);

View File

@ -1,14 +1,5 @@
"use strict";
var Foo = (function () {
function Foo() {
babelHelpers.classCallCheck(this, Foo);
}
babelHelpers.createClass(Foo, null, [{
key: "bar",
value: undefined,
enumerable: true
}]);
return Foo;
})();
var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo);
};

View File

@ -11,8 +11,8 @@ var Foo = (function () {
babelHelpers.createDecoratedClass(Foo, [{
key: "foo",
decorators: [bar],
initializer: function () {},
initializer: null,
enumerable: true
}], null, _instanceInitializers);
return Foo;
})();
})();

View File

@ -10,7 +10,7 @@ var Foo = (function () {
babelHelpers.createDecoratedClass(Foo, null, [{
key: "foo",
decorators: [bar],
initializer: function () {},
initializer: null,
enumerable: true
}], null, _staticInitializers);
babelHelpers.defineDecoratedPropertyDescriptor(Foo, "foo", _staticInitializers);