@simplysm/lint 13.0.99 → 14.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/eslint-plugin.js +7 -10
- package/dist/eslint-plugin.js.map +1 -6
- package/dist/eslint-recommended.d.ts +2 -1
- package/dist/eslint-recommended.d.ts.map +1 -1
- package/dist/eslint-recommended.js +147 -244
- package/dist/eslint-recommended.js.map +1 -6
- package/dist/rules/no-hard-private.d.ts +6 -6
- package/dist/rules/no-hard-private.js +109 -88
- package/dist/rules/no-hard-private.js.map +1 -6
- package/dist/rules/no-subpath-imports-from-simplysm.d.ts +5 -5
- package/dist/rules/no-subpath-imports-from-simplysm.js +72 -60
- package/dist/rules/no-subpath-imports-from-simplysm.js.map +1 -6
- package/dist/rules/ts-no-throw-not-implemented-error.d.ts +5 -5
- package/dist/rules/ts-no-throw-not-implemented-error.js +92 -58
- package/dist/rules/ts-no-throw-not-implemented-error.js.map +1 -6
- package/dist/utils/create-rule.d.ts +3 -3
- package/dist/utils/create-rule.js +19 -7
- package/dist/utils/create-rule.js.map +1 -6
- package/package.json +9 -12
- package/src/eslint-recommended.ts +28 -115
- package/src/rules/no-hard-private.ts +23 -23
- package/src/rules/no-subpath-imports-from-simplysm.ts +13 -13
- package/src/rules/ts-no-throw-not-implemented-error.ts +14 -14
- package/src/utils/create-rule.ts +3 -3
- package/README.md +0 -81
- package/tests/no-hard-private.spec.ts +0 -888
- package/tests/no-subpath-imports-from-simplysm.spec.ts +0 -311
- package/tests/recommended.spec.ts +0 -145
- package/tests/ts-no-throw-not-implemented-error.spec.ts +0 -245
- package/tests/vitest.setup.ts +0 -10
|
@@ -1,888 +0,0 @@
|
|
|
1
|
-
import "./vitest.setup";
|
|
2
|
-
import { describe } from "vitest";
|
|
3
|
-
import { RuleTester } from "@typescript-eslint/rule-tester";
|
|
4
|
-
import rule from "../src/rules/no-hard-private";
|
|
5
|
-
|
|
6
|
-
const ruleTester = new RuleTester();
|
|
7
|
-
|
|
8
|
-
describe("no-hard-private rule", () => {
|
|
9
|
-
describe("allowed code (valid)", () => {
|
|
10
|
-
describe("using TypeScript private keyword", () => {
|
|
11
|
-
ruleTester.run("no-hard-private", rule, {
|
|
12
|
-
valid: [
|
|
13
|
-
{
|
|
14
|
-
code: `
|
|
15
|
-
class MyClass {
|
|
16
|
-
private _field = 1;
|
|
17
|
-
}
|
|
18
|
-
`,
|
|
19
|
-
},
|
|
20
|
-
],
|
|
21
|
-
invalid: [],
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe("using TypeScript private method", () => {
|
|
26
|
-
ruleTester.run("no-hard-private", rule, {
|
|
27
|
-
valid: [
|
|
28
|
-
{
|
|
29
|
-
code: `
|
|
30
|
-
class MyClass {
|
|
31
|
-
private _method() {
|
|
32
|
-
return 1;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
`,
|
|
36
|
-
},
|
|
37
|
-
],
|
|
38
|
-
invalid: [],
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
describe("public/protected fields are allowed", () => {
|
|
43
|
-
ruleTester.run("no-hard-private", rule, {
|
|
44
|
-
valid: [
|
|
45
|
-
{
|
|
46
|
-
code: `
|
|
47
|
-
class MyClass {
|
|
48
|
-
public field = 1;
|
|
49
|
-
protected _protectedField = 2;
|
|
50
|
-
}
|
|
51
|
-
`,
|
|
52
|
-
},
|
|
53
|
-
],
|
|
54
|
-
invalid: [],
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
describe("code that should cause errors (invalid)", () => {
|
|
60
|
-
describe("# private field usage in arrow function field", () => {
|
|
61
|
-
ruleTester.run("no-hard-private", rule, {
|
|
62
|
-
valid: [],
|
|
63
|
-
invalid: [
|
|
64
|
-
{
|
|
65
|
-
code: `
|
|
66
|
-
class MyClass {
|
|
67
|
-
#value = 1;
|
|
68
|
-
getValue = () => this.#value;
|
|
69
|
-
}
|
|
70
|
-
`,
|
|
71
|
-
output: `
|
|
72
|
-
class MyClass {
|
|
73
|
-
private _value = 1;
|
|
74
|
-
getValue = () => this._value;
|
|
75
|
-
}
|
|
76
|
-
`,
|
|
77
|
-
errors: [{ messageId: "preferSoftPrivate" }, { messageId: "preferSoftPrivate" }],
|
|
78
|
-
},
|
|
79
|
-
],
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
describe("autofix tests", () => {
|
|
85
|
-
describe("convert # private field to private _ style", () => {
|
|
86
|
-
ruleTester.run("no-hard-private", rule, {
|
|
87
|
-
valid: [],
|
|
88
|
-
invalid: [
|
|
89
|
-
{
|
|
90
|
-
code: `
|
|
91
|
-
class MyClass {
|
|
92
|
-
#field = 1;
|
|
93
|
-
}
|
|
94
|
-
`.trim(),
|
|
95
|
-
output: `
|
|
96
|
-
class MyClass {
|
|
97
|
-
private _field = 1;
|
|
98
|
-
}
|
|
99
|
-
`.trim(),
|
|
100
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
101
|
-
},
|
|
102
|
-
],
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
describe("convert # private method to private _ style", () => {
|
|
107
|
-
ruleTester.run("no-hard-private", rule, {
|
|
108
|
-
valid: [],
|
|
109
|
-
invalid: [
|
|
110
|
-
{
|
|
111
|
-
code: `
|
|
112
|
-
class MyClass {
|
|
113
|
-
#method() {
|
|
114
|
-
return 1;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
`.trim(),
|
|
118
|
-
output: `
|
|
119
|
-
class MyClass {
|
|
120
|
-
private _method() {
|
|
121
|
-
return 1;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
`.trim(),
|
|
125
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
126
|
-
},
|
|
127
|
-
],
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
describe("convert this.#field to this._field in usage", () => {
|
|
132
|
-
ruleTester.run("no-hard-private", rule, {
|
|
133
|
-
valid: [],
|
|
134
|
-
invalid: [
|
|
135
|
-
{
|
|
136
|
-
code: `
|
|
137
|
-
class MyClass {
|
|
138
|
-
#value = 1;
|
|
139
|
-
get() {
|
|
140
|
-
return this.#value;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
`.trim(),
|
|
144
|
-
output: `
|
|
145
|
-
class MyClass {
|
|
146
|
-
private _value = 1;
|
|
147
|
-
get() {
|
|
148
|
-
return this._value;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
`.trim(),
|
|
152
|
-
errors: [{ messageId: "preferSoftPrivate" }, { messageId: "preferSoftPrivate" }],
|
|
153
|
-
},
|
|
154
|
-
],
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
describe("convert static # private field", () => {
|
|
159
|
-
ruleTester.run("no-hard-private", rule, {
|
|
160
|
-
valid: [],
|
|
161
|
-
invalid: [
|
|
162
|
-
{
|
|
163
|
-
code: `
|
|
164
|
-
class MyClass {
|
|
165
|
-
static #staticField = 1;
|
|
166
|
-
}
|
|
167
|
-
`.trim(),
|
|
168
|
-
output: `
|
|
169
|
-
class MyClass {
|
|
170
|
-
private static _staticField = 1;
|
|
171
|
-
}
|
|
172
|
-
`.trim(),
|
|
173
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
174
|
-
},
|
|
175
|
-
],
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
describe("convert static # private method", () => {
|
|
180
|
-
ruleTester.run("no-hard-private", rule, {
|
|
181
|
-
valid: [],
|
|
182
|
-
invalid: [
|
|
183
|
-
{
|
|
184
|
-
code: `
|
|
185
|
-
class MyClass {
|
|
186
|
-
static #staticMethod() {
|
|
187
|
-
return 1;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
`.trim(),
|
|
191
|
-
output: `
|
|
192
|
-
class MyClass {
|
|
193
|
-
private static _staticMethod() {
|
|
194
|
-
return 1;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
`.trim(),
|
|
198
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
199
|
-
},
|
|
200
|
-
],
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
describe("convert readonly # private field", () => {
|
|
205
|
-
ruleTester.run("no-hard-private", rule, {
|
|
206
|
-
valid: [],
|
|
207
|
-
invalid: [
|
|
208
|
-
{
|
|
209
|
-
code: `
|
|
210
|
-
class MyClass {
|
|
211
|
-
readonly #field = 1;
|
|
212
|
-
}
|
|
213
|
-
`.trim(),
|
|
214
|
-
output: `
|
|
215
|
-
class MyClass {
|
|
216
|
-
private readonly _field = 1;
|
|
217
|
-
}
|
|
218
|
-
`.trim(),
|
|
219
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
220
|
-
},
|
|
221
|
-
],
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
describe("convert static readonly # private field", () => {
|
|
226
|
-
ruleTester.run("no-hard-private", rule, {
|
|
227
|
-
valid: [],
|
|
228
|
-
invalid: [
|
|
229
|
-
{
|
|
230
|
-
code: `
|
|
231
|
-
class MyClass {
|
|
232
|
-
static readonly #staticReadonlyField = 1;
|
|
233
|
-
}
|
|
234
|
-
`.trim(),
|
|
235
|
-
output: `
|
|
236
|
-
class MyClass {
|
|
237
|
-
private static readonly _staticReadonlyField = 1;
|
|
238
|
-
}
|
|
239
|
-
`.trim(),
|
|
240
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
241
|
-
},
|
|
242
|
-
],
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
describe("convert # private getter to private _ style", () => {
|
|
247
|
-
ruleTester.run("no-hard-private", rule, {
|
|
248
|
-
valid: [],
|
|
249
|
-
invalid: [
|
|
250
|
-
{
|
|
251
|
-
code: `
|
|
252
|
-
class MyClass {
|
|
253
|
-
get #value() {
|
|
254
|
-
return 1;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
`.trim(),
|
|
258
|
-
output: `
|
|
259
|
-
class MyClass {
|
|
260
|
-
private get _value() {
|
|
261
|
-
return 1;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
`.trim(),
|
|
265
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
266
|
-
},
|
|
267
|
-
],
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
describe("convert # private setter to private _ style", () => {
|
|
272
|
-
ruleTester.run("no-hard-private", rule, {
|
|
273
|
-
valid: [],
|
|
274
|
-
invalid: [
|
|
275
|
-
{
|
|
276
|
-
code: `
|
|
277
|
-
class MyClass {
|
|
278
|
-
set #value(v) {
|
|
279
|
-
this._internal = v;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
`.trim(),
|
|
283
|
-
output: `
|
|
284
|
-
class MyClass {
|
|
285
|
-
private set _value(v) {
|
|
286
|
-
this._internal = v;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
`.trim(),
|
|
290
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
291
|
-
},
|
|
292
|
-
],
|
|
293
|
-
});
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
describe("convert # private getter/setter pair", () => {
|
|
297
|
-
ruleTester.run("no-hard-private", rule, {
|
|
298
|
-
valid: [],
|
|
299
|
-
invalid: [
|
|
300
|
-
{
|
|
301
|
-
code: `
|
|
302
|
-
class MyClass {
|
|
303
|
-
get #prop() {
|
|
304
|
-
return this._internal;
|
|
305
|
-
}
|
|
306
|
-
set #prop(v) {
|
|
307
|
-
this._internal = v;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
`.trim(),
|
|
311
|
-
output: `
|
|
312
|
-
class MyClass {
|
|
313
|
-
private get _prop() {
|
|
314
|
-
return this._internal;
|
|
315
|
-
}
|
|
316
|
-
private set _prop(v) {
|
|
317
|
-
this._internal = v;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
`.trim(),
|
|
321
|
-
errors: [{ messageId: "preferSoftPrivate" }, { messageId: "preferSoftPrivate" }],
|
|
322
|
-
},
|
|
323
|
-
],
|
|
324
|
-
});
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
describe("convert multiple adjacent # private fields/methods", () => {
|
|
328
|
-
ruleTester.run("no-hard-private", rule, {
|
|
329
|
-
valid: [],
|
|
330
|
-
invalid: [
|
|
331
|
-
{
|
|
332
|
-
code: `
|
|
333
|
-
class MyClass {
|
|
334
|
-
#a;
|
|
335
|
-
#b;
|
|
336
|
-
#method1() {}
|
|
337
|
-
#method2() {}
|
|
338
|
-
}
|
|
339
|
-
`.trim(),
|
|
340
|
-
output: `
|
|
341
|
-
class MyClass {
|
|
342
|
-
private _a;
|
|
343
|
-
private _b;
|
|
344
|
-
private _method1() {}
|
|
345
|
-
private _method2() {}
|
|
346
|
-
}
|
|
347
|
-
`.trim(),
|
|
348
|
-
errors: [
|
|
349
|
-
{ messageId: "preferSoftPrivate" },
|
|
350
|
-
{ messageId: "preferSoftPrivate" },
|
|
351
|
-
{ messageId: "preferSoftPrivate" },
|
|
352
|
-
{ messageId: "preferSoftPrivate" },
|
|
353
|
-
],
|
|
354
|
-
},
|
|
355
|
-
],
|
|
356
|
-
});
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
describe("convert # private field with decorator", () => {
|
|
360
|
-
ruleTester.run("no-hard-private", rule, {
|
|
361
|
-
valid: [],
|
|
362
|
-
invalid: [
|
|
363
|
-
{
|
|
364
|
-
code: `
|
|
365
|
-
class MyClass {
|
|
366
|
-
@Decorator
|
|
367
|
-
#field = 1;
|
|
368
|
-
}
|
|
369
|
-
`.trim(),
|
|
370
|
-
output: `
|
|
371
|
-
class MyClass {
|
|
372
|
-
@Decorator
|
|
373
|
-
private _field = 1;
|
|
374
|
-
}
|
|
375
|
-
`.trim(),
|
|
376
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
377
|
-
},
|
|
378
|
-
],
|
|
379
|
-
});
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
describe("convert # private method with decorator", () => {
|
|
383
|
-
ruleTester.run("no-hard-private", rule, {
|
|
384
|
-
valid: [],
|
|
385
|
-
invalid: [
|
|
386
|
-
{
|
|
387
|
-
code: `
|
|
388
|
-
class MyClass {
|
|
389
|
-
@MethodDecorator
|
|
390
|
-
#method() {
|
|
391
|
-
return 1;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
`.trim(),
|
|
395
|
-
output: `
|
|
396
|
-
class MyClass {
|
|
397
|
-
@MethodDecorator
|
|
398
|
-
private _method() {
|
|
399
|
-
return 1;
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
`.trim(),
|
|
403
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
404
|
-
},
|
|
405
|
-
],
|
|
406
|
-
});
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
describe("convert # private accessor field", () => {
|
|
410
|
-
ruleTester.run("no-hard-private", rule, {
|
|
411
|
-
valid: [],
|
|
412
|
-
invalid: [
|
|
413
|
-
{
|
|
414
|
-
code: `
|
|
415
|
-
class MyClass {
|
|
416
|
-
accessor #value = 1;
|
|
417
|
-
}
|
|
418
|
-
`.trim(),
|
|
419
|
-
output: `
|
|
420
|
-
class MyClass {
|
|
421
|
-
private accessor _value = 1;
|
|
422
|
-
}
|
|
423
|
-
`.trim(),
|
|
424
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
425
|
-
},
|
|
426
|
-
],
|
|
427
|
-
});
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
describe("# private accessor field usage (this.#value)", () => {
|
|
431
|
-
ruleTester.run("no-hard-private", rule, {
|
|
432
|
-
valid: [],
|
|
433
|
-
invalid: [
|
|
434
|
-
{
|
|
435
|
-
code: `
|
|
436
|
-
class MyClass {
|
|
437
|
-
accessor #value = 1;
|
|
438
|
-
method() {
|
|
439
|
-
return this.#value;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
`.trim(),
|
|
443
|
-
output: `
|
|
444
|
-
class MyClass {
|
|
445
|
-
private accessor _value = 1;
|
|
446
|
-
method() {
|
|
447
|
-
return this._value;
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
`.trim(),
|
|
451
|
-
errors: [{ messageId: "preferSoftPrivate" }, { messageId: "preferSoftPrivate" }],
|
|
452
|
-
},
|
|
453
|
-
],
|
|
454
|
-
});
|
|
455
|
-
});
|
|
456
|
-
|
|
457
|
-
describe("convert static # private accessor field", () => {
|
|
458
|
-
ruleTester.run("no-hard-private", rule, {
|
|
459
|
-
valid: [],
|
|
460
|
-
invalid: [
|
|
461
|
-
{
|
|
462
|
-
code: `
|
|
463
|
-
class MyClass {
|
|
464
|
-
static accessor #staticValue = 1;
|
|
465
|
-
}
|
|
466
|
-
`.trim(),
|
|
467
|
-
output: `
|
|
468
|
-
class MyClass {
|
|
469
|
-
private static accessor _staticValue = 1;
|
|
470
|
-
}
|
|
471
|
-
`.trim(),
|
|
472
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
473
|
-
},
|
|
474
|
-
],
|
|
475
|
-
});
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
describe("convert async # private method", () => {
|
|
479
|
-
ruleTester.run("no-hard-private", rule, {
|
|
480
|
-
valid: [],
|
|
481
|
-
invalid: [
|
|
482
|
-
{
|
|
483
|
-
code: `
|
|
484
|
-
class MyClass {
|
|
485
|
-
async #asyncMethod() {
|
|
486
|
-
return await fetch("/api");
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
`.trim(),
|
|
490
|
-
output: `
|
|
491
|
-
class MyClass {
|
|
492
|
-
private async _asyncMethod() {
|
|
493
|
-
return await fetch("/api");
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
`.trim(),
|
|
497
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
498
|
-
},
|
|
499
|
-
],
|
|
500
|
-
});
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
describe("convert static async # private method", () => {
|
|
504
|
-
ruleTester.run("no-hard-private", rule, {
|
|
505
|
-
valid: [],
|
|
506
|
-
invalid: [
|
|
507
|
-
{
|
|
508
|
-
code: `
|
|
509
|
-
class MyClass {
|
|
510
|
-
static async #staticAsyncMethod() {
|
|
511
|
-
return await fetch("/api");
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
`.trim(),
|
|
515
|
-
output: `
|
|
516
|
-
class MyClass {
|
|
517
|
-
private static async _staticAsyncMethod() {
|
|
518
|
-
return await fetch("/api");
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
`.trim(),
|
|
522
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
523
|
-
},
|
|
524
|
-
],
|
|
525
|
-
});
|
|
526
|
-
});
|
|
527
|
-
|
|
528
|
-
describe("convert generator # private method", () => {
|
|
529
|
-
ruleTester.run("no-hard-private", rule, {
|
|
530
|
-
valid: [],
|
|
531
|
-
invalid: [
|
|
532
|
-
{
|
|
533
|
-
code: `
|
|
534
|
-
class MyClass {
|
|
535
|
-
*#generatorMethod() {
|
|
536
|
-
yield 1;
|
|
537
|
-
yield 2;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
`.trim(),
|
|
541
|
-
output: `
|
|
542
|
-
class MyClass {
|
|
543
|
-
private *_generatorMethod() {
|
|
544
|
-
yield 1;
|
|
545
|
-
yield 2;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
`.trim(),
|
|
549
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
550
|
-
},
|
|
551
|
-
],
|
|
552
|
-
});
|
|
553
|
-
});
|
|
554
|
-
|
|
555
|
-
describe("convert # private field with multiple decorators", () => {
|
|
556
|
-
ruleTester.run("no-hard-private", rule, {
|
|
557
|
-
valid: [],
|
|
558
|
-
invalid: [
|
|
559
|
-
{
|
|
560
|
-
code: `
|
|
561
|
-
class MyClass {
|
|
562
|
-
@Decorator1
|
|
563
|
-
@Decorator2
|
|
564
|
-
#field = 1;
|
|
565
|
-
}
|
|
566
|
-
`.trim(),
|
|
567
|
-
output: `
|
|
568
|
-
class MyClass {
|
|
569
|
-
@Decorator1
|
|
570
|
-
@Decorator2
|
|
571
|
-
private _field = 1;
|
|
572
|
-
}
|
|
573
|
-
`.trim(),
|
|
574
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
575
|
-
},
|
|
576
|
-
],
|
|
577
|
-
});
|
|
578
|
-
});
|
|
579
|
-
|
|
580
|
-
describe("convert # private field in class expression", () => {
|
|
581
|
-
ruleTester.run("no-hard-private", rule, {
|
|
582
|
-
valid: [],
|
|
583
|
-
invalid: [
|
|
584
|
-
{
|
|
585
|
-
code: `
|
|
586
|
-
const MyClass = class {
|
|
587
|
-
#field = 1;
|
|
588
|
-
};
|
|
589
|
-
`.trim(),
|
|
590
|
-
output: `
|
|
591
|
-
const MyClass = class {
|
|
592
|
-
private _field = 1;
|
|
593
|
-
};
|
|
594
|
-
`.trim(),
|
|
595
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
596
|
-
},
|
|
597
|
-
],
|
|
598
|
-
});
|
|
599
|
-
});
|
|
600
|
-
|
|
601
|
-
describe("convert # private field in nested class", () => {
|
|
602
|
-
ruleTester.run("no-hard-private", rule, {
|
|
603
|
-
valid: [],
|
|
604
|
-
invalid: [
|
|
605
|
-
{
|
|
606
|
-
code: `
|
|
607
|
-
class Outer {
|
|
608
|
-
Inner = class {
|
|
609
|
-
#innerField = 1;
|
|
610
|
-
};
|
|
611
|
-
}
|
|
612
|
-
`.trim(),
|
|
613
|
-
output: `
|
|
614
|
-
class Outer {
|
|
615
|
-
Inner = class {
|
|
616
|
-
private _innerField = 1;
|
|
617
|
-
};
|
|
618
|
-
}
|
|
619
|
-
`.trim(),
|
|
620
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
621
|
-
},
|
|
622
|
-
],
|
|
623
|
-
});
|
|
624
|
-
});
|
|
625
|
-
|
|
626
|
-
describe("convert # private field with type annotation", () => {
|
|
627
|
-
ruleTester.run("no-hard-private", rule, {
|
|
628
|
-
valid: [],
|
|
629
|
-
invalid: [
|
|
630
|
-
{
|
|
631
|
-
code: `
|
|
632
|
-
class MyClass {
|
|
633
|
-
#count: number = 0;
|
|
634
|
-
}
|
|
635
|
-
`.trim(),
|
|
636
|
-
output: `
|
|
637
|
-
class MyClass {
|
|
638
|
-
private _count: number = 0;
|
|
639
|
-
}
|
|
640
|
-
`.trim(),
|
|
641
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
642
|
-
},
|
|
643
|
-
],
|
|
644
|
-
});
|
|
645
|
-
});
|
|
646
|
-
|
|
647
|
-
describe("convert # private field with optional type", () => {
|
|
648
|
-
ruleTester.run("no-hard-private", rule, {
|
|
649
|
-
valid: [],
|
|
650
|
-
invalid: [
|
|
651
|
-
{
|
|
652
|
-
code: `
|
|
653
|
-
class MyClass {
|
|
654
|
-
#name?: string;
|
|
655
|
-
}
|
|
656
|
-
`.trim(),
|
|
657
|
-
output: `
|
|
658
|
-
class MyClass {
|
|
659
|
-
private _name?: string;
|
|
660
|
-
}
|
|
661
|
-
`.trim(),
|
|
662
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
663
|
-
},
|
|
664
|
-
],
|
|
665
|
-
});
|
|
666
|
-
});
|
|
667
|
-
|
|
668
|
-
describe("convert # private field access from other instance", () => {
|
|
669
|
-
ruleTester.run("no-hard-private", rule, {
|
|
670
|
-
valid: [],
|
|
671
|
-
invalid: [
|
|
672
|
-
{
|
|
673
|
-
code: `
|
|
674
|
-
class MyClass {
|
|
675
|
-
#value = 1;
|
|
676
|
-
compare(other: MyClass) {
|
|
677
|
-
return other.#value;
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
`.trim(),
|
|
681
|
-
output: `
|
|
682
|
-
class MyClass {
|
|
683
|
-
private _value = 1;
|
|
684
|
-
compare(other: MyClass) {
|
|
685
|
-
return other._value;
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
`.trim(),
|
|
689
|
-
errors: [{ messageId: "preferSoftPrivate" }, { messageId: "preferSoftPrivate" }],
|
|
690
|
-
},
|
|
691
|
-
],
|
|
692
|
-
});
|
|
693
|
-
});
|
|
694
|
-
|
|
695
|
-
describe("convert # private field comparison between same class instances", () => {
|
|
696
|
-
ruleTester.run("no-hard-private", rule, {
|
|
697
|
-
valid: [],
|
|
698
|
-
invalid: [
|
|
699
|
-
{
|
|
700
|
-
code: `
|
|
701
|
-
class MyClass {
|
|
702
|
-
#id = 1;
|
|
703
|
-
equals(other: MyClass) {
|
|
704
|
-
return this.#id === other.#id;
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
`.trim(),
|
|
708
|
-
output: `
|
|
709
|
-
class MyClass {
|
|
710
|
-
private _id = 1;
|
|
711
|
-
equals(other: MyClass) {
|
|
712
|
-
return this._id === other._id;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
`.trim(),
|
|
716
|
-
errors: [
|
|
717
|
-
{ messageId: "preferSoftPrivate" },
|
|
718
|
-
{ messageId: "preferSoftPrivate" },
|
|
719
|
-
{ messageId: "preferSoftPrivate" },
|
|
720
|
-
],
|
|
721
|
-
},
|
|
722
|
-
],
|
|
723
|
-
});
|
|
724
|
-
});
|
|
725
|
-
|
|
726
|
-
describe("convert # private field with existing accessibility (no duplicate private)", () => {
|
|
727
|
-
ruleTester.run("no-hard-private", rule, {
|
|
728
|
-
valid: [],
|
|
729
|
-
invalid: [
|
|
730
|
-
{
|
|
731
|
-
code: `
|
|
732
|
-
class MyClass {
|
|
733
|
-
private #field = 1;
|
|
734
|
-
}
|
|
735
|
-
`.trim(),
|
|
736
|
-
output: `
|
|
737
|
-
class MyClass {
|
|
738
|
-
private _field = 1;
|
|
739
|
-
}
|
|
740
|
-
`.trim(),
|
|
741
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
742
|
-
},
|
|
743
|
-
{
|
|
744
|
-
code: `
|
|
745
|
-
class MyClass {
|
|
746
|
-
public #field = 1;
|
|
747
|
-
}
|
|
748
|
-
`.trim(),
|
|
749
|
-
output: `
|
|
750
|
-
class MyClass {
|
|
751
|
-
public _field = 1;
|
|
752
|
-
}
|
|
753
|
-
`.trim(),
|
|
754
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
755
|
-
},
|
|
756
|
-
{
|
|
757
|
-
code: `
|
|
758
|
-
class MyClass {
|
|
759
|
-
protected #field = 1;
|
|
760
|
-
}
|
|
761
|
-
`.trim(),
|
|
762
|
-
output: `
|
|
763
|
-
class MyClass {
|
|
764
|
-
protected _field = 1;
|
|
765
|
-
}
|
|
766
|
-
`.trim(),
|
|
767
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
768
|
-
},
|
|
769
|
-
],
|
|
770
|
-
});
|
|
771
|
-
});
|
|
772
|
-
|
|
773
|
-
describe("convert # private method with existing accessibility", () => {
|
|
774
|
-
ruleTester.run("no-hard-private", rule, {
|
|
775
|
-
valid: [],
|
|
776
|
-
invalid: [
|
|
777
|
-
{
|
|
778
|
-
code: `
|
|
779
|
-
class MyClass {
|
|
780
|
-
private #method() {
|
|
781
|
-
return 1;
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
`.trim(),
|
|
785
|
-
output: `
|
|
786
|
-
class MyClass {
|
|
787
|
-
private _method() {
|
|
788
|
-
return 1;
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
`.trim(),
|
|
792
|
-
errors: [{ messageId: "preferSoftPrivate" }],
|
|
793
|
-
},
|
|
794
|
-
],
|
|
795
|
-
});
|
|
796
|
-
});
|
|
797
|
-
|
|
798
|
-
describe("convert # private field in inherited class", () => {
|
|
799
|
-
ruleTester.run("no-hard-private", rule, {
|
|
800
|
-
valid: [],
|
|
801
|
-
invalid: [
|
|
802
|
-
{
|
|
803
|
-
code: `
|
|
804
|
-
class Parent {
|
|
805
|
-
#parentField = 1;
|
|
806
|
-
}
|
|
807
|
-
class Child extends Parent {
|
|
808
|
-
#childField = 2;
|
|
809
|
-
}
|
|
810
|
-
`.trim(),
|
|
811
|
-
output: `
|
|
812
|
-
class Parent {
|
|
813
|
-
private _parentField = 1;
|
|
814
|
-
}
|
|
815
|
-
class Child extends Parent {
|
|
816
|
-
private _childField = 2;
|
|
817
|
-
}
|
|
818
|
-
`.trim(),
|
|
819
|
-
errors: [{ messageId: "preferSoftPrivate" }, { messageId: "preferSoftPrivate" }],
|
|
820
|
-
},
|
|
821
|
-
],
|
|
822
|
-
});
|
|
823
|
-
});
|
|
824
|
-
|
|
825
|
-
describe("no autofix when name conflict", () => {
|
|
826
|
-
// When converting #field to _field, if existing _field exists, show conflict warning only without autofix.
|
|
827
|
-
ruleTester.run("no-hard-private", rule, {
|
|
828
|
-
valid: [],
|
|
829
|
-
invalid: [
|
|
830
|
-
{
|
|
831
|
-
code: `
|
|
832
|
-
class MyClass {
|
|
833
|
-
_field = 1;
|
|
834
|
-
#field = 2;
|
|
835
|
-
}
|
|
836
|
-
`.trim(),
|
|
837
|
-
// no output = no autofix applied
|
|
838
|
-
errors: [{ messageId: "nameConflict", data: { name: "field" } }],
|
|
839
|
-
},
|
|
840
|
-
],
|
|
841
|
-
});
|
|
842
|
-
});
|
|
843
|
-
|
|
844
|
-
describe("name conflict - method", () => {
|
|
845
|
-
ruleTester.run("no-hard-private", rule, {
|
|
846
|
-
valid: [],
|
|
847
|
-
invalid: [
|
|
848
|
-
{
|
|
849
|
-
code: `
|
|
850
|
-
class MyClass {
|
|
851
|
-
_method() {}
|
|
852
|
-
#method() {}
|
|
853
|
-
}
|
|
854
|
-
`.trim(),
|
|
855
|
-
errors: [{ messageId: "nameConflict", data: { name: "method" } }],
|
|
856
|
-
},
|
|
857
|
-
],
|
|
858
|
-
});
|
|
859
|
-
});
|
|
860
|
-
|
|
861
|
-
describe("static # private field usage (MyClass.#staticField)", () => {
|
|
862
|
-
ruleTester.run("no-hard-private", rule, {
|
|
863
|
-
valid: [],
|
|
864
|
-
invalid: [
|
|
865
|
-
{
|
|
866
|
-
code: `
|
|
867
|
-
class MyClass {
|
|
868
|
-
static #staticValue = 1;
|
|
869
|
-
static getStatic() {
|
|
870
|
-
return MyClass.#staticValue;
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
`.trim(),
|
|
874
|
-
output: `
|
|
875
|
-
class MyClass {
|
|
876
|
-
private static _staticValue = 1;
|
|
877
|
-
static getStatic() {
|
|
878
|
-
return MyClass._staticValue;
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
`.trim(),
|
|
882
|
-
errors: [{ messageId: "preferSoftPrivate" }, { messageId: "preferSoftPrivate" }],
|
|
883
|
-
},
|
|
884
|
-
],
|
|
885
|
-
});
|
|
886
|
-
});
|
|
887
|
-
});
|
|
888
|
-
});
|