Skip to content

Commit

Permalink
fix: fix arrow function using props in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Oct 14, 2017
1 parent 31c1682 commit 4896a34
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
20 changes: 19 additions & 1 deletion src/babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ module.exports = function plugin(args) {
)
}
} else if (!path.node[replaced] && path.node.kind === 'constructor') {
const constructorParams = path.node.params
path.traverse({
AssignmentExpression(exp) {
if (
Expand All @@ -287,11 +288,28 @@ module.exports = function plugin(args) {
)

// arrow function body can either be a block statement or a returned expression
const newMethodBody =
let newMethodBody =
node.body.type === 'BlockStatement'
? node.body
: t.blockStatement([t.returnStatement(node.body)])

if (constructorParams[0]) {
const props = t.variableDeclaration('const', [
t.variableDeclarator(
constructorParams[0],
t.memberExpression(
t.thisExpression(),
t.identifier('props'),
),
),
])

newMethodBody = t.blockStatement([
props,
...newMethodBody.body,
])
}

const newMethod = t.classMethod(
'method',
newIdentifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
class Foo {
class PropsInConstructor {
constructor({ a, b }) {
this.onClick = (e) => {
console.log(a, b)
console.log(e.target.value)
}
}
}

class ReturnFunc {
constructor() {
this.onClick = (e) => e.target.value
}
Expand Down
43 changes: 37 additions & 6 deletions test/__snapshots__/babel.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,54 @@ var _createClass = function () { function defineProperties(target, props) { for
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\\"Cannot call a class as a function\\"); } }
var Foo = function () {
function Foo() {
var PropsInConstructor = function () {
function PropsInConstructor(_ref) {
var _this = this;
_classCallCheck(this, Foo);
var a = _ref.a,
b = _ref.b;
_classCallCheck(this, PropsInConstructor);
this.onClick = function () {
return _this.__onClick__REACT_HOT_LOADER__.apply(_this, arguments);
};
}
_createClass(Foo, [{
_createClass(PropsInConstructor, [{
key: \\"__onClick__REACT_HOT_LOADER__\\",
value: function __onClick__REACT_HOT_LOADER__(e) {
var _props = this.props,
a = _props.a,
b = _props.b;
console.log(a, b);
console.log(e.target.value);
}
}]);
return PropsInConstructor;
}();
var ReturnFunc = function () {
function ReturnFunc() {
var _this2 = this;
_classCallCheck(this, ReturnFunc);
this.onClick = function () {
return _this2.__onClick__REACT_HOT_LOADER__.apply(_this2, arguments);
};
}
_createClass(ReturnFunc, [{
key: \\"__onClick__REACT_HOT_LOADER__\\",
value: function __onClick__REACT_HOT_LOADER__(e) {
return e.target.value;
}
}]);
return Foo;
return ReturnFunc;
}();
;
Expand All @@ -78,7 +107,9 @@ var _temp = function () {
return;
}
__REACT_HOT_LOADER__.register(Foo, \\"Foo\\", __FILENAME__);
__REACT_HOT_LOADER__.register(PropsInConstructor, \\"PropsInConstructor\\", __FILENAME__);
__REACT_HOT_LOADER__.register(ReturnFunc, \\"ReturnFunc\\", __FILENAME__);
}();
;"
Expand Down

0 comments on commit 4896a34

Please sign in to comment.