Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add correct/incorrect tags to prefer-arrow-callback #17589

Merged
merged 1 commit into from Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/src/rules/prefer-arrow-callback.md
Expand Up @@ -23,6 +23,8 @@ This rule locates function expressions used as callbacks or function arguments.

The following examples **will** be flagged:

::: incorrect

```js
/* eslint prefer-arrow-callback: "error" */

Expand All @@ -33,10 +35,14 @@ foo(function() { return this.a; }.bind(this)); // ERROR
// prefer: foo(() => this.a)
```

:::

Instances where an arrow function would not produce identical results will be ignored.

The following examples **will not** be flagged:

::: correct

```js
/* eslint prefer-arrow-callback: "error" */
/* eslint-env es6 */
Expand All @@ -57,6 +63,8 @@ foo(function() { return this.a; }); // OK
foo(function bar(n) { return n && n + bar(n - 1); }); // OK
```

:::

## Options

Access further control over this rule's behavior via an options object.
Expand All @@ -71,12 +79,16 @@ Changing this value to `true` will reverse this option's behavior by allowing us

`{ "allowNamedFunctions": true }` **will not** flag the following example:

::: correct

```js
/* eslint prefer-arrow-callback: [ "error", { "allowNamedFunctions": true } ] */

foo(function bar() {});
```

:::

### allowUnboundThis

By default `{ "allowUnboundThis": true }`, this `boolean` option allows function expressions containing `this` to be used as callbacks, as long as the function in question has not been explicitly bound.
Expand All @@ -85,6 +97,8 @@ When set to `false` this option prohibits the use of function expressions as cal

`{ "allowUnboundThis": false }` **will** flag the following examples:

::: incorrect

```js
/* eslint prefer-arrow-callback: [ "error", { "allowUnboundThis": false } ] */
/* eslint-env es6 */
Expand All @@ -96,6 +110,8 @@ foo(function() { (() => this); });
someArray.map(function(item) { return this.doSomething(item); }, someObject);
```

:::

## When Not To Use It

* In environments that have not yet adopted ES6 language features (ES3/5).
Expand Down