]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-arrow-callback.js
cb46c4a5021a13cb47d91a8b78a4cc5f272392d3
2 * @fileoverview Tests for prefer-arrow-callback rule.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/prefer-arrow-callback");
13 const { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
20 messageId
: "preferArrowCallback",
21 type
: "FunctionExpression"
24 const ruleTester
= new RuleTester({ parserOptions
: { ecmaVersion
: 2020 } });
26 ruleTester
.run("prefer-arrow-callback", rule
, {
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))"
49 code
: "foo(function bar() {});",
50 output
: "foo(() => {});",
54 code
: "foo(function() {});",
55 output
: "foo(() => {});",
56 options
: [{ allowNamedFunctions
: true }],
60 code
: "foo(function bar() {});",
61 output
: "foo(() => {});",
62 options
: [{ allowNamedFunctions
: false }],
66 code
: "foo(function() {});",
67 output
: "foo(() => {});",
71 code
: "foo(nativeCb || function() {});",
72 output
: "foo(nativeCb || (() => {}));",
76 code
: "foo(bar ? function() {} : function() {});",
77 output
: "foo(bar ? () => {} : () => {});",
78 errors
: [errors
[0], errors
[0]]
81 code
: "foo(function() { (function() { this; }); });",
82 output
: "foo(() => { (function() { this; }); });",
86 code
: "foo(function() { this; }.bind(this));",
87 output
: "foo(() => { this; });",
91 code
: "foo(bar || function() { this; }.bind(this));",
92 output
: "foo(bar || (() => { this; }));",
96 code
: "foo(function() { (() => this); }.bind(this));",
97 output
: "foo(() => { (() => this); });",
101 code
: "foo(function bar(a) { a; });",
102 output
: "foo((a) => { a; });",
106 code
: "foo(function(a) { a; });",
107 output
: "foo((a) => { a; });",
111 code
: "foo(function(arguments) { arguments; });",
112 output
: "foo((arguments) => { arguments; });",
116 code
: "foo(function() { this; });",
117 output
: null, // No fix applied
118 options
: [{ allowUnboundThis
: false }],
122 code
: "foo(function() { (() => this); });",
123 output
: null, // No fix applied
124 options
: [{ allowUnboundThis
: false }],
128 code
: "qux(function(foo, bar, baz) { return foo * 2; })",
129 output
: "qux((foo, bar, baz) => { return foo * 2; })",
133 code
: "qux(function(foo, bar, baz) { return foo * bar; }.bind(this))",
134 output
: "qux((foo, bar, baz) => { return foo * bar; })",
138 code
: "qux(function(foo, bar, baz) { return foo * this.qux; }.bind(this))",
139 output
: "qux((foo, bar, baz) => { return foo * this.qux; })",
143 code
: "foo(function() {}.bind(this, somethingElse))",
144 output
: "foo((() => {}).bind(this, somethingElse))",
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; });",
153 code
: "qux(function(baz, baz) { })",
154 output
: null, // Duplicate parameter names are a SyntaxError in arrow functions
158 code
: "qux(function( /* no params */ ) { })",
159 output
: "qux(( /* no params */ ) => { })",
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; })",
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; })",
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; })",
178 code
: "foo((bar || function() {}).bind(this))",
183 code
: "foo(function() {}.bind(this).bind(obj))",
184 output
: "foo((() => {}).bind(obj))",
190 code
: "foo?.(function() {});",
191 output
: "foo?.(() => {});",
195 code
: "foo?.(function() { return this; }.bind(this));",
196 output
: "foo?.(() => { return this; });",
200 code
: "foo(function() { return this; }?.bind(this));",
201 output
: "foo(() => { return this; });",
205 code
: "foo((function() { return this; }?.bind)(this));",