]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-arrow-callback.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / prefer-arrow-callback.js
1 /**
2 * @fileoverview Tests for prefer-arrow-callback rule.
3 * @author Toru Nagashima
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/prefer-arrow-callback");
13 const { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const errors = [{
20 messageId: "preferArrowCallback",
21 type: "FunctionExpression"
22 }];
23
24 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020 } });
25
26 ruleTester.run("prefer-arrow-callback", rule, {
27 valid: [
28 "foo(a => a);",
29 "foo(function*() {});",
30 "foo(function() { this; });",
31 { code: "foo(function bar() {});", options: [{ allowNamedFunctions: true }] },
32 "foo(function() { (() => this); });",
33 "foo(function() { this; }.bind(obj));",
34 "foo(function() { this; }.call(this));",
35 "foo(a => { (function() {}); });",
36 "var foo = function foo() {};",
37 "(function foo() {})();",
38 "foo(function bar() { bar; });",
39 "foo(function bar() { arguments; });",
40 "foo(function bar() { arguments; }.bind(this));",
41 "foo(function bar() { new.target; });",
42 "foo(function bar() { new.target; }.bind(this));",
43 "foo(function bar() { this; }.bind(this, somethingElse));",
44 "foo((function() {}).bind.bar)",
45 "foo((function() { this.bar(); }).bind(obj).bind(this))"
46 ],
47 invalid: [
48 {
49 code: "foo(function bar() {});",
50 output: "foo(() => {});",
51 errors
52 },
53 {
54 code: "foo(function() {});",
55 output: "foo(() => {});",
56 options: [{ allowNamedFunctions: true }],
57 errors
58 },
59 {
60 code: "foo(function bar() {});",
61 output: "foo(() => {});",
62 options: [{ allowNamedFunctions: false }],
63 errors
64 },
65 {
66 code: "foo(function() {});",
67 output: "foo(() => {});",
68 errors
69 },
70 {
71 code: "foo(nativeCb || function() {});",
72 output: "foo(nativeCb || (() => {}));",
73 errors
74 },
75 {
76 code: "foo(bar ? function() {} : function() {});",
77 output: "foo(bar ? () => {} : () => {});",
78 errors: [errors[0], errors[0]]
79 },
80 {
81 code: "foo(function() { (function() { this; }); });",
82 output: "foo(() => { (function() { this; }); });",
83 errors
84 },
85 {
86 code: "foo(function() { this; }.bind(this));",
87 output: "foo(() => { this; });",
88 errors
89 },
90 {
91 code: "foo(bar || function() { this; }.bind(this));",
92 output: "foo(bar || (() => { this; }));",
93 errors
94 },
95 {
96 code: "foo(function() { (() => this); }.bind(this));",
97 output: "foo(() => { (() => this); });",
98 errors
99 },
100 {
101 code: "foo(function bar(a) { a; });",
102 output: "foo((a) => { a; });",
103 errors
104 },
105 {
106 code: "foo(function(a) { a; });",
107 output: "foo((a) => { a; });",
108 errors
109 },
110 {
111 code: "foo(function(arguments) { arguments; });",
112 output: "foo((arguments) => { arguments; });",
113 errors
114 },
115 {
116 code: "foo(function() { this; });",
117 output: null, // No fix applied
118 options: [{ allowUnboundThis: false }],
119 errors
120 },
121 {
122 code: "foo(function() { (() => this); });",
123 output: null, // No fix applied
124 options: [{ allowUnboundThis: false }],
125 errors
126 },
127 {
128 code: "qux(function(foo, bar, baz) { return foo * 2; })",
129 output: "qux((foo, bar, baz) => { return foo * 2; })",
130 errors
131 },
132 {
133 code: "qux(function(foo, bar, baz) { return foo * bar; }.bind(this))",
134 output: "qux((foo, bar, baz) => { return foo * bar; })",
135 errors
136 },
137 {
138 code: "qux(function(foo, bar, baz) { return foo * this.qux; }.bind(this))",
139 output: "qux((foo, bar, baz) => { return foo * this.qux; })",
140 errors
141 },
142 {
143 code: "foo(function() {}.bind(this, somethingElse))",
144 output: "foo((() => {}).bind(this, somethingElse))",
145 errors
146 },
147 {
148 code: "qux(function(foo = 1, [bar = 2] = [], {qux: baz = 3} = {foo: 'bar'}) { return foo + bar; });",
149 output: "qux((foo = 1, [bar = 2] = [], {qux: baz = 3} = {foo: 'bar'}) => { return foo + bar; });",
150 errors
151 },
152 {
153 code: "qux(function(baz, baz) { })",
154 output: null, // Duplicate parameter names are a SyntaxError in arrow functions
155 errors
156 },
157 {
158 code: "qux(function( /* no params */ ) { })",
159 output: "qux(( /* no params */ ) => { })",
160 errors
161 },
162 {
163 code: "qux(function( /* a */ foo /* b */ , /* c */ bar /* d */ , /* e */ baz /* f */ ) { return foo; })",
164 output: "qux(( /* a */ foo /* b */ , /* c */ bar /* d */ , /* e */ baz /* f */ ) => { return foo; })",
165 errors
166 },
167 {
168 code: "qux(async function (foo = 1, bar = 2, baz = 3) { return baz; })",
169 output: "qux(async (foo = 1, bar = 2, baz = 3) => { return baz; })",
170 errors
171 },
172 {
173 code: "qux(async function (foo = 1, bar = 2, baz = 3) { return this; }.bind(this))",
174 output: "qux(async (foo = 1, bar = 2, baz = 3) => { return this; })",
175 errors
176 },
177 {
178 code: "foo((bar || function() {}).bind(this))",
179 output: null,
180 errors
181 },
182 {
183 code: "foo(function() {}.bind(this).bind(obj))",
184 output: "foo((() => {}).bind(obj))",
185 errors
186 },
187
188 // Optional chaining
189 {
190 code: "foo?.(function() {});",
191 output: "foo?.(() => {});",
192 errors
193 },
194 {
195 code: "foo?.(function() { return this; }.bind(this));",
196 output: "foo?.(() => { return this; });",
197 errors
198 },
199 {
200 code: "foo(function() { return this; }?.bind(this));",
201 output: "foo(() => { return this; });",
202 errors
203 },
204 {
205 code: "foo((function() { return this; }?.bind)(this));",
206 output: null,
207 errors
208 },
209
210 // https://github.com/eslint/eslint/issues/16718
211 {
212 code: `
213 test(
214 function ()
215 { }
216 );
217 `,
218 output: `
219 test(
220 () =>
221 { }
222 );
223 `,
224 errors
225 },
226 {
227 code: `
228 test(
229 function (
230 ...args
231 ) /* Lorem ipsum
232 dolor sit amet. */ {
233 return args;
234 }
235 );
236 `,
237 output: `
238 test(
239 (
240 ...args
241 ) => /* Lorem ipsum
242 dolor sit amet. */ {
243 return args;
244 }
245 );
246 `,
247 errors
248 }
249 ]
250 });