forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-unused-services.js
585 lines (570 loc) · 22.6 KB
/
no-unused-services.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require('../../../lib/rules/no-unused-services');
const RuleTester = require('eslint').RuleTester;
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
parser: require.resolve('@babel/eslint-parser'),
});
const SERVICE_NAME = 'fooName';
const SERVICE_IMPORT = "import {inject as service} from '@ember/service';";
const NEW_SERVICE_IMPORT = "import {service} from '@ember/service';";
const EO_IMPORTS = "import {computed, get, getProperties, observer} from '@ember/object';";
const RENAMED_EO_IMPORTS =
"import {computed as cp, get as g, getProperties as gp, observer as ob} from '@ember/object';";
const ALIAS_IMPORT = "import {alias} from '@ember/object/computed';";
const RENAMED_ALIAS_IMPORT = "import {alias as al} from '@ember/object/computed';";
const OBSERVES_IMPORT = "import {observes} from '@ember-decorators/object';";
const RENAMED_OBSERVES_IMPORT = "import {observes as obs} from '@ember-decorators/object';";
const EMBER_IMPORT = "import Ember from 'ember';";
const RENAMED_EMBER_IMPORT = "import Em from 'ember';";
/**
* Generate an array of usecases using the given property name
* @param {String} propertyName The given property name to access
* @returns {Array}
*/
function generateUseCasesFor(propertyName) {
return [
`this.${propertyName};`,
`this.${propertyName}[0];`,
`this.${propertyName}.prop;`,
`this.${propertyName}.func();`,
`this.get('${propertyName}');`,
`this.get('${propertyName}.prop');`,
`this.getProperties('a', '${propertyName}');`,
`this.getProperties('a', '${propertyName}.prop');`,
`this.getProperties(['a', '${propertyName}']);`,
`this.getProperties(['a', '${propertyName}.prop']);`,
`const { a, b, ${propertyName} } = this;`,
`let { c, ${propertyName} : prop, d } = this;`,
];
}
/**
* Generate an array of usecases with Ember.get and Ember.getProperties using the given property name
* @param {String} propertyName The given property name to access
* @param {Boolean} renamed Whether or not the imports are renamed
* @returns {Array}
*/
function generateEmberObjectUseCasesFor(propertyName, renamed = false) {
const getName = renamed ? 'g' : 'get';
const getPropertiesName = renamed ? 'gp' : 'getProperties';
return [
`${getName}(this, '${propertyName}');`,
`${getName}(this, '${propertyName}.prop');`,
`${getPropertiesName}(this, 'a', '${propertyName}');`,
`${getPropertiesName}(this, 'a', '${propertyName}.prop');`,
`${getPropertiesName}(this, ['a', '${propertyName}']);`,
`${getPropertiesName}(this, ['a', '${propertyName}.prop']);`,
];
}
/**
* Generate an array of usecases with a computed macro `alias` using the given property name
* @param {String} propertyName The given property name to access
* @param {Boolean} renamed Whether or not the imports are renamed
* @returns {Array}
*/
function generateMacroUseCasesFor(propertyName, renamed = false) {
const aliasName = renamed ? 'al' : 'alias';
const aliasImport = renamed ? RENAMED_ALIAS_IMPORT : ALIAS_IMPORT;
return [
`${SERVICE_IMPORT}${aliasImport} class MyClass { @service() ${propertyName}; @${aliasName}('${propertyName}.prop') someAlias; }`,
`${SERVICE_IMPORT}${aliasImport} Component.extend({ ${SERVICE_NAME}: service(), someAlias: ${aliasName}('${propertyName}.prop') });`,
];
}
/**
* Generate an array of usecases with a computed property using the given property name
* @param {String} propertyName The given property name to access
* @param {Boolean} renamed Whether or not the imports are renamed
* @returns {Array}
*/
function generateComputedUseCasesFor(propertyName, renamed = false) {
const computedName = renamed ? 'cp' : 'computed';
const computedImport = renamed ? RENAMED_EO_IMPORTS : EO_IMPORTS;
const emberName = renamed ? 'Em' : 'Ember';
const emberImport = renamed ? RENAMED_EMBER_IMPORT : EMBER_IMPORT;
return [
`${SERVICE_IMPORT}${computedImport} class MyClass { @service() ${propertyName}; @${computedName}('${propertyName}.prop') get someComputed() {} }`,
`${SERVICE_IMPORT}${computedImport} Component.extend({ ${SERVICE_NAME}: service(), someComputed: ${computedName}('${propertyName}.prop', ()=>{}) });`,
`${SERVICE_IMPORT}${computedImport} Component.extend({ ${SERVICE_NAME}: service(), someComputed: ${computedName}.alias('${propertyName}.prop', ()=>{}) });`,
`${SERVICE_IMPORT}${emberImport} Component.extend({ ${SERVICE_NAME}: service(), someComputed: ${emberName}.computed('${propertyName}.prop', ()=>{}) });`,
`${SERVICE_IMPORT}${emberImport} Component.extend({ ${SERVICE_NAME}: service(), someComputed: ${emberName}.computed.alias('${propertyName}.prop', ()=>{}) });`,
];
}
/**
* Generate an array of usecases with an observer using the given property name
* @param {String} propertyName The given property name to access
* @param {Boolean} renamed Whether or not the imports are renamed
* @returns {Array}
*/
function generateObserverUseCasesFor(propertyName, renamed = false) {
const observerName = renamed ? 'ob' : 'observer';
const observerImport = renamed ? RENAMED_EO_IMPORTS : EO_IMPORTS;
const observesName = renamed ? 'obs' : 'observes';
const observesImport = renamed ? RENAMED_OBSERVES_IMPORT : OBSERVES_IMPORT;
const emberName = renamed ? 'Em' : 'Ember';
const emberImport = renamed ? RENAMED_EMBER_IMPORT : EMBER_IMPORT;
return [
`${SERVICE_IMPORT}${observerImport} Component.extend({ ${SERVICE_NAME}: service(), someObserved: ${observerName}('${propertyName}.prop', ()=>{}) });`,
`${SERVICE_IMPORT}${observerImport} Component.extend({ ${SERVICE_NAME}: service(), someObserved: on('init', ${observerName}('${propertyName}.prop', ()=>{})) });`,
`${SERVICE_IMPORT}${emberImport} Component.extend({ ${SERVICE_NAME}: service(), someObserved: ${emberName}.observer('${propertyName}.prop', ()=>{}) });`,
`${SERVICE_IMPORT}${emberImport} Component.extend({ ${SERVICE_NAME}: service(), someObserved: on('init', ${emberName}.observer('${propertyName}.prop', ()=>{})) });`,
`${SERVICE_IMPORT}${observesImport} class MyClass { @service() ${propertyName}; @${observesName}('${propertyName}.prop') get someVal() {} }`,
];
}
/**
* Generate an array of valid test cases
* @returns {Array}
*/
function generateValid(importString = SERVICE_IMPORT) {
const valid = [];
const useCases = generateUseCasesFor(SERVICE_NAME);
for (const use of useCases) {
valid.push(
`${importString} class MyClass { @service('foo') ${SERVICE_NAME}; fooFunc() {${use}} }`,
`${importString} class MyClass { @service() ${SERVICE_NAME}; fooFunc() {${use}} }`,
`${importString} class MyClass { @service() '${SERVICE_NAME}'; fooFunc() {${use}} }`,
`${importString} Component.extend({ ${SERVICE_NAME}: service('foo'), fooFunc() {${use}} });`,
`${importString} Component.extend({ ${SERVICE_NAME}: service(), fooFunc() {${use}} });`,
`${importString} Component.extend({ '${SERVICE_NAME}': service(), fooFunc() {${use}} });`
);
}
const emberObjectUseCases = [
generateEmberObjectUseCasesFor(SERVICE_NAME),
generateEmberObjectUseCasesFor(SERVICE_NAME, true),
];
for (const [idx, useCases] of emberObjectUseCases.entries()) {
const imports = idx === 0 ? EO_IMPORTS : RENAMED_EO_IMPORTS;
for (const use of useCases) {
valid.push(
`${importString}${imports} class MyClass { @service() ${SERVICE_NAME}; fooFunc() {${use}} }`,
`${importString}${imports} Component.extend({ ${SERVICE_NAME}: service(), fooFunc() {${use}} });`
);
}
}
return valid;
}
// Testing for unrelated props + some edge cases
const unrelatedPropUses = generateUseCasesFor('unrelatedProp');
const edgeCases = ['let foo;', `this.prop.${SERVICE_NAME};`];
const nonUses = [...unrelatedPropUses, ...edgeCases].join('');
const emberObjectUses1 = generateEmberObjectUseCasesFor(SERVICE_NAME).join('');
const emberObjectUses2 = generateEmberObjectUseCasesFor('unrelatedProp').join('');
ruleTester.run('no-unused-services', rule, {
valid: [
...generateValid(),
...generateValid(NEW_SERVICE_IMPORT),
...generateMacroUseCasesFor(SERVICE_NAME),
...generateMacroUseCasesFor(SERVICE_NAME, true),
...generateComputedUseCasesFor(SERVICE_NAME),
...generateComputedUseCasesFor(SERVICE_NAME, true),
...generateObserverUseCasesFor(SERVICE_NAME),
...generateObserverUseCasesFor(SERVICE_NAME, true),
`class MyClass { @service() ${SERVICE_NAME}; }`,
`Component.extend({ ${SERVICE_NAME}: service() });`,
`Component.extend({ ${SERVICE_NAME}: Ember.inject.service() });`,
`${SERVICE_IMPORT} class MyClass {}`,
`${SERVICE_IMPORT} Component.extend({});`,
'const foo = service();',
],
invalid: [
{
code: `${SERVICE_IMPORT} class MyClass { @service('foo') ${SERVICE_NAME}; fooFunc() {${nonUses}} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} class MyClass { fooFunc() {${nonUses}} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
// With new import
{
code: `${NEW_SERVICE_IMPORT} class MyClass { @service('foo') ${SERVICE_NAME}; fooFunc() {${nonUses}} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${NEW_SERVICE_IMPORT} class MyClass { fooFunc() {${nonUses}} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; fooFunc() {${nonUses}} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} class MyClass { fooFunc() {${nonUses}} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service('foo'), fooFunc() {${nonUses}} });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} Component.extend({ fooFunc() {${nonUses}} });`,
},
],
type: 'Property',
},
],
},
{
code: `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service('foo') });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} Component.extend({ });`,
},
],
type: 'Property',
},
],
},
{
code: `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), fooFunc() {${nonUses}} });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} Component.extend({ fooFunc() {${nonUses}} });`,
},
],
type: 'Property',
},
],
},
{
code: `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service() });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} Component.extend({ });`,
},
],
type: 'Property',
},
],
},
/* Using get/getProperties without @ember/object import */
{
code: `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; fooFunc() {${emberObjectUses1}} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} class MyClass { fooFunc() {${emberObjectUses1}} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), fooFunc() {${emberObjectUses1}} });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} Component.extend({ fooFunc() {${emberObjectUses1}} });`,
},
],
type: 'Property',
},
],
},
/* Using get/getProperties with @ember/object import for an unrelatedProp */
{
code: `${SERVICE_IMPORT}${EO_IMPORTS} class MyClass { @service() ${SERVICE_NAME}; fooFunc() {${emberObjectUses2}} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT}${EO_IMPORTS} class MyClass { fooFunc() {${emberObjectUses2}} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT}${EO_IMPORTS} Component.extend({ ${SERVICE_NAME}: service(), fooFunc() {${emberObjectUses2}} });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT}${EO_IMPORTS} Component.extend({ fooFunc() {${emberObjectUses2}} });`,
},
],
type: 'Property',
},
],
},
/* Using computed props and macros without the imports */
{
code: `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; @alias('${SERVICE_NAME}') someAlias; @computed('${SERVICE_NAME}.prop') get someComputed() {} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} class MyClass { @alias('${SERVICE_NAME}') someAlias; @computed('${SERVICE_NAME}.prop') get someComputed() {} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), someAlias1: alias('${SERVICE_NAME}'), someAlias2: computed.alias('${SERVICE_NAME}.prop'), someComputed: computed('${SERVICE_NAME}.prop', ()=>{}) });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} Component.extend({ someAlias1: alias('${SERVICE_NAME}'), someAlias2: computed.alias('${SERVICE_NAME}.prop'), someComputed: computed('${SERVICE_NAME}.prop', ()=>{}) });`,
},
],
type: 'Property',
},
],
},
/* Using computed props and macros with the imports for an unrelatedProp */
{
code: `${SERVICE_IMPORT}${EO_IMPORTS}${ALIAS_IMPORT} class MyClass { @service() ${SERVICE_NAME}; @alias('unrelatedProp', '${SERVICE_NAME}') someAlias; @computed('unrelatedProp.prop') get someComputed() {} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT}${EO_IMPORTS}${ALIAS_IMPORT} class MyClass { @alias('unrelatedProp', '${SERVICE_NAME}') someAlias; @computed('unrelatedProp.prop') get someComputed() {} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT}${EO_IMPORTS}${ALIAS_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), someAlias1: alias('unrelatedProp', '${SERVICE_NAME}'), someAlias2: computed.alias('unrelatedProp.prop'), someComputed: computed('unrelatedProp.prop', ()=>{}) });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT}${EO_IMPORTS}${ALIAS_IMPORT} Component.extend({ someAlias1: alias('unrelatedProp', '${SERVICE_NAME}'), someAlias2: computed.alias('unrelatedProp.prop'), someComputed: computed('unrelatedProp.prop', ()=>{}) });`,
},
],
type: 'Property',
},
],
},
{
code: `${SERVICE_IMPORT}${EO_IMPORTS}${ALIAS_IMPORT} class MyClass { @service() ${SERVICE_NAME}; @alias(${SERVICE_NAME}) someAlias; @computed(${SERVICE_NAME}) get someComputed() {} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT}${EO_IMPORTS}${ALIAS_IMPORT} class MyClass { @alias(${SERVICE_NAME}) someAlias; @computed(${SERVICE_NAME}) get someComputed() {} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT}${EO_IMPORTS}${ALIAS_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), someAlias1: alias(${SERVICE_NAME}), someAlias2: computed.alias(${SERVICE_NAME}), someComputed: computed(${SERVICE_NAME}, ()=>{}) });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT}${EO_IMPORTS}${ALIAS_IMPORT} Component.extend({ someAlias1: alias(${SERVICE_NAME}), someAlias2: computed.alias(${SERVICE_NAME}), someComputed: computed(${SERVICE_NAME}, ()=>{}) });`,
},
],
type: 'Property',
},
],
},
/* Using dummy macros that don't have dependent key props */
{
code: `${SERVICE_IMPORT} import {foobar} from '@ember/object/computed'; class MyClass { @service() ${SERVICE_NAME}; @foobar('${SERVICE_NAME}') someFoobar; }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} import {foobar} from '@ember/object/computed'; class MyClass { @foobar('${SERVICE_NAME}') someFoobar; }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT} import {foobar} from '@ember/object/computed'; Component.extend({ ${SERVICE_NAME}: service(), someFoobar: foobar('${SERVICE_NAME}') });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} import {foobar} from '@ember/object/computed'; Component.extend({ someFoobar: foobar('${SERVICE_NAME}') });`,
},
],
type: 'Property',
},
],
},
{
code: `${SERVICE_IMPORT}${EMBER_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), someFoobar: Ember.computed.foobar('${SERVICE_NAME}') });`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT}${EMBER_IMPORT} Component.extend({ someFoobar: Ember.computed.foobar('${SERVICE_NAME}') });`,
},
],
type: 'Property',
},
],
},
/* Multiple classes */
{
code: `${SERVICE_IMPORT} class MyClass1 { @service() ${SERVICE_NAME}; } class MyClass2 { fooFunc() {this.${SERVICE_NAME};} }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} class MyClass1 { } class MyClass2 { fooFunc() {this.${SERVICE_NAME};} }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT} class MyClass1 { fooFunc() {this.${SERVICE_NAME};} } class MyClass2 { @service() ${SERVICE_NAME}; }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} class MyClass1 { fooFunc() {this.${SERVICE_NAME};} } class MyClass2 { }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
/* Nested classes */
{
code: `${SERVICE_IMPORT} class MyClass1 { @service() ${SERVICE_NAME}; fooFunc1() { class MyClass2 { fooFunc2() {this.${SERVICE_NAME};} } } }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} class MyClass1 { fooFunc1() { class MyClass2 { fooFunc2() {this.${SERVICE_NAME};} } } }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
code: `${SERVICE_IMPORT} class MyClass1 { fooFunc1() {this.${SERVICE_NAME};} fooFunc2() { class MyClass2 { @service() ${SERVICE_NAME}; } } }`,
output: null,
errors: [
{
messageId: 'main',
suggestions: [
{
messageId: 'removeServiceInjection',
output: `${SERVICE_IMPORT} class MyClass1 { fooFunc1() {this.${SERVICE_NAME};} fooFunc2() { class MyClass2 { } } }`,
},
],
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
],
});