Skip to content

Conversation

@vjeux
Copy link
Contributor

@vjeux vjeux commented Jul 11, 2016

This converts the manual bind to an arrow property initializer function.

It works for the base case this.method = this.method.bind(this);, two other patterns that I've seen in the codebase that should be codemodded as well in a subsequent version

  • (this: any).method = this.method.bind(this)
  • const self = (this: any); self.method = this.method.bind(this)

Test Plan:
npm t

I haven't tried it on the codebase yet, just want to get early feedback

)) {
return false;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can replace the above with

 root .find(j.MethodDefinition, { kind: 'constructor', }) .find(j.AssignmentExpression, { left: { type: 'MemberExpression', object: { type: 'ThisExpression', }, property: { type: 'Identifier', }, }, right: { type: 'CallExpression', callee: { type: 'MemberExpression', property: { type: 'Identifier', name: 'bind', }, object: { type: 'MemberExpression', object: { type: 'ThisExpression', }, property: { type: 'Identifier', }, }, }, }, }) .filter(path => path.node.left.property.name === path.node.right.callee.object.property.name )
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the trick! In the new version I've added conditionals inside of that logic so I'm not really sure we can do it as you mentioned.

Also, for the method lookup, I need the reference to that node, so I'd need to traverse it up anyway.

@vjeux
Copy link
Contributor Author

vjeux commented Jul 12, 2016

  • Added support for:
    • (this: any).method = this.method.bind(this);
    • const self: any = this; self.method = self.method.bind(this);
  • Now removes the constructor when there's no content left but a super call.
  • It bails out early if there's nothing to transform.
  • Added unit tests for all those.

I'm going to run this on www now and see how it looks. All the features I want are now supported, there are probably going to be things uncovered trying it out in the real codebase.

@vjeux
Copy link
Contributor Author

vjeux commented Jul 13, 2016

  • Adding support for async functions
  • Use the babylon parser to avoid issues with different line location from flow parser
Summary: This converts the manual bind to an arrow property initializer function. Test Plan: npm t
@vjeux
Copy link
Contributor Author

vjeux commented Jul 26, 2016

Fixing lint error on one of the test that prevented travis to be green :)

@keyz
Copy link
Member

keyz commented Jul 31, 2016

Since this has already been battle tested let's merge this? 😃

@keyz keyz merged commit 180e040 into reactjs:master Aug 4, 2016
cpojer pushed a commit that referenced this pull request Aug 27, 2016
Following up on #67, this adds the manual-bind-to-arrow to the README's transform documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants