You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This rule is a variation of the core eslint `prefer-arrow-callback` rule that is mocha-aware and does not flag non-arrow callbacks within mocha functions.
9
+
This rule is a Mocha-aware drop-in replacement for ESLint's core
You will want to disable the original `prefer-arrow-callback` rule and configure the mocha-friendly replacement under the rules section.
12
+
Use it instead of the core rule when linting Mocha tests. It keeps the core rule's behavior, options,
13
+
and fixes, but does not report the callback functions passed directly to Mocha suites, tests, and hooks.
12
14
13
15
```json
14
16
{
@@ -19,136 +21,55 @@ You will want to disable the original `prefer-arrow-callback` rule and configure
19
21
}
20
22
```
21
23
22
-
## Rule Overview
23
-
24
-
Arrow functions can be an attractive alternative to function expressions for callbacks or function arguments.
25
-
26
-
For example, arrow functions are automatically bound to their surrounding scope/context. This provides an alternative to the pre-ES6 standard of explicitly binding function expressions to achieve similar behavior.
27
-
28
-
Additionally, arrow functions are:
29
-
30
-
- less verbose, and easier to reason about.
31
-
32
-
- bound lexically regardless of where or when they are invoked.
33
-
34
24
## Rule Details
35
25
36
-
This rule locates function expressions used as callbacks or function arguments. An error will be produced for any that could be replaced by an arrow function without changing the result.
26
+
This rule behaves like ESLint's core `prefer-arrow-callback`, except that direct callbacks for Mocha
27
+
functions are allowed.
37
28
38
-
The following examples **will** be flagged:
29
+
These patterns are considered correct:
39
30
40
31
```js
41
32
/* eslint mocha/prefer-arrow-callback: "error" */
42
33
43
-
foo(function (a) {
44
-
return a;
45
-
}); // ERROR
46
-
// prefer: foo(a => a)
34
+
describe("suite", function () {
35
+
beforeEach(function () {
36
+
setup();
37
+
});
47
38
48
-
foo(function () {
49
-
returnthis.a;
50
-
}
51
-
.bind(this)); // ERROR
52
-
// prefer: foo(() => this.a)
39
+
it("works", function () {
40
+
runAssertion();
41
+
});
42
+
});
53
43
```
54
44
55
-
Instances where an arrow function would not produce identical results will be ignored.
56
-
57
-
The following examples **will not** be flagged:
45
+
Non-Mocha callbacks are still checked, even when they appear inside Mocha callbacks:
58
46
59
47
```js
60
48
/* eslint mocha/prefer-arrow-callback: "error" */
61
-
/* eslint-env es6 */
62
-
63
-
// arrow function callback
64
-
foo((a) => a); // OK
65
-
66
-
// generator as callback
67
-
foo(function* () {
68
-
yield;
69
-
}); // OK
70
-
71
-
// function expression not used as callback or function argument
72
-
varfoo=functionfoo(a) {
73
-
return a;
74
-
}; // OK
75
-
76
-
// unbound function expression callback
77
-
foo(function () {
78
-
returnthis.a;
79
-
}); // OK
80
-
81
-
// recursive named function callback
82
-
foo(functionbar(n) {
83
-
return n && n +bar(n -1);
84
-
}); // OK
85
-
86
-
// mocha suite definition callback
87
-
describe('test suite', function () {
88
-
returnPromise.resolve();
89
-
}); // OK
90
-
91
-
// mocha hook callback
92
-
beforeEach('before each test', function () {
93
-
returnPromise.resolve();
94
-
}); // OK
95
-
96
-
// mocha test case callback
97
-
it('should resolve', function () {
98
-
returnPromise.resolve();
99
-
}); // OK
100
-
```
101
-
102
-
## Options
103
-
104
-
Access further control over this rule's behavior via an options object.
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.
125
-
126
-
When set to `false` this option prohibits the use of function expressions as callbacks or function arguments entirely, without exception.
127
-
128
-
`{ "allowUnboundThis": false }`**will** flag the following examples:
0 commit comments