]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-extra-bind.js
import 7.12.1 upstream release
[pve-eslint.git] / eslint / tests / lib / rules / no-extra-bind.js
1 /**
2 * @fileoverview Tests for no-extra-bind rule
3 * @author Bence Dányi <bence@danyi.me>
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/no-extra-bind"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14 //------------------------------------------------------------------------------
15 // Tests
16 //------------------------------------------------------------------------------
17
18 const ruleTester = new RuleTester();
19 const errors = [{ messageId: "unexpected", type: "CallExpression" }];
20
21 ruleTester.run("no-extra-bind", rule, {
22 valid: [
23 "var a = function(b) { return b }.bind(c, d)",
24 { code: "var a = function(b) { return b }.bind(...c)", parserOptions: { ecmaVersion: 6 } },
25 "var a = function() { this.b }()",
26 "var a = function() { this.b }.foo()",
27 "var a = f.bind(a)",
28 "var a = function() { return this.b }.bind(c)",
29 { code: "var a = (() => { return b }).bind(c, d)", parserOptions: { ecmaVersion: 6 } },
30 "(function() { (function() { this.b }.bind(this)) }.bind(c))",
31 "var a = function() { return 1; }[bind](b)",
32 { code: "var a = function() { return 1; }[`bi${n}d`](b)", parserOptions: { ecmaVersion: 6 } },
33 { code: "var a = function() { return () => this; }.bind(b)", parserOptions: { ecmaVersion: 6 } }
34 ],
35 invalid: [
36 {
37 code: "var a = function() { return 1; }.bind(b)",
38 output: "var a = function() { return 1; }",
39 errors: [{
40 messageId: "unexpected",
41 type: "CallExpression",
42 line: 1,
43 column: 34,
44 endLine: 1,
45 endColumn: 38
46 }]
47 },
48 {
49 code: "var a = function() { return 1; }['bind'](b)",
50 output: "var a = function() { return 1; }",
51 errors: [{
52 messageId: "unexpected",
53 type: "CallExpression",
54 line: 1,
55 column: 34,
56 endLine: 1,
57 endColumn: 40
58 }]
59 },
60 {
61 code: "var a = function() { return 1; }[`bind`](b)",
62 output: "var a = function() { return 1; }",
63 parserOptions: { ecmaVersion: 6 },
64 errors: [{
65 messageId: "unexpected",
66 type: "CallExpression",
67 line: 1,
68 column: 34,
69 endLine: 1,
70 endColumn: 40
71 }]
72 },
73 {
74 code: "var a = (() => { return 1; }).bind(b)",
75 output: "var a = (() => { return 1; })",
76 parserOptions: { ecmaVersion: 6 },
77 errors
78 },
79 {
80 code: "var a = (() => { return this; }).bind(b)",
81 output: "var a = (() => { return this; })",
82 parserOptions: { ecmaVersion: 6 },
83 errors
84 },
85 {
86 code: "var a = function() { (function(){ this.c }) }.bind(b)",
87 output: "var a = function() { (function(){ this.c }) }",
88 errors
89 },
90 {
91 code: "var a = function() { function c(){ this.d } }.bind(b)",
92 output: "var a = function() { function c(){ this.d } }",
93 errors
94 },
95 {
96 code: "var a = function() { return 1; }.bind(this)",
97 output: "var a = function() { return 1; }",
98 errors
99 },
100 {
101 code: "var a = function() { (function(){ (function(){ this.d }.bind(c)) }) }.bind(b)",
102 output: "var a = function() { (function(){ (function(){ this.d }.bind(c)) }) }",
103 errors: [{ messageId: "unexpected", type: "CallExpression", column: 71 }]
104 },
105 {
106 code: "var a = (function() { return 1; }).bind(this)",
107 output: "var a = (function() { return 1; })",
108 errors
109 },
110 {
111 code: "var a = (function() { return 1; }.bind)(this)",
112 output: "var a = (function() { return 1; })",
113 errors
114 },
115
116 // Should not autofix if bind expression args have side effects
117 {
118 code: "var a = function() {}.bind(b++)",
119 output: null,
120 errors
121 },
122 {
123 code: "var a = function() {}.bind(b())",
124 output: null,
125 errors
126 },
127 {
128 code: "var a = function() {}.bind(b.c)",
129 output: null,
130 errors
131 },
132
133 // Should not autofix if it would remove comments
134 {
135 code: "var a = function() {}/**/.bind(b)",
136 output: "var a = function() {}/**/",
137 errors
138 },
139 {
140 code: "var a = function() {}/**/['bind'](b)",
141 output: "var a = function() {}/**/",
142 errors
143 },
144 {
145 code: "var a = function() {}//comment\n.bind(b)",
146 output: "var a = function() {}//comment\n",
147 errors
148 },
149 {
150 code: "var a = function() {}./**/bind(b)",
151 output: null,
152 errors
153 },
154 {
155 code: "var a = function() {}[/**/'bind'](b)",
156 output: null,
157 errors
158 },
159 {
160 code: "var a = function() {}.//\nbind(b)",
161 output: null,
162 errors
163 },
164 {
165 code: "var a = function() {}.bind/**/(b)",
166 output: null,
167 errors
168 },
169 {
170 code: "var a = function() {}.bind(\n/**/b)",
171 output: null,
172 errors
173 },
174 {
175 code: "var a = function() {}.bind(b/**/)",
176 output: null,
177 errors
178 },
179 {
180 code: "var a = function() {}.bind(b//\n)",
181 output: null,
182 errors
183 },
184 {
185 code: "var a = function() {}.bind(b\n/**/)",
186 output: null,
187 errors
188 },
189 {
190 code: "var a = function() {}.bind(b)/**/",
191 output: "var a = function() {}/**/",
192 errors
193 },
194
195 // Optional chaining
196 {
197 code: "var a = function() { return 1; }.bind?.(b)",
198 output: "var a = function() { return 1; }",
199 parserOptions: { ecmaVersion: 2020 },
200 errors: [{ messageId: "unexpected" }]
201 },
202 {
203 code: "var a = function() { return 1; }?.bind(b)",
204 output: "var a = function() { return 1; }",
205 parserOptions: { ecmaVersion: 2020 },
206 errors: [{ messageId: "unexpected" }]
207 },
208 {
209 code: "var a = (function() { return 1; }?.bind)(b)",
210 output: "var a = (function() { return 1; })",
211 parserOptions: { ecmaVersion: 2020 },
212 errors: [{ messageId: "unexpected" }]
213 },
214 {
215 code: "var a = function() { return 1; }['bind']?.(b)",
216 output: "var a = function() { return 1; }",
217 parserOptions: { ecmaVersion: 2020 },
218 errors: [{ messageId: "unexpected" }]
219 },
220 {
221 code: "var a = function() { return 1; }?.['bind'](b)",
222 output: "var a = function() { return 1; }",
223 parserOptions: { ecmaVersion: 2020 },
224 errors: [{ messageId: "unexpected" }]
225 },
226 {
227 code: "var a = (function() { return 1; }?.['bind'])(b)",
228 output: "var a = (function() { return 1; })",
229 parserOptions: { ecmaVersion: 2020 },
230 errors: [{ messageId: "unexpected" }]
231 }
232 ]
233 });