From f3b73ef9f91a4366a4a66f1456ccdf1d71ccd938 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Fri, 20 Sep 2019 14:03:21 +0100 Subject: [PATCH] docs: add docs for no-bind-operator --- docs/no-bind-operator.md | 46 ++++++++++++++++++++++++++++++++++++++++ test/no-bind-operator.js | 21 +++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 docs/no-bind-operator.md diff --git a/docs/no-bind-operator.md b/docs/no-bind-operator.md new file mode 100644 index 0000000..3d6be22 --- /dev/null +++ b/docs/no-bind-operator.md @@ -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. diff --git a/test/no-bind-operator.js b/test/no-bind-operator.js index 1e408b6..08d10c0 100644 --- a/test/no-bind-operator.js +++ b/test/no-bind-operator.js @@ -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: [ { @@ -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' + } + ] + }, ] })