Skip to content

Commit

Permalink
docs: add docs for no-bind-operator
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Sep 20, 2019
1 parent 6b84d1d commit f3b73ef
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
46 changes: 46 additions & 0 deletions docs/no-bind-operator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# no-bind-operator

This prevents the use of the Bind operator (`::`)

```js
obj::func
::obj.func

::obj.func(val)
```

These will not be allowed because they are not supported in the following browsers:

- Edge (any version at the time of writing)
- Safari (any version at the time of writing)
- Firefox (any version at the time of writing)
- Chrome (any version at the time of writing)

## What is the Fix?

You can use the `.bind` function prototype method to create a bound function:

```js
// these are equivalent:
obj::func
func.bind(obj)

// these are equivalent:
::obj.func
obj.func.bind(obj)
```

If you're using the bind operator as a call expression, then you can use the `.call` function prototype method:

```js
// these are equivalent:
obj::func(val)
func.call(obj, vall)

// these are equivalent:
::obj.func(val)
obj.func.call(obj, val)
```


This can be safely disabled if you intend to compile code with the `@babel/plugin-proposal-function-bind` Babel plugin.
21 changes: 20 additions & 1 deletion test/no-bind-operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var ruleTester = new RuleTester({parser: require.resolve('babel-eslint'), parser
ruleTester.run('no-bind-operator', rule, {
valid: [
{code: 'console.log.bind(console)'},
{code: 'console.log.call(console)'},
],
invalid: [
{
Expand All @@ -25,6 +26,24 @@ ruleTester.run('no-bind-operator', rule, {
'The Bind Operator is not supported in undefined'
}
]
}
},
{
code: 'console::log(1)',
errors: [
{
message:
'The Bind Operator is not supported in undefined'
}
]
},
{
code: '::console.log(1)',
errors: [
{
message:
'The Bind Operator is not supported in undefined'
}
]
},
]
})

0 comments on commit f3b73ef

Please sign in to comment.