]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-extra-bind.js
121979671765d18a15174b6f57136ab453a7cbd8
[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 // Should not autofix if bind expression args have side effects
107 {
108 code: "var a = function() {}.bind(b++)",
109 output: null,
110 errors
111 },
112 {
113 code: "var a = function() {}.bind(b())",
114 output: null,
115 errors
116 },
117 {
118 code: "var a = function() {}.bind(b.c)",
119 output: null,
120 errors
121 },
122
123 // Should not autofix if it would remove comments
124 {
125 code: "var a = function() {}/**/.bind(b)",
126 output: "var a = function() {}/**/",
127 errors
128 },
129 {
130 code: "var a = function() {}/**/['bind'](b)",
131 output: "var a = function() {}/**/",
132 errors
133 },
134 {
135 code: "var a = function() {}//comment\n.bind(b)",
136 output: "var a = function() {}//comment\n",
137 errors
138 },
139 {
140 code: "var a = function() {}./**/bind(b)",
141 output: null,
142 errors
143 },
144 {
145 code: "var a = function() {}[/**/'bind'](b)",
146 output: null,
147 errors
148 },
149 {
150 code: "var a = function() {}.//\nbind(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() {}.bind(\n/**/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(b//\n)",
171 output: null,
172 errors
173 },
174 {
175 code: "var a = function() {}.bind(b\n/**/)",
176 output: null,
177 errors
178 },
179 {
180 code: "var a = function() {}.bind(b)/**/",
181 output: "var a = function() {}/**/",
182 errors
183 }
184 ]
185 });