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:
committed by
Henry Zhu
parent
cf3a38fb40
commit
f8ddd80f96
@@ -0,0 +1,9 @@
|
||||
var foo = "bar";
|
||||
|
||||
class Foo {
|
||||
bar = foo;
|
||||
|
||||
constructor() {
|
||||
var foo = "foo";
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
});
|
||||
};
|
||||
3
packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
class Foo extends Bar {
|
||||
bar = "foo";
|
||||
}
|
||||
16
packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js
vendored
Normal file
16
packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js
vendored
Normal 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);
|
||||
9
packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/actual.js
vendored
Normal file
9
packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/actual.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
class Child extends Parent {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
scopedFunctionWithThis = () => {
|
||||
this.name = {};
|
||||
}
|
||||
}
|
||||
22
packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js
vendored
Normal file
22
packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js
vendored
Normal 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);
|
||||
4
packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/options.json
vendored
Normal file
4
packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/options.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"plugins": ["external-helpers", ["transform-class-properties", { "spec": true }]],
|
||||
"presets": ["stage-0", "es2015"]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Foo {
|
||||
bar;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
var Foo = function Foo() {
|
||||
babelHelpers.classCallCheck(this, Foo);
|
||||
};
|
||||
3
packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
class Foo {
|
||||
bar = "foo";
|
||||
}
|
||||
8
packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/expected.js
vendored
Normal file
8
packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/expected.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
var Foo = function Foo() {
|
||||
babelHelpers.classCallCheck(this, Foo);
|
||||
Object.defineProperty(this, "bar", {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: "foo"
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
export default param =>
|
||||
class App {
|
||||
static props = {
|
||||
prop1: 'prop1',
|
||||
prop2: 'prop2'
|
||||
}
|
||||
|
||||
getParam() {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
});
|
||||
3
packages/babel-plugin-transform-class-properties/test/fixtures/spec/options.json
vendored
Normal file
3
packages/babel-plugin-transform-class-properties/test/fixtures/spec/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", ["transform-class-properties", { "spec": true }], "transform-es2015-classes", "transform-es2015-block-scoping", "syntax-class-properties"]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
call(class {
|
||||
static test = true
|
||||
});
|
||||
|
||||
export default class {
|
||||
static test = true
|
||||
};
|
||||
21
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T2983/expected.js
vendored
Normal file
21
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T2983/expected.js
vendored
Normal 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;
|
||||
;
|
||||
14
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/actual.js
vendored
Normal file
14
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/actual.js
vendored
Normal 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,
|
||||
}
|
||||
),
|
||||
};
|
||||
};
|
||||
}
|
||||
24
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/expected.js
vendored
Normal file
24
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/expected.js
vendored
Normal 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;
|
||||
}
|
||||
17
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/actual.js
vendored
Normal file
17
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/actual.js
vendored
Normal 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);
|
||||
}
|
||||
}
|
||||
62
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/expected.js
vendored
Normal file
62
packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/expected.js
vendored
Normal 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);
|
||||
};
|
||||
})()
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
"transform-async-to-generator",
|
||||
["transform-class-properties", { "spec": true }]
|
||||
]
|
||||
}
|
||||
7
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/actual.js
vendored
Normal file
7
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/actual.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
export class MyClass {
|
||||
static property = value;
|
||||
}
|
||||
|
||||
export default class MyClass2 {
|
||||
static property = value;
|
||||
}
|
||||
20
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/expected.js
vendored
Normal file
20
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/expected.js
vendored
Normal 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;
|
||||
@@ -0,0 +1,3 @@
|
||||
var Foo = class {
|
||||
static num = 0;
|
||||
}
|
||||
@@ -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");
|
||||
@@ -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);
|
||||
@@ -0,0 +1,3 @@
|
||||
class Foo {
|
||||
static bar;
|
||||
}
|
||||
6
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/exec.js
vendored
Normal file
6
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/exec.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
static num;
|
||||
}
|
||||
|
||||
assert.equal("num" in Foo, true);
|
||||
assert.equal(Foo.num, undefined);
|
||||
@@ -0,0 +1,9 @@
|
||||
var Foo = function Foo() {
|
||||
babelHelpers.classCallCheck(this, Foo);
|
||||
};
|
||||
|
||||
Object.defineProperty(Foo, "bar", {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: undefined
|
||||
});
|
||||
3
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
class Foo {
|
||||
static bar = "foo";
|
||||
}
|
||||
9
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/exec.js
vendored
Normal file
9
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/exec.js
vendored
Normal 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");
|
||||
9
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/expected.js
vendored
Normal file
9
packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/expected.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
var Foo = function Foo() {
|
||||
babelHelpers.classCallCheck(this, Foo);
|
||||
};
|
||||
|
||||
Object.defineProperty(Foo, "bar", {
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: "foo"
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo extends Bar {
|
||||
bar = "foo";
|
||||
|
||||
constructor() {
|
||||
foo(super());
|
||||
}
|
||||
}
|
||||
18
packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js
vendored
Normal file
18
packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js
vendored
Normal 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);
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo extends Bar {
|
||||
bar = "foo";
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
18
packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js
vendored
Normal file
18
packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js
vendored
Normal 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);
|
||||
Reference in New Issue
Block a user