Greater spec compliance for class properties (#4544)

* Desugar class properties to Object.defineProperty per spec
* Define uninitialized static class properties with value=undefined
* Make class-properties increased spec compliance opt-in (spec: true)
This commit is contained in:
Moti Zilberman
2016-11-15 23:35:51 +02:00
committed by Henry Zhu
parent cf3a38fb40
commit f8ddd80f96
39 changed files with 466 additions and 11 deletions

View File

@@ -0,0 +1,9 @@
var foo = "bar";
class Foo {
bar = foo;
constructor() {
var foo = "foo";
}
}

View File

@@ -0,0 +1,17 @@
var foo = "bar";
var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo);
_initialiseProps.call(this);
var foo = "foo";
};
var _initialiseProps = function () {
Object.defineProperty(this, "bar", {
enumerable: true,
writable: true,
value: foo
});
};

View File

@@ -0,0 +1,3 @@
class Foo extends Bar {
bar = "foo";
}

View File

@@ -0,0 +1,16 @@
var Foo = function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo(...args) {
var _temp, _this, _ret;
babelHelpers.classCallCheck(this, Foo);
return _ret = (_temp = (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, ...args)), _this), Object.defineProperty(_this, "bar", {
enumerable: true,
writable: true,
value: "foo"
}), _temp), babelHelpers.possibleConstructorReturn(_this, _ret);
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,9 @@
class Child extends Parent {
constructor() {
super();
}
scopedFunctionWithThis = () => {
this.name = {};
}
}

View File

@@ -0,0 +1,22 @@
"use strict";
var Child = function (_Parent) {
babelHelpers.inherits(Child, _Parent);
function Child() {
babelHelpers.classCallCheck(this, Child);
var _this = babelHelpers.possibleConstructorReturn(this, (Child.__proto__ || Object.getPrototypeOf(Child)).call(this));
Object.defineProperty(_this, "scopedFunctionWithThis", {
enumerable: true,
writable: true,
value: function value() {
_this.name = {};
}
});
return _this;
}
return Child;
}(Parent);

View File

@@ -0,0 +1,4 @@
{
"plugins": ["external-helpers", ["transform-class-properties", { "spec": true }]],
"presets": ["stage-0", "es2015"]
}

View File

@@ -0,0 +1,3 @@
class Foo {
bar;
}

View File

@@ -0,0 +1,3 @@
var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo);
};

View File

@@ -0,0 +1,3 @@
class Foo {
bar = "foo";
}

View File

@@ -0,0 +1,8 @@
var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo);
Object.defineProperty(this, "bar", {
enumerable: true,
writable: true,
value: "foo"
});
};

View File

@@ -0,0 +1,11 @@
export default param =>
class App {
static props = {
prop1: 'prop1',
prop2: 'prop2'
}
getParam() {
return param;
}
}

View File

@@ -0,0 +1,24 @@
export default (param => {
var _class, _temp;
return _temp = _class = function () {
function App() {
babelHelpers.classCallCheck(this, App);
}
babelHelpers.createClass(App, [{
key: 'getParam',
value: function getParam() {
return param;
}
}]);
return App;
}(), Object.defineProperty(_class, 'props', {
enumerable: true,
writable: true,
value: {
prop1: 'prop1',
prop2: 'prop2'
}
}), _temp;
});

View File

@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", ["transform-class-properties", { "spec": true }], "transform-es2015-classes", "transform-es2015-block-scoping", "syntax-class-properties"]
}

View File

@@ -0,0 +1,7 @@
call(class {
static test = true
});
export default class {
static test = true
};

View File

@@ -0,0 +1,21 @@
var _class, _temp;
call((_temp = _class = function _class() {
babelHelpers.classCallCheck(this, _class);
}, Object.defineProperty(_class, "test", {
enumerable: true,
writable: true,
value: true
}), _temp));
var _class2 = function _class2() {
babelHelpers.classCallCheck(this, _class2);
};
Object.defineProperty(_class2, "test", {
enumerable: true,
writable: true,
value: true
});
export default _class2;
;

View File

@@ -0,0 +1,14 @@
function withContext(ComposedComponent) {
return class WithContext extends Component {
static propTypes = {
context: PropTypes.shape(
{
addCss: PropTypes.func,
setTitle: PropTypes.func,
setMeta: PropTypes.func,
}
),
};
};
}

View File

@@ -0,0 +1,24 @@
function withContext(ComposedComponent) {
var _class, _temp;
return _temp = _class = function (_Component) {
babelHelpers.inherits(WithContext, _Component);
function WithContext() {
babelHelpers.classCallCheck(this, WithContext);
return babelHelpers.possibleConstructorReturn(this, (WithContext.__proto__ || Object.getPrototypeOf(WithContext)).apply(this, arguments));
}
return WithContext;
}(Component), Object.defineProperty(_class, "propTypes", {
enumerable: true,
writable: true,
value: {
context: PropTypes.shape({
addCss: PropTypes.func,
setTitle: PropTypes.func,
setMeta: PropTypes.func
})
}
}), _temp;
}

