]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-arrow-callback.js
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
: 6 } });
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));"
47 code
: "foo(function bar() {});",
48 output
: "foo(() => {});",
52 code
: "foo(function() {});",
53 output
: "foo(() => {});",
54 options
: [{ allowNamedFunctions
: true }],
58 code
: "foo(function bar() {});",
59 output
: "foo(() => {});",
60 options
: [{ allowNamedFunctions
: false }],
64 code
: "foo(function() {});",
65 output
: "foo(() => {});",
69 code
: "foo(nativeCb || function() {});",
70 output
: "foo(nativeCb || (() => {}));",
74 code
: "foo(bar ? function() {} : function() {});",
75 output
: "foo(bar ? () => {} : () => {});",
76 errors
: [errors
[0], errors
[0]]
79 code
: "foo(function() { (function() { this; }); });",
80 output
: "foo(() => { (function() { this; }); });",
84 code
: "foo(function() { this; }.bind(this));",
85 output
: "foo(() => { this; });",
89 code
: "foo(bar || function() { this; }.bind(this));",
90 output
: "foo(bar || (() => { this; }));",
94 code
: "foo(function() { (() => this); }.bind(this));",
95 output
: "foo(() => { (() => this); });",
99 code
: "foo(function bar(a) { a; });",
100 output
: "foo((a) => { a; });",
104 code
: "foo(function(a) { a; });",
105 output
: "foo((a) => { a; });",
109 code
: "foo(function(arguments) { arguments; });",
110 output
: "foo((arguments) => { arguments; });",
114 code
: "foo(function() { this; });",
115 output
: null, // No fix applied
116 options
: [{ allowUnboundThis
: false }],
120 code
: "foo(function() { (() => this); });",
121 output
: null, // No fix applied
122 options
: [{ allowUnboundThis
: false }],
126 code
: "qux(function(foo, bar, baz) { return foo * 2; })",
127 output
: "qux((foo, bar, baz) => { return foo * 2; })",
131 code
: "qux(function(foo, bar, baz) { return foo * bar; }.bind(this))",
132 output
: "qux((foo, bar, baz) => { return foo * bar; })",
136 code
: "qux(function(foo, bar, baz) { return foo * this.qux; }.bind(this))",
137 output
: "qux((foo, bar, baz) => { return foo * this.qux; })",
141 code
: "foo(function() {}.bind(this, somethingElse))",
142 output
: "foo((() => {}).bind(this, somethingElse))",
146 code
: "qux(function(foo = 1, [bar = 2] = [], {qux: baz = 3} = {foo: 'bar'}) { return foo + bar; });",
147 output
: "qux((foo = 1, [bar = 2] = [], {qux: baz = 3} = {foo: 'bar'}) => { return foo + bar; });",
151 code
: "qux(function(baz, baz) { })",
152 output
: null, // Duplicate parameter names are a SyntaxError in arrow functions
156 code
: "qux(function( /* no params */ ) { })",
157 output
: "qux(( /* no params */ ) => { })",
161 code
: "qux(function( /* a */ foo /* b */ , /* c */ bar /* d */ , /* e */ baz /* f */ ) { return foo; })",
162 output
: "qux(( /* a */ foo /* b */ , /* c */ bar /* d */ , /* e */ baz /* f */ ) => { return foo; })",
166 code
: "qux(async function (foo = 1, bar = 2, baz = 3) { return baz; })",
167 output
: "qux(async (foo = 1, bar = 2, baz = 3) => { return baz; })",
168 parserOptions
: { ecmaVersion
: 8 },
172 code
: "qux(async function (foo = 1, bar = 2, baz = 3) { return this; }.bind(this))",
173 output
: "qux(async (foo = 1, bar = 2, baz = 3) => { return this; })",
174 parserOptions
: { ecmaVersion
: 8 },