Skip to content

Commit

Permalink
Fix bind detection in render when assigned to a variable (fixes #474)
Browse files Browse the repository at this point in the history
  • Loading branch information
petersendidit authored and yannickcr committed Jun 5, 2016
1 parent 82b3aa9 commit 4353ddf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ module.exports = function(context) {
var configuration = context.options[0] || {};

return {
CallExpression: function(node) {
var callee = node.callee;
if (
!configuration.allowBind &&
callee.type !== 'MemberExpression' ||
callee.property.name !== 'bind'
) {
return;
}
var ancestors = context.getAncestors(callee).reverse();
for (var i = 0, j = ancestors.length; i < j; i++) {
if (
!configuration.allowBind &&
(ancestors[i].type === 'MethodDefinition' && ancestors[i].key.name === 'render') ||
(ancestors[i].type === 'Property' && ancestors[i].key.name === 'render')
) {
context.report({
node: callee,
message: 'JSX props should not use .bind()'
});
break;
}
}
},

JSXAttribute: function(node) {
var isRef = configuration.ignoreRefs && node.name.name === 'ref';
if (isRef || !node.value || !node.value.expression) {
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ ruleTester.run('jsx-no-bind', rule, {
errors: [{message: 'JSX props should not use .bind()'}],
parser: 'babel-eslint'
},
{
code: [
'var Hello = React.createClass({',
' render: function() {',
' const click = this.someMethod.bind(this);',
' return <div onClick={click}>Hello {this.state.name}</div>;',
' }',
'});'
].join('\n'),
errors: [{message: 'JSX props should not use .bind()'}],
parser: 'babel-eslint'
},
{
code: [
'class Hello23 extends React.Component {',
' render() {',
' const click = this.someMethod.bind(this);',
' return <div onClick={click}>Hello {this.state.name}</div>;',
' }',
'};'
].join('\n'),
errors: [{message: 'JSX props should not use .bind()'}],
parser: 'babel-eslint'
},

// Arrow functions
{
Expand Down

0 comments on commit 4353ddf

Please sign in to comment.