View File

@@ -0,0 +1,17 @@
class MyClass {
myAsyncMethod = async () => {
console.log(this);
}
}
(class MyClass2 {
myAsyncMethod = async () => {
console.log(this);
}
})
export default class MyClass3 {
myAsyncMethod = async () => {
console.log(this);
}
}

View File

@@ -0,0 +1,62 @@
class MyClass {
constructor() {
var _this = this;
Object.defineProperty(this, "myAsyncMethod", {
enumerable: true,
writable: true,
value: (() => {
var _ref = babelHelpers.asyncToGenerator(function* () {
console.log(_this);
});
return function value() {
return _ref.apply(this, arguments);
};
})()
});
}
}
(class MyClass2 {
constructor() {
var _this2 = this;
Object.defineProperty(this, "myAsyncMethod", {
enumerable: true,
writable: true,
value: (() => {
var _ref2 = babelHelpers.asyncToGenerator(function* () {
console.log(_this2);
});
return function value() {
return _ref2.apply(this, arguments);
};
})()
});
}
});
export default class MyClass3 {
constructor() {
var _this3 = this;
Object.defineProperty(this, "myAsyncMethod", {
enumerable: true,
writable: true,
value: (() => {
var _ref3 = babelHelpers.asyncToGenerator(function* () {
console.log(_this3);
});
return function value() {
return _ref3.apply(this, arguments);
};
})()
});
}
}

View File

@@ -0,0 +1,7 @@
{
"plugins": [
"external-helpers",
"transform-async-to-generator",
["transform-class-properties", { "spec": true }]
]
}

View File

@@ -0,0 +1,7 @@
export class MyClass {
static property = value;
}
export default class MyClass2 {
static property = value;
}

View File

@@ -0,0 +1,20 @@
export var MyClass = function MyClass() {
babelHelpers.classCallCheck(this, MyClass);
};
Object.defineProperty(MyClass, "property", {
enumerable: true,
writable: true,
value: value
});
var MyClass2 = function MyClass2() {
babelHelpers.classCallCheck(this, MyClass2);
};
Object.defineProperty(MyClass2, "property", {
enumerable: true,
writable: true,
value: value
});
export default MyClass2;

View File

@@ -0,0 +1,3 @@
var Foo = class {
static num = 0;
}

View File

@@ -0,0 +1,7 @@
var Foo = class {
static num = 0;
}
assert.equal(Foo.num, 0);
assert.equal(Foo.num = 1, 1);
assert.equal(Foo.name, "Foo");

View File

@@ -0,0 +1,9 @@
var _class, _temp;
var Foo = (_temp = _class = function Foo() {
babelHelpers.classCallCheck(this, Foo);
}, Object.defineProperty(_class, "num", {
enumerable: true,
writable: true,
value: 0
}), _temp);

View File

@@ -0,0 +1,3 @@
class Foo {
static bar;
}

View File

@@ -0,0 +1,6 @@
class Foo {
static num;
}
assert.equal("num" in Foo, true);
assert.equal(Foo.num, undefined);

View File

@@ -0,0 +1,9 @@
var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo);
};
Object.defineProperty(Foo, "bar", {
enumerable: true,
writable: true,
value: undefined
});

View File

@@ -0,0 +1,3 @@
class Foo {
static bar = "foo";
}

View File

@@ -0,0 +1,9 @@
class Foo {
static num = 0;
static str = "foo";
}
assert.equal(Foo.num, 0);
assert.equal(Foo.num = 1, 1);
assert.equal(Foo.str, "foo");
assert.equal(Foo.str = "bar", "bar");

View File

@@ -0,0 +1,9 @@
var Foo = function Foo() {
babelHelpers.classCallCheck(this, Foo);
};
Object.defineProperty(Foo, "bar", {
enumerable: true,
writable: true,
value: "foo"
});

View File

@@ -0,0 +1,7 @@
class Foo extends Bar {
bar = "foo";
constructor() {
foo(super());
}
}

View File

@@ -0,0 +1,18 @@
var Foo = function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
var _temp, _this;
babelHelpers.classCallCheck(this, Foo);
foo((_temp = (_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this)), _this), Object.defineProperty(_this, "bar", {
enumerable: true,
writable: true,
value: "foo"
}), _temp));
return _this;
}
return Foo;
}(Bar);

View File

@@ -0,0 +1,7 @@
class Foo extends Bar {
bar = "foo";
constructor() {
super();
}
}

View File

@@ -0,0 +1,18 @@
var Foo = function (_Bar) {
babelHelpers.inherits(Foo, _Bar);
function Foo() {
babelHelpers.classCallCheck(this, Foo);
var _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
Object.defineProperty(_this, "bar", {
enumerable: true,
writable: true,
value: "foo"
});
return _this;
}
return Foo;
}(Bar);