/
class-methods-use-this.js
216 lines (210 loc) · 10.3 KB
/
class-methods-use-this.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @fileoverview Tests for class-methods-use-this rule.
* @author Patrick Williams
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/class-methods-use-this");
const { RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("class-methods-use-this", rule, {
valid: [
{ code: "class A { constructor() {} }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { foo() {this} }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { foo() {this.bar = 'bar';} }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { foo() {bar(this);} }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A extends B { foo() {super.foo();} }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { foo() { if(true) { return this; } } }", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { static foo() {} }", parserOptions: { ecmaVersion: 6 } },
{ code: "({ a(){} });", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { foo() { () => this; } }", parserOptions: { ecmaVersion: 6 } },
{ code: "({ a: function () {} });", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { foo() {this} bar() {} }", options: [{ exceptMethods: ["bar"] }], parserOptions: { ecmaVersion: 6 } },
{ code: "class A { \"foo\"() { } }", options: [{ exceptMethods: ["foo"] }], parserOptions: { ecmaVersion: 6 } },
{ code: "class A { 42() { } }", options: [{ exceptMethods: ["42"] }], parserOptions: { ecmaVersion: 6 } },
{ code: "class A { foo = function() {this} }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { foo = () => {this} }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { foo = () => {super.toString} }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { static foo = function() {} }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { static foo = () => {} }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { #bar() {} }", options: [{ exceptMethods: ["#bar"] }], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { foo = function () {} }", options: [{ enforceForClassFields: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { foo = () => {} }", options: [{ enforceForClassFields: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { foo() { return class { [this.foo] = 1 }; } }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { static {} }", parserOptions: { ecmaVersion: 2022 } }
],
invalid: [
{
code: "class A { foo() {} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {/**this**/} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {var a = function () {this};} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {var a = function () {var b = function(){this}};} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {window.this} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {that.this = 'this';} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() { () => undefined; } }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {} bar() {} }",
options: [{ exceptMethods: ["bar"] }],
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {} hasOwnProperty() {} }",
options: [{ exceptMethods: ["foo"] }],
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 20, messageId: "missingThis", data: { name: "method 'hasOwnProperty'" } }
]
},
{
code: "class A { [foo]() {} }",
options: [{ exceptMethods: ["foo"] }],
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 11, messageId: "missingThis", data: { name: "method" } }
]
},
{
code: "class A { #foo() { } foo() {} #bar() {} }",
options: [{ exceptMethods: ["#foo"] }],
parserOptions: { ecmaVersion: 2022 },
errors: [
{ type: "FunctionExpression", line: 1, column: 22, messageId: "missingThis", data: { name: "method 'foo'" } },
{ type: "FunctionExpression", line: 1, column: 31, messageId: "missingThis", data: { name: "private method #bar" } }
]
},
{
code: "class A { foo(){} 'bar'(){} 123(){} [`baz`](){} [a](){} [f(a)](){} get quux(){} set[a](b){} *quuux(){} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ messageId: "missingThis", data: { name: "method 'foo'" }, type: "FunctionExpression", column: 11 },
{ messageId: "missingThis", data: { name: "method 'bar'" }, type: "FunctionExpression", column: 19 },
{ messageId: "missingThis", data: { name: "method '123'" }, type: "FunctionExpression", column: 29 },
{ messageId: "missingThis", data: { name: "method 'baz'" }, type: "FunctionExpression", column: 37 },
{ messageId: "missingThis", data: { name: "method" }, type: "FunctionExpression", column: 49 },
{ messageId: "missingThis", data: { name: "method" }, type: "FunctionExpression", column: 57 },
{ messageId: "missingThis", data: { name: "getter 'quux'" }, type: "FunctionExpression", column: 68 },
{ messageId: "missingThis", data: { name: "setter" }, type: "FunctionExpression", column: 81 },
{ messageId: "missingThis", data: { name: "generator method 'quuux'" }, type: "FunctionExpression", column: 93 }
]
},
{
code: "class A { foo = function() {} }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "method 'foo'" }, column: 11, endColumn: 25 }
]
},
{
code: "class A { foo = () => {} }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "method 'foo'" }, column: 11, endColumn: 17 }
]
},
{
code: "class A { #foo = function() {} }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "private method #foo" }, column: 11, endColumn: 26 }
]
},
{
code: "class A { #foo = () => {} }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "private method #foo" }, column: 11, endColumn: 18 }
]
},
{
code: "class A { #foo() {} }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "private method #foo" }, column: 11, endColumn: 15 }
]
},
{
code: "class A { get #foo() {} }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "private getter #foo" }, column: 11, endColumn: 19 }
]
},
{
code: "class A { set #foo(x) {} }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "private setter #foo" }, column: 11, endColumn: 19 }
]
},
{
code: "class A { foo () { return class { foo = this }; } }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "method 'foo'" }, column: 11, endColumn: 15 }
]
},
{
code: "class A { foo () { return function () { foo = this }; } }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "method 'foo'" }, column: 11, endColumn: 15 }
]
},
{
code: "class A { foo () { return class { static { this; } } } }",
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "missingThis", data: { name: "method 'foo'" }, column: 11, endColumn: 15 }
]
}
]
});