]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-arrow-callback.js
first commit
[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: 6 } });
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 ],
45 invalid: [
46 {
47 code: "foo(function bar() {});",
48 output: "foo(() => {});",
49 errors
50 },
51 {
52 code: "foo(function() {});",
53 output: "foo(() => {});",
54 options: [{ allowNamedFunctions: true }],
55 errors
56 },
57 {
58 code: "foo(function bar() {});",
59 output: "foo(() => {});",
60 options: [{ allowNamedFunctions: false }],
61 errors
62 },
63 {
64 code: "foo(function() {});",
65 output: "foo(() => {});",
66 errors
67 },
68 {
69 code: "foo(nativeCb || function() {});",
70 output: "foo(nativeCb || (() => {}));",
71 errors
72 },
73 {
74 code: "foo(bar ? function() {} : function() {});",
75 output: "foo(bar ? () => {} : () => {});",
76 errors: [errors[0], errors[0]]
77 },
78 {
79 code: "foo(function() { (function() { this; }); });",
80 output: "foo(() => { (function() { this; }); });",
81 errors
82 },
83 {
84 code: "foo(function() { this; }.bind(this));",
85 output: "foo(() => { this; });",
86 errors
87 },
88 {
89 code: "foo(bar || function() { this; }.bind(this));",
90 output: "foo(bar || (() => { this; }));",
91 errors
92 },
93 {
94 code: "foo(function() { (() => this); }.bind(this));",
95 output: "foo(() => { (() => this); });",
96 errors
97 },
98 {
99 code: "foo(function bar(a) { a; });",
100 output: "foo((a) => { a; });",
101 errors
102 },
103 {
104 code: "foo(function(a) { a; });",
105 output: "foo((a) => { a; });",
106 errors
107 },
108 {
109 code: "foo(function(arguments) { arguments; });",
110 output: "foo((arguments) => { arguments; });",
111 errors
112 },
113 {
114 code: "foo(function() { this; });",
115 output: null, // No fix applied
116 options: [{ allowUnboundThis: false }],
117 errors
118 },
119 {
120 code: "foo(function() { (() => this); });",
121 output: null, // No fix applied
122 options: [{ allowUnboundThis: false }],
123 errors
124 },
125 {
126 code: "qux(function(foo, bar, baz) { return foo * 2; })",
127 output: "qux((foo, bar, baz) => { return foo * 2; })",
128 errors
129 },
130 {
131 code: "qux(function(foo, bar, baz) { return foo * bar; }.bind(this))",
132 output: "qux((foo, bar, baz) => { return foo * bar; })",
133 errors
134 },
135 {
136 code: "qux(function(foo, bar, baz) { return foo * this.qux; }.bind(this))",
137 output: "qux((foo, bar, baz) => { return foo * this.qux; })",
138 errors
139 },
140 {
141 code: "foo(function() {}.bind(this, somethingElse))",
142 output: "foo((() => {}).bind(this, somethingElse))",
143 errors
144 },
145 {
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; });",
148 errors
149 },
150 {
151 code: "qux(function(baz, baz) { })",
152 output: null, // Duplicate parameter names are a SyntaxError in arrow functions
153 errors
154 },
155 {
156 code: "qux(function( /* no params */ ) { })",
157 output: "qux(( /* no params */ ) => { })",
158 errors
159 },
160 {
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; })",
163 errors
164 },
165 {
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 },
169 errors
170 },
171 {
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 },
175 errors
176 }
177 ]
178 });