Skip to content

Commit 4353ddf

Browse files
petersendidityannickcr
authored andcommitted
Fix bind detection in render when assigned to a variable (fixes jsx-eslint#474)
1 parent 82b3aa9 commit 4353ddf

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/rules/jsx-no-bind.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@ module.exports = function(context) {
1313
var configuration = context.options[0] || {};
1414

1515
return {
16+
CallExpression: function(node) {
17+
var callee = node.callee;
18+
if (
19+
!configuration.allowBind &&
20+
callee.type !== 'MemberExpression' ||
21+
callee.property.name !== 'bind'
22+
) {
23+
return;
24+
}
25+
var ancestors = context.getAncestors(callee).reverse();
26+
for (var i = 0, j = ancestors.length; i < j; i++) {
27+
if (
28+
!configuration.allowBind &&
29+
(ancestors[i].type === 'MethodDefinition' && ancestors[i].key.name === 'render') ||
30+
(ancestors[i].type === 'Property' && ancestors[i].key.name === 'render')
31+
) {
32+
context.report({
33+
node: callee,
34+
message: 'JSX props should not use .bind()'
35+
});
36+
break;
37+
}
38+
}
39+
},
40+
1641
JSXAttribute: function(node) {
1742
var isRef = configuration.ignoreRefs && node.name.name === 'ref';
1843
if (isRef || !node.value || !node.value.expression) {

tests/lib/rules/jsx-no-bind.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@ ruleTester.run('jsx-no-bind', rule, {
8383
errors: [{message: 'JSX props should not use .bind()'}],
8484
parser: 'babel-eslint'
8585
},
86+
{
87+
code: [
88+
'var Hello = React.createClass({',
89+
' render: function() {',
90+
' const click = this.someMethod.bind(this);',
91+
' return <div onClick={click}>Hello {this.state.name}</div>;',
92+
' }',
93+
'});'
94+
].join('\n'),
95+
errors: [{message: 'JSX props should not use .bind()'}],
96+
parser: 'babel-eslint'
97+
},
98+
{
99+
code: [
100+
'class Hello23 extends React.Component {',
101+
' render() {',
102+
' const click = this.someMethod.bind(this);',
103+
' return <div onClick={click}>Hello {this.state.name}</div>;',
104+
' }',
105+
'};'
106+
].join('\n'),
107+
errors: [{message: 'JSX props should not use .bind()'}],
108+
parser: 'babel-eslint'
109+
},
86110

87111
// Arrow functions
88112
{

0 commit comments

Comments
 (0)