Skip to content

Commit 46b956d

Browse files
committed
Add allowRefs to the jsx-no-bind rule
Fixes #330. Refs #357.
1 parent a929545 commit 46b956d

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

docs/rules/jsx-no-bind.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@ The following patterns are not considered warnings:
1818
<div onClick={this._handleClick}></div>
1919
```
2020

21+
## Rule Options
22+
23+
```js
24+
"jsx-no-bind": [<enabled>, {
25+
"ignoreRefs": <boolean> || false,
26+
"allowArrowFunctions": <boolean> || false,
27+
"allowBind": <boolean> || false
28+
}]
29+
```
30+
31+
### `ignoreRefs`
32+
33+
When `true` the following are not considered warnings:
34+
35+
```jsx
36+
<div ref={c => this._div = c} />
37+
<div ref={this._refCallback.bind(this)} />
38+
```
39+
40+
### `allowArrowFunctions`
41+
42+
When `true` the following is not considered a warning:
43+
44+
```jsx
45+
<div onClick={() => alert("1337")} />
46+
```
47+
48+
### `allowBind`
49+
50+
When `true` the following is not considered a warning:
51+
52+
```jsx
53+
<div onClick={this._handleClick.bind(this)} />
54+
```
55+
2156
## Protips
2257

2358
### Lists of Items

lib/rules/jsx-no-bind.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ module.exports = function(context) {
1414

1515
return {
1616
JSXAttribute: function(node) {
17-
if (!node.value || !node.value.expression) {
17+
var isRef = configuration.ignoreRefs && node.name.name === 'ref';
18+
if (isRef || !node.value || !node.value.expression) {
1819
return;
1920
}
2021
var valueNode = node.value.expression;
@@ -45,6 +46,10 @@ module.exports.schema = [{
4546
allowBind: {
4647
default: false,
4748
type: 'boolean'
49+
},
50+
ignoreRefs: {
51+
default: false,
52+
type: 'boolean'
4853
}
4954
},
5055
additionalProperties: false

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ ruleTester.run('jsx-no-bind', rule, {
3434
parser: 'babel-eslint'
3535
},
3636

37+
// bind() and arrow functions in refs explicitly ignored
38+
{
39+
code: '<div ref={c => this._input = c}></div>',
40+
options: [{ignoreRefs: true}],
41+
parser: 'babel-eslint'
42+
},
43+
{
44+
code: '<div ref={this._refCallback.bind(this)}></div>',
45+
options: [{ignoreRefs: true}],
46+
parser: 'babel-eslint'
47+
},
48+
3749
// bind() explicitly allowed
3850
{
3951
code: '<div onClick={this._handleClick.bind(this)}></div>',
@@ -66,6 +78,11 @@ ruleTester.run('jsx-no-bind', rule, {
6678
errors: [{message: 'JSX props should not use .bind()'}],
6779
parser: 'babel-eslint'
6880
},
81+
{
82+
code: '<div ref={this._refCallback.bind(this)}></div>',
83+
errors: [{message: 'JSX props should not use .bind()'}],
84+
parser: 'babel-eslint'
85+
},
6986

7087
// Arrow functions
7188
{
@@ -82,6 +99,11 @@ ruleTester.run('jsx-no-bind', rule, {
8299
code: '<div onClick={param => { first(); second(); }}></div>',
83100
errors: [{message: 'JSX props should not use arrow functions'}],
84101
parser: 'babel-eslint'
102+
},
103+
{
104+
code: '<div ref={c => this._input = c}></div>',
105+
errors: [{message: 'JSX props should not use arrow functions'}],
106+
parser: 'babel-eslint'
85107
}
86108
]
87109
});

0 commit comments

Comments
 (0)