Skip to content

Commit

Permalink
Merge pull request #385 from silvenon/ref
Browse files Browse the repository at this point in the history
Add ignoreRefs option to jsx-no-bind
  • Loading branch information
yannickcr committed Jan 14, 2016
2 parents 42f9c0c + 46b956d commit 4da92df
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
35 changes: 35 additions & 0 deletions docs/rules/jsx-no-bind.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,41 @@ The following patterns are not considered warnings:
<div onClick={this._handleClick}></div>
```

## Rule Options

```js
"jsx-no-bind": [<enabled>, {
"ignoreRefs": <boolean> || false,
"allowArrowFunctions": <boolean> || false,
"allowBind": <boolean> || false
}]
```

### `ignoreRefs`

When `true` the following are not considered warnings:

```jsx
<div ref={c => this._div = c} />
<div ref={this._refCallback.bind(this)} />
```

### `allowArrowFunctions`

When `true` the following is not considered a warning:

```jsx
<div onClick={() => alert("1337")} />
```

### `allowBind`

When `true` the following is not considered a warning:

```jsx
<div onClick={this._handleClick.bind(this)} />
```

## Protips

### Lists of Items
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = function(context) {

return {
JSXAttribute: function(node) {
if (!node.value || !node.value.expression) {
var isRef = configuration.ignoreRefs && node.name.name === 'ref';
if (isRef || !node.value || !node.value.expression) {
return;
}
var valueNode = node.value.expression;
Expand Down Expand Up @@ -45,6 +46,10 @@ module.exports.schema = [{
allowBind: {
default: false,
type: 'boolean'
},
ignoreRefs: {
default: false,
type: 'boolean'
}
},
additionalProperties: false
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ ruleTester.run('jsx-no-bind', rule, {
parser: 'babel-eslint'
},

// bind() and arrow functions in refs explicitly ignored
{
code: '<div ref={c => this._input = c}></div>',
options: [{ignoreRefs: true}],
parser: 'babel-eslint'
},
{
code: '<div ref={this._refCallback.bind(this)}></div>',
options: [{ignoreRefs: true}],
parser: 'babel-eslint'
},

// bind() explicitly allowed
{
code: '<div onClick={this._handleClick.bind(this)}></div>',
Expand Down Expand Up @@ -66,6 +78,11 @@ ruleTester.run('jsx-no-bind', rule, {
errors: [{message: 'JSX props should not use .bind()'}],
parser: 'babel-eslint'
},
{
code: '<div ref={this._refCallback.bind(this)}></div>',
errors: [{message: 'JSX props should not use .bind()'}],
parser: 'babel-eslint'
},

// Arrow functions
{
Expand All @@ -82,6 +99,11 @@ ruleTester.run('jsx-no-bind', rule, {
code: '<div onClick={param => { first(); second(); }}></div>',
errors: [{message: 'JSX props should not use arrow functions'}],
parser: 'babel-eslint'
},
{
code: '<div ref={c => this._input = c}></div>',
errors: [{message: 'JSX props should not use arrow functions'}],
parser: 'babel-eslint'
}
]
});

0 comments on commit 4da92df

Please sign in to comment.