diff --git a/packages/babel-plugin-transform-class-properties/README.md b/packages/babel-plugin-transform-class-properties/README.md index 6b66fcc4f8c7c..b1d4c638095f5 100644 --- a/packages/babel-plugin-transform-class-properties/README.md +++ b/packages/babel-plugin-transform-class-properties/README.md @@ -6,6 +6,11 @@ $ npm install babel-plugin-transform-class-properties ``` +### Options: `spec` + +- Class properties are compiled to use `Object.defineProperty` +- Static fields are now defined even if they are not initialized + ## Usage ### Via `.babelrc` (Recommended) @@ -13,9 +18,17 @@ $ npm install babel-plugin-transform-class-properties **.babelrc** ```json +// without options { "plugins": ["transform-class-properties"] } + +// with options +{ + "plugins": [ + ["transform-class-properties", { "spec": true }] + ] +} ``` ### Via CLI diff --git a/packages/babel-plugin-transform-class-properties/package.json b/packages/babel-plugin-transform-class-properties/package.json index 5ece9ddc20803..7d24b8964fa23 100644 --- a/packages/babel-plugin-transform-class-properties/package.json +++ b/packages/babel-plugin-transform-class-properties/package.json @@ -11,7 +11,8 @@ "dependencies": { "babel-helper-function-name": "^6.18.0", "babel-plugin-syntax-class-properties": "^6.8.0", - "babel-runtime": "^6.9.1" + "babel-runtime": "^6.9.1", + "babel-template": "^6.15.0" }, "devDependencies": { "babel-helper-plugin-test-runner": "^6.18.0" diff --git a/packages/babel-plugin-transform-class-properties/src/index.js b/packages/babel-plugin-transform-class-properties/src/index.js index 5744b14afc904..10451b377988b 100644 --- a/packages/babel-plugin-transform-class-properties/src/index.js +++ b/packages/babel-plugin-transform-class-properties/src/index.js @@ -1,6 +1,6 @@ /* eslint max-len: 0 */ -// todo: define instead of assign import nameFunction from "babel-helper-function-name"; +import template from "babel-template"; export default function ({ types: t }) { let findBareSupers = { @@ -20,11 +20,31 @@ export default function ({ types: t }) { } }; + const buildObjectDefineProperty = template(` + Object.defineProperty(REF, KEY, { + // configurable is false by default + enumerable: true, + writable: true, + value: VALUE + }); + `); + + const buildClassPropertySpec = (ref, {key, value, computed}) => buildObjectDefineProperty({ + REF: ref, + KEY: (t.isIdentifier(key) && !computed) ? t.stringLiteral(key.name) : key, + VALUE: value ? value : t.identifier("undefined") + }); + + const buildClassPropertyNonSpec = (ref, {key, value, computed}) => t.expressionStatement( + t.assignmentExpression("=", t.memberExpression(ref, key, computed || t.isLiteral(key)), value) + ); + return { inherits: require("babel-plugin-syntax-class-properties"), visitor: { - Class(path) { + Class(path, state) { + const buildClassProperty = state.opts.spec ? buildClassPropertySpec : buildClassPropertyNonSpec; let isDerived = !!path.node.superClass; let constructor; let props = []; @@ -55,19 +75,18 @@ export default function ({ types: t }) { for (let prop of props) { let propNode = prop.node; if (propNode.decorators && propNode.decorators.length > 0) continue; - if (!propNode.value) continue; + + // In non-spec mode, all properties without values are ignored. + // In spec mode, *static* properties without values are still defined (see below). + if (!state.opts.spec && !propNode.value) continue; let isStatic = propNode.static; - let isComputed = propNode.computed || t.isLiteral(prop.key); if (isStatic) { - nodes.push(t.expressionStatement( - t.assignmentExpression("=", t.memberExpression(ref, propNode.key, isComputed), propNode.value) - )); + nodes.push(buildClassProperty(ref, propNode)); } else { - instanceBody.push(t.expressionStatement( - t.assignmentExpression("=", t.memberExpression(t.thisExpression(), propNode.key, isComputed), propNode.value) - )); + if (!propNode.value) continue; // Ignore instance property with no value in spec mode + instanceBody.push(buildClassProperty(t.thisExpression(), propNode)); } } diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/constructor-collision/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/constructor-collision/actual.js new file mode 100644 index 0000000000000..3bf6c834720ff --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/constructor-collision/actual.js @@ -0,0 +1,9 @@ +var foo = "bar"; + +class Foo { + bar = foo; + + constructor() { + var foo = "foo"; + } +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/constructor-collision/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/constructor-collision/expected.js new file mode 100644 index 0000000000000..0b239c656791d --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/constructor-collision/expected.js @@ -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 + }); +}; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/actual.js new file mode 100644 index 0000000000000..cd8e3fdd80315 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/actual.js @@ -0,0 +1,3 @@ +class Foo extends Bar { + bar = "foo"; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js new file mode 100644 index 0000000000000..f2cd752350235 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/derived/expected.js @@ -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); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/actual.js new file mode 100644 index 0000000000000..60a1c5e823cdd --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/actual.js @@ -0,0 +1,9 @@ +class Child extends Parent { + constructor() { + super(); + } + + scopedFunctionWithThis = () => { + this.name = {}; + } +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js new file mode 100644 index 0000000000000..f03f625034232 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/expected.js @@ -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); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/options.json b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/options.json new file mode 100644 index 0000000000000..a9cbc232a33b6 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/foobar/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["external-helpers", ["transform-class-properties", { "spec": true }]], + "presets": ["stage-0", "es2015"] +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance-undefined/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance-undefined/actual.js new file mode 100644 index 0000000000000..a36cdd975c823 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance-undefined/actual.js @@ -0,0 +1,3 @@ +class Foo { + bar; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance-undefined/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance-undefined/expected.js new file mode 100644 index 0000000000000..4f46d4a642303 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance-undefined/expected.js @@ -0,0 +1,3 @@ +var Foo = function Foo() { + babelHelpers.classCallCheck(this, Foo); +}; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/actual.js new file mode 100644 index 0000000000000..9cecfe8a30138 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/actual.js @@ -0,0 +1,3 @@ +class Foo { + bar = "foo"; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/expected.js new file mode 100644 index 0000000000000..912039e900647 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/instance/expected.js @@ -0,0 +1,8 @@ +var Foo = function Foo() { + babelHelpers.classCallCheck(this, Foo); + Object.defineProperty(this, "bar", { + enumerable: true, + writable: true, + value: "foo" + }); +}; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/non-block-arrow-func/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/non-block-arrow-func/actual.js new file mode 100644 index 0000000000000..4b67e0e158f68 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/non-block-arrow-func/actual.js @@ -0,0 +1,11 @@ +export default param => + class App { + static props = { + prop1: 'prop1', + prop2: 'prop2' + } + + getParam() { + return param; + } + } diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/non-block-arrow-func/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/non-block-arrow-func/expected.js new file mode 100644 index 0000000000000..977b45abfb1cd --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/non-block-arrow-func/expected.js @@ -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; +}); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/options.json b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/options.json new file mode 100644 index 0000000000000..31ab836c1e9dd --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers", ["transform-class-properties", { "spec": true }], "transform-es2015-classes", "transform-es2015-block-scoping", "syntax-class-properties"] +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T2983/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T2983/actual.js new file mode 100644 index 0000000000000..2961c9a40366b --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T2983/actual.js @@ -0,0 +1,7 @@ +call(class { + static test = true +}); + +export default class { + static test = true +}; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T2983/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T2983/expected.js new file mode 100644 index 0000000000000..e146566f95b85 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T2983/expected.js @@ -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; +; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/actual.js new file mode 100644 index 0000000000000..38b9e80baeccd --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/actual.js @@ -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, + } + ), + }; + }; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/expected.js new file mode 100644 index 0000000000000..4ecdd47774316 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T6719/expected.js @@ -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; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/actual.js new file mode 100644 index 0000000000000..1ddb4a1e78874 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/actual.js @@ -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); + } +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/expected.js new file mode 100644 index 0000000000000..74a4cbc036f1f --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/expected.js @@ -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); + }; + })() + }); + } + +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/options.json b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/options.json new file mode 100644 index 0000000000000..3d827b26c9548 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/regression-T7364/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "external-helpers", + "transform-async-to-generator", + ["transform-class-properties", { "spec": true }] + ] +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/actual.js new file mode 100644 index 0000000000000..729475cc52ffc --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/actual.js @@ -0,0 +1,7 @@ +export class MyClass { + static property = value; +} + +export default class MyClass2 { + static property = value; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/expected.js new file mode 100644 index 0000000000000..3363e3b4cecfe --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-export/expected.js @@ -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; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/actual.js new file mode 100644 index 0000000000000..96f03df576a08 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/actual.js @@ -0,0 +1,3 @@ +var Foo = class { + static num = 0; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/exec.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/exec.js new file mode 100644 index 0000000000000..b7ecf2f9cb5bd --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/exec.js @@ -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"); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/expected.js new file mode 100644 index 0000000000000..78cc382206741 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-infer-name/expected.js @@ -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); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/actual.js new file mode 100644 index 0000000000000..4ee31b9c35417 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/actual.js @@ -0,0 +1,3 @@ +class Foo { + static bar; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/exec.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/exec.js new file mode 100644 index 0000000000000..0771105a6137d --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/exec.js @@ -0,0 +1,6 @@ +class Foo { + static num; +} + +assert.equal("num" in Foo, true); +assert.equal(Foo.num, undefined); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/expected.js new file mode 100644 index 0000000000000..cbdc0a11b6d68 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static-undefined/expected.js @@ -0,0 +1,9 @@ +var Foo = function Foo() { + babelHelpers.classCallCheck(this, Foo); +}; + +Object.defineProperty(Foo, "bar", { + enumerable: true, + writable: true, + value: undefined +}); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/actual.js new file mode 100644 index 0000000000000..ddc637ec2391f --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/actual.js @@ -0,0 +1,3 @@ +class Foo { + static bar = "foo"; +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/exec.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/exec.js new file mode 100644 index 0000000000000..299aa7949d6be --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/exec.js @@ -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"); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/expected.js new file mode 100644 index 0000000000000..78f546364a44c --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/static/expected.js @@ -0,0 +1,9 @@ +var Foo = function Foo() { + babelHelpers.classCallCheck(this, Foo); +}; + +Object.defineProperty(Foo, "bar", { + enumerable: true, + writable: true, + value: "foo" +}); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/actual.js new file mode 100644 index 0000000000000..222dec81a60cd --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/actual.js @@ -0,0 +1,7 @@ +class Foo extends Bar { + bar = "foo"; + + constructor() { + foo(super()); + } +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js new file mode 100644 index 0000000000000..78def7939a3f4 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-expression/expected.js @@ -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); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/actual.js new file mode 100644 index 0000000000000..0af8843375d43 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/actual.js @@ -0,0 +1,7 @@ +class Foo extends Bar { + bar = "foo"; + + constructor() { + super(); + } +} diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js new file mode 100644 index 0000000000000..5050e95e6a8d3 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/spec/super-statement/expected.js @@ -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);