@futdevpro/dynamo-eslint 1.14.24 → 1.14.26
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/.github/workflows/main.yml +6 -1
- package/build/configs/base.js +1 -0
- package/build/configs/base.js.map +1 -1
- package/build/plugin/index.d.ts +2 -0
- package/build/plugin/index.d.ts.map +1 -1
- package/build/plugin/index.js +3 -0
- package/build/plugin/index.js.map +1 -1
- package/build/plugin/rules/no-getter-logic.d.ts +4 -0
- package/build/plugin/rules/no-getter-logic.d.ts.map +1 -0
- package/build/plugin/rules/no-getter-logic.js +176 -0
- package/build/plugin/rules/no-getter-logic.js.map +1 -0
- package/build/plugin/rules/no-getter-logic.spec.d.ts +2 -0
- package/build/plugin/rules/no-getter-logic.spec.d.ts.map +1 -0
- package/build/plugin/rules/no-getter-logic.spec.js +461 -0
- package/build/plugin/rules/no-getter-logic.spec.js.map +1 -0
- package/build/plugin/rules/require-jsdoc-description.d.ts.map +1 -1
- package/build/plugin/rules/require-jsdoc-description.js +34 -3
- package/build/plugin/rules/require-jsdoc-description.js.map +1 -1
- package/futdevpro-dynamo-eslint-1.14.26.tgz +0 -0
- package/package.json +2 -2
- package/src/configs/base.ts +1 -0
- package/src/plugin/index.ts +3 -0
- package/src/plugin/rules/no-getter-logic.spec.ts +520 -0
- package/src/plugin/rules/no-getter-logic.ts +201 -0
- package/src/plugin/rules/require-jsdoc-description.ts +40 -3
- package/futdevpro-dynamo-eslint-1.14.24.tgz +0 -0
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import noGetterLogicRule from './no-getter-logic';
|
|
2
|
+
|
|
3
|
+
describe('| no-getter-logic', () => {
|
|
4
|
+
it('| should be a valid ESLint rule', () => {
|
|
5
|
+
expect(noGetterLogicRule.meta?.type).toBe('suggestion');
|
|
6
|
+
expect(noGetterLogicRule.meta?.docs?.description).toContain('Getters should only contain simple property mappings');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('| should have create function that returns visitor object', () => {
|
|
10
|
+
const mockContext = {
|
|
11
|
+
report: () => {},
|
|
12
|
+
options: [],
|
|
13
|
+
sourceCode: {
|
|
14
|
+
getText: () => '',
|
|
15
|
+
},
|
|
16
|
+
} as any;
|
|
17
|
+
|
|
18
|
+
const result = noGetterLogicRule.create(mockContext);
|
|
19
|
+
|
|
20
|
+
expect(typeof result).toBe('object');
|
|
21
|
+
expect(typeof result.MethodDefinition).toBe('function');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('| should not report for simple property access getter', () => {
|
|
25
|
+
let reportCalled = false;
|
|
26
|
+
const mockContext = {
|
|
27
|
+
report: () => {
|
|
28
|
+
reportCalled = true;
|
|
29
|
+
},
|
|
30
|
+
options: [],
|
|
31
|
+
sourceCode: {
|
|
32
|
+
getText: () => '',
|
|
33
|
+
},
|
|
34
|
+
} as any;
|
|
35
|
+
|
|
36
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
37
|
+
|
|
38
|
+
const mockNode = {
|
|
39
|
+
kind: 'get',
|
|
40
|
+
key: { name: 'selectedTokenPackage' },
|
|
41
|
+
value: {
|
|
42
|
+
body: {
|
|
43
|
+
type: 'BlockStatement',
|
|
44
|
+
body: [
|
|
45
|
+
{
|
|
46
|
+
type: 'ReturnStatement',
|
|
47
|
+
argument: {
|
|
48
|
+
type: 'MemberExpression',
|
|
49
|
+
object: {
|
|
50
|
+
type: 'MemberExpression',
|
|
51
|
+
object: { type: 'ThisExpression' },
|
|
52
|
+
property: { type: 'Identifier', name: 'tokenPackages_CS' },
|
|
53
|
+
},
|
|
54
|
+
property: {
|
|
55
|
+
type: 'MemberExpression',
|
|
56
|
+
object: { type: 'Identifier', name: 'tokenPackageByType' },
|
|
57
|
+
property: {
|
|
58
|
+
type: 'MemberExpression',
|
|
59
|
+
object: { type: 'ThisExpression' },
|
|
60
|
+
property: { type: 'Identifier', name: 'selectedPackageType' },
|
|
61
|
+
},
|
|
62
|
+
computed: true,
|
|
63
|
+
},
|
|
64
|
+
computed: true,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
} as any;
|
|
71
|
+
|
|
72
|
+
rule.MethodDefinition(mockNode);
|
|
73
|
+
|
|
74
|
+
expect(reportCalled).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('| should not report for simple length property access', () => {
|
|
78
|
+
let reportCalled = false;
|
|
79
|
+
const mockContext = {
|
|
80
|
+
report: () => {
|
|
81
|
+
reportCalled = true;
|
|
82
|
+
},
|
|
83
|
+
options: [],
|
|
84
|
+
sourceCode: {
|
|
85
|
+
getText: () => '',
|
|
86
|
+
},
|
|
87
|
+
} as any;
|
|
88
|
+
|
|
89
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
90
|
+
|
|
91
|
+
const mockNode = {
|
|
92
|
+
kind: 'get',
|
|
93
|
+
key: { name: 'runningProgressCount' },
|
|
94
|
+
value: {
|
|
95
|
+
body: {
|
|
96
|
+
type: 'BlockStatement',
|
|
97
|
+
body: [
|
|
98
|
+
{
|
|
99
|
+
type: 'ReturnStatement',
|
|
100
|
+
argument: {
|
|
101
|
+
type: 'MemberExpression',
|
|
102
|
+
object: {
|
|
103
|
+
type: 'MemberExpression',
|
|
104
|
+
object: { type: 'ThisExpression' },
|
|
105
|
+
property: { type: 'Identifier', name: '_runningProgresses' },
|
|
106
|
+
},
|
|
107
|
+
property: { type: 'Identifier', name: 'length' },
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
} as any;
|
|
114
|
+
|
|
115
|
+
rule.MethodDefinition(mockNode);
|
|
116
|
+
|
|
117
|
+
expect(reportCalled).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('| should not report for simple signal property access', () => {
|
|
121
|
+
let reportCalled = false;
|
|
122
|
+
const mockContext = {
|
|
123
|
+
report: () => {
|
|
124
|
+
reportCalled = true;
|
|
125
|
+
},
|
|
126
|
+
options: [],
|
|
127
|
+
sourceCode: {
|
|
128
|
+
getText: () => '',
|
|
129
|
+
},
|
|
130
|
+
} as any;
|
|
131
|
+
|
|
132
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
133
|
+
|
|
134
|
+
const mockNode = {
|
|
135
|
+
kind: 'get',
|
|
136
|
+
key: { name: 'myChannels_$' },
|
|
137
|
+
value: {
|
|
138
|
+
body: {
|
|
139
|
+
type: 'BlockStatement',
|
|
140
|
+
body: [
|
|
141
|
+
{
|
|
142
|
+
type: 'ReturnStatement',
|
|
143
|
+
argument: {
|
|
144
|
+
type: 'MemberExpression',
|
|
145
|
+
object: {
|
|
146
|
+
type: 'MemberExpression',
|
|
147
|
+
object: { type: 'ThisExpression' },
|
|
148
|
+
property: { type: 'Identifier', name: 'myChannels_DH' },
|
|
149
|
+
},
|
|
150
|
+
property: { type: 'Identifier', name: 'dataList_$' },
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
} as any;
|
|
157
|
+
|
|
158
|
+
rule.MethodDefinition(mockNode);
|
|
159
|
+
|
|
160
|
+
expect(reportCalled).toBe(false);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('| should report for binary expression (multiplication)', () => {
|
|
164
|
+
let reportCalled = false;
|
|
165
|
+
const mockContext = {
|
|
166
|
+
report: (options: any) => {
|
|
167
|
+
reportCalled = true;
|
|
168
|
+
expect(options.messageId).toBe('noLogic');
|
|
169
|
+
expect(options.data.name).toBe('shownHUNVat');
|
|
170
|
+
},
|
|
171
|
+
options: [],
|
|
172
|
+
sourceCode: {
|
|
173
|
+
getText: () => '',
|
|
174
|
+
},
|
|
175
|
+
} as any;
|
|
176
|
+
|
|
177
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
178
|
+
|
|
179
|
+
const mockNode = {
|
|
180
|
+
kind: 'get',
|
|
181
|
+
key: { name: 'shownHUNVat' },
|
|
182
|
+
value: {
|
|
183
|
+
body: {
|
|
184
|
+
type: 'BlockStatement',
|
|
185
|
+
body: [
|
|
186
|
+
{
|
|
187
|
+
type: 'ReturnStatement',
|
|
188
|
+
argument: {
|
|
189
|
+
type: 'BinaryExpression',
|
|
190
|
+
operator: '*',
|
|
191
|
+
left: { type: 'Identifier', name: 'HU_vat' },
|
|
192
|
+
right: { type: 'Literal', value: 100 },
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
],
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
} as any;
|
|
199
|
+
|
|
200
|
+
rule.MethodDefinition(mockNode);
|
|
201
|
+
|
|
202
|
+
expect(reportCalled).toBe(true);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('| should report for function call', () => {
|
|
206
|
+
let reportCalled = false;
|
|
207
|
+
const mockContext = {
|
|
208
|
+
report: (options: any) => {
|
|
209
|
+
reportCalled = true;
|
|
210
|
+
expect(options.messageId).toBe('noLogic');
|
|
211
|
+
expect(options.data.name).toBe('selectedPackageType');
|
|
212
|
+
},
|
|
213
|
+
options: [],
|
|
214
|
+
sourceCode: {
|
|
215
|
+
getText: () => '',
|
|
216
|
+
},
|
|
217
|
+
} as any;
|
|
218
|
+
|
|
219
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
220
|
+
|
|
221
|
+
const mockNode = {
|
|
222
|
+
kind: 'get',
|
|
223
|
+
key: { name: 'selectedPackageType' },
|
|
224
|
+
value: {
|
|
225
|
+
body: {
|
|
226
|
+
type: 'BlockStatement',
|
|
227
|
+
body: [
|
|
228
|
+
{
|
|
229
|
+
type: 'ReturnStatement',
|
|
230
|
+
argument: {
|
|
231
|
+
type: 'MemberExpression',
|
|
232
|
+
object: {
|
|
233
|
+
type: 'CallExpression',
|
|
234
|
+
callee: {
|
|
235
|
+
type: 'MemberExpression',
|
|
236
|
+
object: { type: 'ThisExpression' },
|
|
237
|
+
property: { type: 'Identifier', name: 'completedPurchaseData$' },
|
|
238
|
+
},
|
|
239
|
+
arguments: [],
|
|
240
|
+
},
|
|
241
|
+
property: { type: 'Identifier', name: 'selectedPackageType' },
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
} as any;
|
|
248
|
+
|
|
249
|
+
rule.MethodDefinition(mockNode);
|
|
250
|
+
|
|
251
|
+
expect(reportCalled).toBe(true);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('| should report for unary expression', () => {
|
|
255
|
+
let reportCalled = false;
|
|
256
|
+
const mockContext = {
|
|
257
|
+
report: (options: any) => {
|
|
258
|
+
reportCalled = true;
|
|
259
|
+
expect(options.messageId).toBe('noLogic');
|
|
260
|
+
},
|
|
261
|
+
options: [],
|
|
262
|
+
sourceCode: {
|
|
263
|
+
getText: () => '',
|
|
264
|
+
},
|
|
265
|
+
} as any;
|
|
266
|
+
|
|
267
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
268
|
+
|
|
269
|
+
const mockNode = {
|
|
270
|
+
kind: 'get',
|
|
271
|
+
key: { name: 'negatedValue' },
|
|
272
|
+
value: {
|
|
273
|
+
body: {
|
|
274
|
+
type: 'BlockStatement',
|
|
275
|
+
body: [
|
|
276
|
+
{
|
|
277
|
+
type: 'ReturnStatement',
|
|
278
|
+
argument: {
|
|
279
|
+
type: 'UnaryExpression',
|
|
280
|
+
operator: '!',
|
|
281
|
+
argument: { type: 'Identifier', name: 'someValue' },
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
],
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
} as any;
|
|
288
|
+
|
|
289
|
+
rule.MethodDefinition(mockNode);
|
|
290
|
+
|
|
291
|
+
expect(reportCalled).toBe(true);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('| should report for conditional expression (ternary)', () => {
|
|
295
|
+
let reportCalled = false;
|
|
296
|
+
const mockContext = {
|
|
297
|
+
report: (options: any) => {
|
|
298
|
+
reportCalled = true;
|
|
299
|
+
expect(options.messageId).toBe('noLogic');
|
|
300
|
+
},
|
|
301
|
+
options: [],
|
|
302
|
+
sourceCode: {
|
|
303
|
+
getText: () => '',
|
|
304
|
+
},
|
|
305
|
+
} as any;
|
|
306
|
+
|
|
307
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
308
|
+
|
|
309
|
+
const mockNode = {
|
|
310
|
+
kind: 'get',
|
|
311
|
+
key: { name: 'conditionalValue' },
|
|
312
|
+
value: {
|
|
313
|
+
body: {
|
|
314
|
+
type: 'BlockStatement',
|
|
315
|
+
body: [
|
|
316
|
+
{
|
|
317
|
+
type: 'ReturnStatement',
|
|
318
|
+
argument: {
|
|
319
|
+
type: 'ConditionalExpression',
|
|
320
|
+
test: { type: 'Identifier', name: 'condition' },
|
|
321
|
+
consequent: { type: 'Literal', value: 'true' },
|
|
322
|
+
alternate: { type: 'Literal', value: 'false' },
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
],
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
} as any;
|
|
329
|
+
|
|
330
|
+
rule.MethodDefinition(mockNode);
|
|
331
|
+
|
|
332
|
+
expect(reportCalled).toBe(true);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('| should report for logical expression', () => {
|
|
336
|
+
let reportCalled = false;
|
|
337
|
+
const mockContext = {
|
|
338
|
+
report: (options: any) => {
|
|
339
|
+
reportCalled = true;
|
|
340
|
+
expect(options.messageId).toBe('noLogic');
|
|
341
|
+
},
|
|
342
|
+
options: [],
|
|
343
|
+
sourceCode: {
|
|
344
|
+
getText: () => '',
|
|
345
|
+
},
|
|
346
|
+
} as any;
|
|
347
|
+
|
|
348
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
349
|
+
|
|
350
|
+
const mockNode = {
|
|
351
|
+
kind: 'get',
|
|
352
|
+
key: { name: 'logicalValue' },
|
|
353
|
+
value: {
|
|
354
|
+
body: {
|
|
355
|
+
type: 'BlockStatement',
|
|
356
|
+
body: [
|
|
357
|
+
{
|
|
358
|
+
type: 'ReturnStatement',
|
|
359
|
+
argument: {
|
|
360
|
+
type: 'LogicalExpression',
|
|
361
|
+
operator: '&&',
|
|
362
|
+
left: { type: 'Identifier', name: 'value1' },
|
|
363
|
+
right: { type: 'Identifier', name: 'value2' },
|
|
364
|
+
},
|
|
365
|
+
},
|
|
366
|
+
],
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
} as any;
|
|
370
|
+
|
|
371
|
+
rule.MethodDefinition(mockNode);
|
|
372
|
+
|
|
373
|
+
expect(reportCalled).toBe(true);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it('| should not report for non-getter methods', () => {
|
|
377
|
+
let reportCalled = false;
|
|
378
|
+
const mockContext = {
|
|
379
|
+
report: () => {
|
|
380
|
+
reportCalled = true;
|
|
381
|
+
},
|
|
382
|
+
options: [],
|
|
383
|
+
sourceCode: {
|
|
384
|
+
getText: () => '',
|
|
385
|
+
},
|
|
386
|
+
} as any;
|
|
387
|
+
|
|
388
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
389
|
+
|
|
390
|
+
const mockNode = {
|
|
391
|
+
kind: 'method',
|
|
392
|
+
key: { name: 'someMethod' },
|
|
393
|
+
value: {
|
|
394
|
+
body: {
|
|
395
|
+
type: 'BlockStatement',
|
|
396
|
+
body: [
|
|
397
|
+
{
|
|
398
|
+
type: 'ReturnStatement',
|
|
399
|
+
argument: {
|
|
400
|
+
type: 'CallExpression',
|
|
401
|
+
callee: { type: 'Identifier', name: 'someFunction' },
|
|
402
|
+
arguments: [],
|
|
403
|
+
},
|
|
404
|
+
},
|
|
405
|
+
],
|
|
406
|
+
},
|
|
407
|
+
},
|
|
408
|
+
} as any;
|
|
409
|
+
|
|
410
|
+
rule.MethodDefinition(mockNode);
|
|
411
|
+
|
|
412
|
+
expect(reportCalled).toBe(false);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it('| should not report for setter methods', () => {
|
|
416
|
+
let reportCalled = false;
|
|
417
|
+
const mockContext = {
|
|
418
|
+
report: () => {
|
|
419
|
+
reportCalled = true;
|
|
420
|
+
},
|
|
421
|
+
options: [],
|
|
422
|
+
sourceCode: {
|
|
423
|
+
getText: () => '',
|
|
424
|
+
},
|
|
425
|
+
} as any;
|
|
426
|
+
|
|
427
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
428
|
+
|
|
429
|
+
const mockNode = {
|
|
430
|
+
kind: 'set',
|
|
431
|
+
key: { name: 'someSetter' },
|
|
432
|
+
value: {
|
|
433
|
+
body: {
|
|
434
|
+
type: 'BlockStatement',
|
|
435
|
+
body: [
|
|
436
|
+
{
|
|
437
|
+
type: 'ExpressionStatement',
|
|
438
|
+
expression: {
|
|
439
|
+
type: 'AssignmentExpression',
|
|
440
|
+
operator: '=',
|
|
441
|
+
left: { type: 'Identifier', name: 'this.value' },
|
|
442
|
+
right: { type: 'Identifier', name: 'value' },
|
|
443
|
+
},
|
|
444
|
+
},
|
|
445
|
+
],
|
|
446
|
+
},
|
|
447
|
+
},
|
|
448
|
+
} as any;
|
|
449
|
+
|
|
450
|
+
rule.MethodDefinition(mockNode);
|
|
451
|
+
|
|
452
|
+
expect(reportCalled).toBe(false);
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it('| should handle getter without body gracefully', () => {
|
|
456
|
+
let reportCalled = false;
|
|
457
|
+
const mockContext = {
|
|
458
|
+
report: () => {
|
|
459
|
+
reportCalled = true;
|
|
460
|
+
},
|
|
461
|
+
options: [],
|
|
462
|
+
sourceCode: {
|
|
463
|
+
getText: () => '',
|
|
464
|
+
},
|
|
465
|
+
} as any;
|
|
466
|
+
|
|
467
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
468
|
+
|
|
469
|
+
const mockNode = {
|
|
470
|
+
kind: 'get',
|
|
471
|
+
key: { name: 'emptyGetter' },
|
|
472
|
+
value: {},
|
|
473
|
+
} as any;
|
|
474
|
+
|
|
475
|
+
expect(() => {
|
|
476
|
+
rule.MethodDefinition(mockNode);
|
|
477
|
+
}).not.toThrow();
|
|
478
|
+
|
|
479
|
+
expect(reportCalled).toBe(false);
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it('| should handle errors gracefully', () => {
|
|
483
|
+
const mockContext = {
|
|
484
|
+
report: () => {},
|
|
485
|
+
options: [],
|
|
486
|
+
sourceCode: {
|
|
487
|
+
getText: () => {
|
|
488
|
+
throw new Error('Test error');
|
|
489
|
+
},
|
|
490
|
+
},
|
|
491
|
+
} as any;
|
|
492
|
+
|
|
493
|
+
const rule = noGetterLogicRule.create(mockContext);
|
|
494
|
+
|
|
495
|
+
const mockNode = {
|
|
496
|
+
kind: 'get',
|
|
497
|
+
key: { name: 'testGetter' },
|
|
498
|
+
value: {
|
|
499
|
+
body: {
|
|
500
|
+
type: 'BlockStatement',
|
|
501
|
+
body: [
|
|
502
|
+
{
|
|
503
|
+
type: 'ReturnStatement',
|
|
504
|
+
argument: {
|
|
505
|
+
type: 'CallExpression',
|
|
506
|
+
callee: { type: 'Identifier', name: 'test' },
|
|
507
|
+
arguments: [],
|
|
508
|
+
},
|
|
509
|
+
},
|
|
510
|
+
],
|
|
511
|
+
},
|
|
512
|
+
},
|
|
513
|
+
} as any;
|
|
514
|
+
|
|
515
|
+
// Should not throw error
|
|
516
|
+
expect(() => {
|
|
517
|
+
rule.MethodDefinition(mockNode);
|
|
518
|
+
}).not.toThrow();
|
|
519
|
+
});
|
|
520
|
+
});
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { Rule } from 'eslint';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if a node type is allowed in getter (simple property mapping only)
|
|
5
|
+
*/
|
|
6
|
+
function isAllowedNodeType(nodeType: string): boolean {
|
|
7
|
+
const allowedTypes = [
|
|
8
|
+
'ReturnStatement',
|
|
9
|
+
'MemberExpression',
|
|
10
|
+
'Identifier',
|
|
11
|
+
'ThisExpression',
|
|
12
|
+
'Literal',
|
|
13
|
+
'BlockStatement',
|
|
14
|
+
'ExpressionStatement',
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
return allowedTypes.includes(nodeType);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Check if a node type is explicitly forbidden (logic, calculations, function calls)
|
|
22
|
+
*/
|
|
23
|
+
function isForbiddenNodeType(nodeType: string): boolean {
|
|
24
|
+
const forbiddenTypes = [
|
|
25
|
+
'CallExpression',
|
|
26
|
+
'BinaryExpression',
|
|
27
|
+
'UnaryExpression',
|
|
28
|
+
'ConditionalExpression',
|
|
29
|
+
'LogicalExpression',
|
|
30
|
+
'AssignmentExpression',
|
|
31
|
+
'UpdateExpression',
|
|
32
|
+
'NewExpression',
|
|
33
|
+
'YieldExpression',
|
|
34
|
+
'AwaitExpression',
|
|
35
|
+
'TaggedTemplateExpression',
|
|
36
|
+
'TemplateLiteral',
|
|
37
|
+
'SequenceExpression',
|
|
38
|
+
'SpreadElement',
|
|
39
|
+
'RestElement',
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
return forbiddenTypes.includes(nodeType);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Recursively check if a node contains only allowed patterns
|
|
47
|
+
* Returns true if forbidden node is found
|
|
48
|
+
*/
|
|
49
|
+
function containsForbiddenNode(node: any): boolean {
|
|
50
|
+
if (!node || typeof node !== 'object') {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Check current node type
|
|
55
|
+
if (node.type && isForbiddenNodeType(node.type)) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Special handling for MemberExpression - allow property access but check nested calls
|
|
60
|
+
if (node.type === 'MemberExpression') {
|
|
61
|
+
// Check if this is a function call disguised as property access
|
|
62
|
+
// This will be caught by parent CallExpression, so we just check children
|
|
63
|
+
if (node.object && containsForbiddenNode(node.object)) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (node.property && containsForbiddenNode(node.property)) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Allow computed property access like [index] but check the index expression
|
|
72
|
+
if (node.computed && node.property && containsForbiddenNode(node.property)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Special handling for ReturnStatement - check the argument
|
|
80
|
+
if (node.type === 'ReturnStatement') {
|
|
81
|
+
if (node.argument && containsForbiddenNode(node.argument)) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Special handling for BlockStatement - check all statements
|
|
89
|
+
if (node.type === 'BlockStatement') {
|
|
90
|
+
if (node.body && Array.isArray(node.body)) {
|
|
91
|
+
for (const statement of node.body) {
|
|
92
|
+
if (containsForbiddenNode(statement)) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Special handling for ExpressionStatement - check the expression
|
|
102
|
+
if (node.type === 'ExpressionStatement') {
|
|
103
|
+
if (node.expression && containsForbiddenNode(node.expression)) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// For other node types, recursively check all properties
|
|
111
|
+
for (const key in node) {
|
|
112
|
+
if (key === 'parent' || key === 'range' || key === 'loc') {
|
|
113
|
+
continue; // Skip parent references and location data
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const value = node[key];
|
|
117
|
+
|
|
118
|
+
if (Array.isArray(value)) {
|
|
119
|
+
for (const item of value) {
|
|
120
|
+
if (containsForbiddenNode(item)) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
} else if (value && typeof value === 'object') {
|
|
125
|
+
if (containsForbiddenNode(value)) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get getter name for error message
|
|
136
|
+
*/
|
|
137
|
+
function getGetterName(node: any): string {
|
|
138
|
+
try {
|
|
139
|
+
if (node.key && node.key.name) {
|
|
140
|
+
return node.key.name;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (node.key && node.key.type === 'Identifier') {
|
|
144
|
+
return node.key.name || 'unknown';
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return 'unknown';
|
|
148
|
+
} catch (error) {
|
|
149
|
+
console.error('[no-getter-logic] Error in getGetterName:', error);
|
|
150
|
+
|
|
151
|
+
return 'unknown';
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const rule: Rule.RuleModule = {
|
|
156
|
+
meta: {
|
|
157
|
+
type: 'suggestion',
|
|
158
|
+
docs: {
|
|
159
|
+
description: 'Getters should only contain simple property mappings, no logic, calculations, or function calls',
|
|
160
|
+
recommended: true,
|
|
161
|
+
},
|
|
162
|
+
schema: [],
|
|
163
|
+
messages: {
|
|
164
|
+
noLogic: 'Getter "{{name}}" should only contain simple property mappings, no logic, calculations, or function calls.',
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
create(context) {
|
|
168
|
+
return {
|
|
169
|
+
MethodDefinition(node: any, _context?: any, _sourceCode?: any): void {
|
|
170
|
+
try {
|
|
171
|
+
// Only check getters
|
|
172
|
+
if (node.kind !== 'get') {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Check if getter has a body
|
|
177
|
+
if (!node.value || !node.value.body) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const body = node.value.body;
|
|
182
|
+
|
|
183
|
+
// Check for forbidden nodes in the getter body
|
|
184
|
+
if (containsForbiddenNode(body)) {
|
|
185
|
+
const getterName = getGetterName(node);
|
|
186
|
+
|
|
187
|
+
context.report({
|
|
188
|
+
node: body,
|
|
189
|
+
messageId: 'noLogic',
|
|
190
|
+
data: { name: getterName },
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
} catch (error) {
|
|
194
|
+
console.error('[no-getter-logic] Error in MethodDefinition visitor:', error);
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
export default rule;
|