@pobammer-ts/eslint-cease-nonsense-rules 1.2.2 → 1.2.4

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.
Files changed (37) hide show
  1. package/dist/build-metadata.json +5 -0
  2. package/dist/index.js +11637 -80
  3. package/dist/index.js.map +515 -1
  4. package/dist/rules/require-paired-calls.d.ts +1 -1
  5. package/package.json +10 -10
  6. package/dist/configure-utilities.js +0 -136
  7. package/dist/configure-utilities.js.map +0 -1
  8. package/dist/rules/ban-instances.js +0 -123
  9. package/dist/rules/ban-instances.js.map +0 -1
  10. package/dist/rules/ban-react-fc.js +0 -42
  11. package/dist/rules/ban-react-fc.js.map +0 -1
  12. package/dist/rules/enforce-ianitor-check-type.js +0 -340
  13. package/dist/rules/enforce-ianitor-check-type.js.map +0 -1
  14. package/dist/rules/no-color3-constructor.js +0 -76
  15. package/dist/rules/no-color3-constructor.js.map +0 -1
  16. package/dist/rules/no-instance-methods-without-this.js +0 -122
  17. package/dist/rules/no-instance-methods-without-this.js.map +0 -1
  18. package/dist/rules/no-print.js +0 -25
  19. package/dist/rules/no-print.js.map +0 -1
  20. package/dist/rules/no-shorthand-names.js +0 -116
  21. package/dist/rules/no-shorthand-names.js.map +0 -1
  22. package/dist/rules/no-warn.js +0 -25
  23. package/dist/rules/no-warn.js.map +0 -1
  24. package/dist/rules/prefer-sequence-overloads.js +0 -109
  25. package/dist/rules/prefer-sequence-overloads.js.map +0 -1
  26. package/dist/rules/prefer-udim2-shorthand.js +0 -161
  27. package/dist/rules/prefer-udim2-shorthand.js.map +0 -1
  28. package/dist/rules/require-named-effect-functions.js +0 -350
  29. package/dist/rules/require-named-effect-functions.js.map +0 -1
  30. package/dist/rules/require-paired-calls.js +0 -848
  31. package/dist/rules/require-paired-calls.js.map +0 -1
  32. package/dist/rules/require-react-component-keys.js +0 -357
  33. package/dist/rules/require-react-component-keys.js.map +0 -1
  34. package/dist/rules/use-exhaustive-dependencies.js +0 -697
  35. package/dist/rules/use-exhaustive-dependencies.js.map +0 -1
  36. package/dist/rules/use-hook-at-top-level.js +0 -351
  37. package/dist/rules/use-hook-at-top-level.js.map +0 -1
@@ -1,848 +0,0 @@
1
- import { AST_NODE_TYPES } from "@typescript-eslint/types";
2
- import Type from "typebox";
3
- import { Compile } from "typebox/compile";
4
- const isStringArray = Compile(Type.Readonly(Type.Array(Type.String())));
5
- const isPairConfiguration = Compile(Type.Readonly(Type.Object({
6
- alternatives: Type.Optional(isStringArray),
7
- closer: Type.Union([Type.String(), isStringArray]),
8
- opener: Type.String(),
9
- openerAlternatives: Type.Optional(isStringArray),
10
- platform: Type.Optional(Type.Literal("roblox")),
11
- requireSync: Type.Optional(Type.Boolean()),
12
- yieldingFunctions: Type.Optional(isStringArray),
13
- })));
14
- const isRuleOptions = Compile(Type.Partial(Type.Readonly(Type.Object({
15
- allowConditionalClosers: Type.Optional(Type.Boolean()),
16
- allowMultipleOpeners: Type.Optional(Type.Boolean()),
17
- maxNestingDepth: Type.Optional(Type.Number()),
18
- pairs: Type.Readonly(Type.Array(isPairConfiguration)),
19
- }))));
20
- const LOOP_NODE_TYPES = new Set([
21
- AST_NODE_TYPES.DoWhileStatement,
22
- AST_NODE_TYPES.ForInStatement,
23
- AST_NODE_TYPES.ForOfStatement,
24
- AST_NODE_TYPES.ForStatement,
25
- AST_NODE_TYPES.WhileStatement,
26
- ]);
27
- export const DEFAULT_ROBLOX_YIELDING_FUNCTIONS = ["task.wait", "wait", "*.WaitForChild"];
28
- function getCallName(node) {
29
- const { callee } = node;
30
- if (callee.type === AST_NODE_TYPES.Identifier)
31
- return callee.name;
32
- if (callee.type === AST_NODE_TYPES.MemberExpression) {
33
- const object = callee.object.type === AST_NODE_TYPES.Identifier ? callee.object.name : undefined;
34
- const property = callee.property.type === AST_NODE_TYPES.Identifier ? callee.property.name : undefined;
35
- if (object !== undefined && property !== undefined)
36
- return `${object}.${property}`;
37
- }
38
- return undefined;
39
- }
40
- function getValidClosers(configuration) {
41
- const result = new Array();
42
- if (isStringArray.Check(configuration.closer))
43
- result.push(...configuration.closer);
44
- else if (typeof configuration.closer === "string")
45
- result.push(configuration.closer);
46
- if (configuration.alternatives)
47
- for (const alternative of configuration.alternatives)
48
- result.push(alternative);
49
- return result;
50
- }
51
- function getAllOpeners(configuration) {
52
- const openers = [configuration.opener];
53
- if (configuration.openerAlternatives)
54
- openers.push(...configuration.openerAlternatives);
55
- return openers;
56
- }
57
- function formatOpenerList(openers) {
58
- if (openers.length === 0)
59
- return "configured opener";
60
- if (openers.length === 1)
61
- return openers[0] ?? "configured opener";
62
- return openers.join("' or '");
63
- }
64
- function isLoopLikeStatement(node) {
65
- if (!node)
66
- return false;
67
- return LOOP_NODE_TYPES.has(node.type);
68
- }
69
- function isSwitchStatement(node) {
70
- return node?.type === AST_NODE_TYPES.SwitchStatement;
71
- }
72
- function findLabeledStatementBody(label, startingNode) {
73
- let current = startingNode;
74
- while (current) {
75
- if (current.type === AST_NODE_TYPES.LabeledStatement && current.label.name === label.name) {
76
- return current.body;
77
- }
78
- current = current.parent ?? undefined;
79
- }
80
- return undefined;
81
- }
82
- function resolveBreakTargetLoop(statement) {
83
- const labeledBody = statement.label
84
- ? findLabeledStatementBody(statement.label, statement.parent ?? undefined)
85
- : undefined;
86
- if (labeledBody) {
87
- return isLoopLikeStatement(labeledBody) ? labeledBody : undefined;
88
- }
89
- let current = statement.parent ?? undefined;
90
- while (current) {
91
- if (isLoopLikeStatement(current))
92
- return current;
93
- if (isSwitchStatement(current))
94
- return undefined;
95
- current = current.parent ?? undefined;
96
- }
97
- return undefined;
98
- }
99
- function resolveContinueTargetLoop(statement) {
100
- const labeledBody = statement.label
101
- ? findLabeledStatementBody(statement.label, statement.parent ?? undefined)
102
- : undefined;
103
- if (labeledBody) {
104
- return isLoopLikeStatement(labeledBody) ? labeledBody : undefined;
105
- }
106
- let current = statement.parent ?? undefined;
107
- while (current) {
108
- if (isLoopLikeStatement(current))
109
- return current;
110
- current = current.parent ?? undefined;
111
- }
112
- return undefined;
113
- }
114
- function cloneEntry(value) {
115
- return { ...value, loopAncestors: [...value.loopAncestors] };
116
- }
117
- const rule = {
118
- create(context) {
119
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- ESLint context.options is typed as any[]
120
- const rawOptions = context.options[0];
121
- const baseOptions = isRuleOptions.Check(rawOptions) ? rawOptions : {};
122
- const options = {
123
- allowConditionalClosers: baseOptions.allowConditionalClosers ?? false,
124
- allowMultipleOpeners: baseOptions.allowMultipleOpeners ?? true,
125
- maxNestingDepth: baseOptions.maxNestingDepth ?? 0,
126
- pairs: baseOptions.pairs ?? [],
127
- };
128
- if (options.pairs.length === 0) {
129
- options.pairs = [
130
- {
131
- closer: "debug.profileend",
132
- opener: "debug.profilebegin",
133
- platform: "roblox",
134
- requireSync: true,
135
- yieldingFunctions: [...DEFAULT_ROBLOX_YIELDING_FUNCTIONS],
136
- },
137
- ];
138
- }
139
- const openerStack = new Array();
140
- const loopStack = new Array();
141
- let stackIndexCounter = 0;
142
- const functionStacks = new Array();
143
- let yieldingAutoClosed = false;
144
- let yieldingReportedFirst = false;
145
- const contextStack = new Array();
146
- const stackSnapshots = new Map();
147
- const branchStacks = new Map();
148
- const closerToOpenersCache = new Map();
149
- const openerToClosersCache = new Map();
150
- function getConfiguredOpenersForCloser(closer) {
151
- if (closerToOpenersCache.has(closer))
152
- return closerToOpenersCache.get(closer) ?? [];
153
- const names = new Array();
154
- for (const pair of options.pairs) {
155
- if (!getValidClosers(pair).includes(closer))
156
- continue;
157
- for (const openerName of getAllOpeners(pair)) {
158
- if (!names.includes(openerName))
159
- names.push(openerName);
160
- }
161
- }
162
- closerToOpenersCache.set(closer, names);
163
- return names;
164
- }
165
- function getExpectedClosersForOpener(opener) {
166
- if (openerToClosersCache.has(opener))
167
- return openerToClosersCache.get(opener) ?? [];
168
- const closers = new Array();
169
- for (const pair of options.pairs) {
170
- const allOpeners = getAllOpeners(pair);
171
- if (!allOpeners.includes(opener))
172
- continue;
173
- const validClosers = getValidClosers(pair);
174
- for (const closer of validClosers) {
175
- if (!closers.includes(closer))
176
- closers.push(closer);
177
- }
178
- }
179
- openerToClosersCache.set(opener, closers);
180
- return closers;
181
- }
182
- function getCurrentContext() {
183
- return contextStack.length > 0
184
- ? // oxlint-disable-next-line no-non-null-assertion -- this is fine! we checked already!
185
- contextStack.at(-1)
186
- : {
187
- asyncContext: false,
188
- currentFunction: undefined,
189
- hasEarlyExit: false,
190
- inCatch: false,
191
- inConditional: false,
192
- inFinally: false,
193
- inLoop: false,
194
- inTry: false,
195
- };
196
- }
197
- function pushContext(newContext) {
198
- const currentContext = getCurrentContext();
199
- contextStack.push({ ...currentContext, ...newContext });
200
- }
201
- function popContext() {
202
- contextStack.pop();
203
- }
204
- function updateContext(updates) {
205
- const last = contextStack.at(-1);
206
- if (!last)
207
- return;
208
- contextStack[contextStack.length - 1] = { ...last, ...updates };
209
- }
210
- function cloneStack() {
211
- // oxlint-disable-next-line no-array-callback-reference -- this is fine. leave it alone.
212
- return openerStack.map(cloneEntry);
213
- }
214
- function saveSnapshot(node) {
215
- stackSnapshots.set(node, cloneStack());
216
- }
217
- function findPairConfig(functionName, isOpener) {
218
- return options.pairs.find((pair) => {
219
- if (isOpener)
220
- return getAllOpeners(pair).includes(functionName);
221
- // Check if it matches closer or alternatives
222
- const validClosers = getValidClosers(pair);
223
- return validClosers.includes(functionName);
224
- });
225
- }
226
- function isRobloxYieldingFunction(functionName, configuration) {
227
- if (configuration.platform !== "roblox")
228
- return false;
229
- const yieldingFunctions = configuration.yieldingFunctions ?? DEFAULT_ROBLOX_YIELDING_FUNCTIONS;
230
- return yieldingFunctions.some((pattern) => {
231
- if (pattern.startsWith("*.")) {
232
- // Match any method call with this name
233
- const methodName = pattern.slice(2);
234
- return functionName.endsWith(`.${methodName}`);
235
- }
236
- return functionName === pattern;
237
- });
238
- }
239
- function onFunctionEnter(node) {
240
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor functions receive unknown, we know the type from selector
241
- const functionNode = node;
242
- functionStacks.push([...openerStack]);
243
- openerStack.length = 0;
244
- yieldingAutoClosed = false;
245
- yieldingReportedFirst = false;
246
- pushContext({
247
- asyncContext: functionNode.async ?? false,
248
- currentFunction: functionNode,
249
- hasEarlyExit: false,
250
- inCatch: false,
251
- inConditional: false,
252
- inFinally: false,
253
- inLoop: false,
254
- inTry: false,
255
- });
256
- }
257
- function onFunctionExit() {
258
- if (openerStack.length > 0) {
259
- for (const entry of openerStack) {
260
- const validClosers = getValidClosers(entry.config);
261
- const closer = validClosers.length === 1 ? (validClosers[0] ?? "closer") : validClosers.join("' or '");
262
- context.report({
263
- data: {
264
- closer,
265
- opener: entry.opener,
266
- paths: "function exit",
267
- },
268
- messageId: "unpairedOpener",
269
- node: entry.node,
270
- });
271
- }
272
- }
273
- const parentStack = functionStacks.pop();
274
- if (parentStack) {
275
- openerStack.length = 0;
276
- openerStack.push(...parentStack);
277
- }
278
- else
279
- openerStack.length = 0;
280
- popContext();
281
- }
282
- function onIfStatementEnter(node) {
283
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
284
- const ifNode = node;
285
- pushContext({ inConditional: true });
286
- saveSnapshot(ifNode);
287
- }
288
- function onIfStatementExit(node) {
289
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
290
- const ifNode = node;
291
- popContext();
292
- const originalStack = stackSnapshots.get(ifNode);
293
- const branches = branchStacks.get(ifNode);
294
- if (originalStack && branches && branches.length > 0) {
295
- const hasCompleteElse = ifNode.alternate !== undefined && ifNode.alternate !== null;
296
- if (hasCompleteElse) {
297
- for (const { index, config, opener, node } of originalStack) {
298
- const branchesWithOpener = branches.filter((branchStack) => branchStack.some((branch) => branch.index === index));
299
- if (branchesWithOpener.length <= 0 || branchesWithOpener.length >= branches.length)
300
- continue;
301
- if (options.allowConditionalClosers !== false)
302
- continue;
303
- const validClosers = getValidClosers(config);
304
- const closer = validClosers.length === 1 ? (validClosers[0] ?? "closer") : validClosers.join("' or '");
305
- context.report({
306
- data: {
307
- closer,
308
- opener,
309
- paths: "not all execution paths",
310
- },
311
- messageId: "unpairedOpener",
312
- node,
313
- });
314
- }
315
- const commonOpeners = originalStack.filter((opener) => branches.every((branchStack) => branchStack.some(({ index }) => index === opener.index)));
316
- openerStack.length = 0;
317
- openerStack.push(...commonOpeners);
318
- }
319
- else {
320
- openerStack.length = 0;
321
- for (const entry of originalStack)
322
- openerStack.push({ ...entry });
323
- }
324
- }
325
- stackSnapshots.delete(ifNode);
326
- branchStacks.delete(ifNode);
327
- }
328
- function onIfConsequentExit(node) {
329
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
330
- const consequentNode = node;
331
- const parent = consequentNode.parent;
332
- if (parent?.type === AST_NODE_TYPES.IfStatement) {
333
- const branches = branchStacks.get(parent) ?? [];
334
- branches.push(cloneStack());
335
- branchStacks.set(parent, branches);
336
- const originalStack = stackSnapshots.get(parent);
337
- if (!originalStack)
338
- return;
339
- openerStack.length = 0;
340
- for (const entry of originalStack)
341
- openerStack.push({ ...entry });
342
- }
343
- }
344
- function onIfAlternateExit(node) {
345
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
346
- const alternateNode = node;
347
- const parent = alternateNode.parent;
348
- if (parent?.type === AST_NODE_TYPES.IfStatement) {
349
- const branches = branchStacks.get(parent) ?? [];
350
- branches.push(cloneStack());
351
- branchStacks.set(parent, branches);
352
- }
353
- }
354
- function onTryStatementEnter(node) {
355
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
356
- const tryNode = node;
357
- saveSnapshot(tryNode);
358
- }
359
- function onTryStatementExit(node) {
360
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
361
- const tryNode = node;
362
- const originalStack = stackSnapshots.get(tryNode);
363
- const branches = branchStacks.get(tryNode);
364
- if (tryNode.finalizer) {
365
- stackSnapshots.delete(tryNode);
366
- branchStacks.delete(tryNode);
367
- return;
368
- }
369
- if (originalStack && branches && branches.length > 0) {
370
- for (const opener of originalStack) {
371
- const branchesWithOpener = branches.filter((branchStack) => branchStack.some((entry) => entry.index === opener.index));
372
- if (branchesWithOpener.length > 0 &&
373
- branchesWithOpener.length < branches.length &&
374
- options.allowConditionalClosers === false) {
375
- const validClosers = getValidClosers(opener.config);
376
- const closer = validClosers.length === 1 ? (validClosers[0] ?? "closer") : validClosers.join("' or '");
377
- context.report({
378
- data: {
379
- closer,
380
- opener: opener.opener,
381
- paths: "not all execution paths",
382
- },
383
- messageId: "unpairedOpener",
384
- node: opener.node,
385
- });
386
- }
387
- }
388
- const commonOpeners = originalStack.filter((opener) => branches.every((branchStack) => branchStack.some((entry) => entry.index === opener.index)));
389
- openerStack.length = 0;
390
- openerStack.push(...commonOpeners);
391
- }
392
- stackSnapshots.delete(tryNode);
393
- branchStacks.delete(tryNode);
394
- }
395
- function onTryBlockEnter() {
396
- pushContext({ inTry: true });
397
- }
398
- function onTryBlockExit(node) {
399
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
400
- const blockNode = node;
401
- const { parent } = blockNode;
402
- if (parent?.type === AST_NODE_TYPES.TryStatement) {
403
- const branches = branchStacks.get(parent) ?? [];
404
- branches.push(cloneStack());
405
- branchStacks.set(parent, branches);
406
- const originalStack = stackSnapshots.get(parent);
407
- if (originalStack) {
408
- openerStack.length = 0;
409
- for (const entry of originalStack)
410
- openerStack.push({ ...entry });
411
- }
412
- }
413
- popContext();
414
- }
415
- function onCatchClauseEnter() {
416
- pushContext({ inCatch: true });
417
- }
418
- function onCatchClauseExit(node) {
419
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
420
- const catchNode = node;
421
- const { parent } = catchNode;
422
- if (parent?.type === AST_NODE_TYPES.TryStatement) {
423
- const branches = branchStacks.get(parent) ?? [];
424
- branches.push(cloneStack());
425
- branchStacks.set(parent, branches);
426
- const originalStack = stackSnapshots.get(parent);
427
- if (originalStack) {
428
- openerStack.length = 0;
429
- for (const entry of originalStack)
430
- openerStack.push({ ...entry });
431
- }
432
- }
433
- popContext();
434
- }
435
- function onFinallyBlockEnter() {
436
- pushContext({ inFinally: true });
437
- }
438
- function onFinallyBlockExit() {
439
- popContext();
440
- }
441
- function onSwitchStatementEnter(node) {
442
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
443
- const switchNode = node;
444
- pushContext({ inConditional: true });
445
- saveSnapshot(switchNode);
446
- }
447
- function onSwitchStatementExit(node) {
448
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
449
- const switchNode = node;
450
- popContext();
451
- const originalStack = stackSnapshots.get(switchNode);
452
- const branches = branchStacks.get(switchNode);
453
- if (originalStack && branches && branches.length > 0) {
454
- const hasDefault = switchNode.cases.some((caseNode) => caseNode.test === null);
455
- if (hasDefault && branches.length === switchNode.cases.length) {
456
- for (const opener of originalStack) {
457
- const branchesWithOpener = branches.filter((branchStack) => branchStack.some((entry) => entry.index === opener.index));
458
- if (branchesWithOpener.length > 0 &&
459
- branchesWithOpener.length < branches.length &&
460
- options.allowConditionalClosers === false) {
461
- const validClosers = getValidClosers(opener.config);
462
- const closer = validClosers.length === 1 ? (validClosers[0] ?? "closer") : validClosers.join("' or '");
463
- context.report({
464
- data: {
465
- closer,
466
- opener: opener.opener,
467
- paths: "not all execution paths",
468
- },
469
- messageId: "unpairedOpener",
470
- node: opener.node,
471
- });
472
- }
473
- }
474
- const commonOpeners = originalStack.filter((opener) => branches.every((branchStack) => branchStack.some((entry) => entry.index === opener.index)));
475
- openerStack.length = 0;
476
- openerStack.push(...commonOpeners);
477
- }
478
- else {
479
- openerStack.length = 0;
480
- for (const entry of originalStack)
481
- openerStack.push({ ...entry });
482
- }
483
- }
484
- stackSnapshots.delete(switchNode);
485
- branchStacks.delete(switchNode);
486
- }
487
- function onSwitchCaseExit(node) {
488
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
489
- const caseNode = node;
490
- const { parent } = caseNode;
491
- if (parent?.type === AST_NODE_TYPES.SwitchStatement) {
492
- const branches = branchStacks.get(parent) ?? [];
493
- branches.push(cloneStack());
494
- branchStacks.set(parent, branches);
495
- const originalStack = stackSnapshots.get(parent);
496
- if (!originalStack)
497
- return;
498
- openerStack.length = 0;
499
- for (const entry of originalStack)
500
- openerStack.push({ ...entry });
501
- }
502
- }
503
- function onLoopEnter(node) {
504
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
505
- const loopNode = node;
506
- loopStack.push(loopNode);
507
- pushContext({ inLoop: true });
508
- }
509
- function onLoopExit() {
510
- if (loopStack.length > 0)
511
- loopStack.pop();
512
- popContext();
513
- }
514
- function onEarlyExit(node) {
515
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
516
- const statementNode = node;
517
- updateContext({ hasEarlyExit: true });
518
- const currentContext = getCurrentContext();
519
- if (currentContext.inFinally || openerStack.length === 0)
520
- return;
521
- for (const { opener, config, node } of openerStack) {
522
- const validClosers = getValidClosers(config);
523
- const closer = validClosers.length === 1 ? (validClosers[0] ?? "closer") : validClosers.join("' or '");
524
- const statementType = statementNode.type === AST_NODE_TYPES.ReturnStatement ? "return" : "throw";
525
- const lineNumber = statementNode.loc?.start.line ?? 0;
526
- context.report({
527
- data: {
528
- closer,
529
- opener,
530
- paths: `${statementType} at line ${lineNumber}`,
531
- },
532
- messageId: "unpairedOpener",
533
- node: node,
534
- });
535
- }
536
- }
537
- function onBreakContinue(node) {
538
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
539
- const statementNode = node;
540
- if (openerStack.length === 0)
541
- return;
542
- const targetLoop = statementNode.type === AST_NODE_TYPES.ContinueStatement
543
- ? resolveContinueTargetLoop(statementNode)
544
- : resolveBreakTargetLoop(statementNode);
545
- if (!targetLoop)
546
- return;
547
- for (const { node: openerNode, config, opener, loopAncestors } of openerStack) {
548
- if (!loopAncestors.some((loopNode) => loopNode === targetLoop))
549
- continue;
550
- const validClosers = getValidClosers(config);
551
- const closer = validClosers.length === 1 ? (validClosers[0] ?? "closer") : validClosers.join("' or '");
552
- const statementType = statementNode.type === AST_NODE_TYPES.BreakStatement ? "break" : "continue";
553
- const lineNumber = statementNode.loc?.start.line ?? 0;
554
- context.report({
555
- data: {
556
- closer,
557
- opener,
558
- paths: `${statementType} at line ${lineNumber}`,
559
- },
560
- messageId: "unpairedOpener",
561
- node: openerNode,
562
- });
563
- }
564
- }
565
- function onCallExpression(node) {
566
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
567
- const callNode = node;
568
- const callName = getCallName(callNode);
569
- if (callName === undefined || callName === "")
570
- return;
571
- const openerConfig = findPairConfig(callName, true);
572
- if (openerConfig) {
573
- handleOpener(callNode, callName, openerConfig);
574
- return;
575
- }
576
- if (findPairConfig(callName, false)) {
577
- handleCloser(callNode, callName);
578
- return;
579
- }
580
- for (const entry of openerStack) {
581
- if (!isRobloxYieldingFunction(callName, entry.config))
582
- continue;
583
- handleRobloxYield(callNode, callName, entry);
584
- // Roblox auto-closes ALL profiles
585
- openerStack.length = 0;
586
- yieldingAutoClosed = true;
587
- return;
588
- }
589
- }
590
- function handleOpener(node, opener, config) {
591
- const maxDepth = options.maxNestingDepth ?? 0;
592
- if (maxDepth > 0 && openerStack.length >= maxDepth) {
593
- context.report({
594
- data: { max: String(maxDepth) },
595
- messageId: "maxNestingExceeded",
596
- node,
597
- });
598
- }
599
- if (options.allowMultipleOpeners === false &&
600
- openerStack.length > 0 &&
601
- openerStack.at(-1)?.opener === opener) {
602
- context.report({
603
- data: { opener },
604
- messageId: "multipleOpeners",
605
- node,
606
- });
607
- }
608
- const entry = {
609
- config,
610
- index: stackIndexCounter++,
611
- location: node.loc,
612
- loopAncestors: [...loopStack],
613
- node,
614
- opener,
615
- };
616
- openerStack.push(entry);
617
- }
618
- function handleCloser(node, closer) {
619
- const matchingIndex = openerStack.findLastIndex((entry) => getValidClosers(entry.config).includes(closer));
620
- if (matchingIndex === -1) {
621
- if (yieldingAutoClosed && !yieldingReportedFirst) {
622
- yieldingReportedFirst = true;
623
- return;
624
- }
625
- // Contextual error messages based on stack state
626
- if (openerStack.length === 0) {
627
- // Stack is empty - no opener to close
628
- context.report({
629
- data: {
630
- closer,
631
- },
632
- messageId: "unpairedCloser",
633
- node,
634
- });
635
- }
636
- else {
637
- // Stack has openers, but this closer doesn't match any
638
- // Show what closer was expected for the top opener
639
- const topEntry = openerStack.at(-1);
640
- if (topEntry) {
641
- const expectedClosers = getExpectedClosersForOpener(topEntry.opener);
642
- const closerDescription = formatOpenerList(expectedClosers);
643
- context.report({
644
- data: {
645
- closer,
646
- expected: closerDescription,
647
- },
648
- messageId: "unexpectedCloser",
649
- node,
650
- });
651
- }
652
- else {
653
- // Fallback to old behavior if somehow no top entry
654
- const openerCandidates = getConfiguredOpenersForCloser(closer);
655
- const openerDescription = formatOpenerList(openerCandidates);
656
- context.report({
657
- data: {
658
- closer,
659
- opener: openerDescription,
660
- },
661
- messageId: "unpairedCloser",
662
- node,
663
- });
664
- }
665
- }
666
- return;
667
- }
668
- const matchingEntry = openerStack[matchingIndex];
669
- if (!matchingEntry)
670
- return;
671
- if (matchingIndex !== openerStack.length - 1) {
672
- const topEntry = openerStack.at(-1);
673
- if (topEntry) {
674
- context.report({
675
- data: {
676
- actual: topEntry.opener,
677
- closer,
678
- expected: matchingEntry.opener,
679
- },
680
- messageId: "wrongOrder",
681
- node,
682
- });
683
- }
684
- }
685
- openerStack.splice(matchingIndex, 1);
686
- }
687
- function handleRobloxYield(node, yieldingFunction, openerEntry) {
688
- const validClosers = getValidClosers(openerEntry.config);
689
- const closer = validClosers.length === 1 ? (validClosers[0] ?? "closer") : validClosers.join("' or '");
690
- context.report({
691
- data: { closer, yieldingFunction },
692
- messageId: "robloxYieldViolation",
693
- node,
694
- });
695
- }
696
- function onAsyncYield(node) {
697
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
698
- const asyncNode = node;
699
- for (const { opener, config } of openerStack) {
700
- if (config.requireSync !== true)
701
- continue;
702
- const validClosers = getValidClosers(config);
703
- const closer = validClosers.length === 1 ? (validClosers[0] ?? "closer") : validClosers.join("' or '");
704
- const asyncType = asyncNode.type === AST_NODE_TYPES.AwaitExpression ? "await" : "yield";
705
- context.report({
706
- data: { asyncType, closer, opener },
707
- messageId: "asyncViolation",
708
- node: asyncNode,
709
- });
710
- }
711
- }
712
- return {
713
- ArrowFunctionExpression: onFunctionEnter,
714
- "ArrowFunctionExpression:exit": onFunctionExit,
715
- AwaitExpression: onAsyncYield,
716
- BreakStatement: onBreakContinue,
717
- CallExpression: onCallExpression,
718
- CatchClause: onCatchClauseEnter,
719
- "CatchClause:exit": onCatchClauseExit,
720
- ContinueStatement: onBreakContinue,
721
- DoWhileStatement: onLoopEnter,
722
- "DoWhileStatement:exit": onLoopExit,
723
- ForInStatement: onLoopEnter,
724
- "ForInStatement:exit": onLoopExit,
725
- ForOfStatement: (node) => {
726
- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- ESLint visitor type from selector
727
- const forOfNode = node;
728
- if (forOfNode.await)
729
- onAsyncYield(forOfNode);
730
- onLoopEnter(forOfNode);
731
- },
732
- "ForOfStatement:exit": onLoopExit,
733
- ForStatement: onLoopEnter,
734
- "ForStatement:exit": onLoopExit,
735
- FunctionDeclaration: onFunctionEnter,
736
- "FunctionDeclaration:exit": onFunctionExit,
737
- FunctionExpression: onFunctionEnter,
738
- "FunctionExpression:exit": onFunctionExit,
739
- IfStatement: onIfStatementEnter,
740
- "IfStatement > .alternate:exit": onIfAlternateExit,
741
- "IfStatement > .consequent:exit": onIfConsequentExit,
742
- "IfStatement:exit": onIfStatementExit,
743
- ReturnStatement: onEarlyExit,
744
- "SwitchCase:exit": onSwitchCaseExit,
745
- SwitchStatement: onSwitchStatementEnter,
746
- "SwitchStatement:exit": onSwitchStatementExit,
747
- ThrowStatement: onEarlyExit,
748
- TryStatement: onTryStatementEnter,
749
- "TryStatement > .block": onTryBlockEnter,
750
- "TryStatement > .block:exit": onTryBlockExit,
751
- "TryStatement > .finalizer": onFinallyBlockEnter,
752
- "TryStatement > .finalizer:exit": onFinallyBlockExit,
753
- "TryStatement:exit": onTryStatementExit,
754
- WhileStatement: onLoopEnter,
755
- "WhileStatement:exit": onLoopExit,
756
- YieldExpression: onAsyncYield,
757
- };
758
- },
759
- meta: {
760
- docs: {
761
- description: "Enforces balanced opener/closer function calls across all execution paths",
762
- recommended: false,
763
- url: "https://github.com/howmanysmall/eslint-idiot-lint/tree/main/docs/rules/require-paired-calls.md",
764
- },
765
- fixable: "code",
766
- messages: {
767
- asyncViolation: "Cannot use {{asyncType}} between '{{opener}}' and '{{closer}}' (requireSync: true)",
768
- conditionalOpener: "Conditional opener '{{opener}}' at {{location}} may not have matching closer on all paths",
769
- maxNestingExceeded: "Maximum nesting depth of {{max}} exceeded for paired calls",
770
- multipleOpeners: "Multiple consecutive calls to '{{opener}}' without matching closers (allowMultipleOpeners: false)",
771
- robloxYieldViolation: "Yielding function '{{yieldingFunction}}' auto-closes all profiles - subsequent '{{closer}}' will error",
772
- unexpectedCloser: "Unexpected call to '{{closer}}' - expected one of: {{expected}}",
773
- unpairedCloser: "Unexpected call to '{{closer}}' - no matching opener on stack",
774
- unpairedOpener: "Unpaired call to '{{opener}}' - missing '{{closer}}' on {{paths}}",
775
- wrongOrder: "Closer '{{closer}}' called out of order - expected to close '{{expected}}' but '{{actual}}' is still open",
776
- },
777
- schema: [
778
- {
779
- additionalProperties: false,
780
- properties: {
781
- allowConditionalClosers: {
782
- default: false,
783
- type: "boolean",
784
- },
785
- allowMultipleOpeners: {
786
- default: true,
787
- type: "boolean",
788
- },
789
- maxNestingDepth: {
790
- default: 0,
791
- minimum: 0,
792
- type: "number",
793
- },
794
- pairs: {
795
- items: {
796
- additionalProperties: false,
797
- properties: {
798
- alternatives: {
799
- items: { minLength: 1, type: "string" },
800
- type: "array",
801
- },
802
- closer: {
803
- oneOf: [
804
- { minLength: 1, type: "string" },
805
- {
806
- items: { minLength: 1, type: "string" },
807
- minItems: 1,
808
- type: "array",
809
- },
810
- ],
811
- },
812
- opener: {
813
- minLength: 1,
814
- type: "string",
815
- },
816
- openerAlternatives: {
817
- items: { minLength: 1, type: "string" },
818
- type: "array",
819
- },
820
- platform: {
821
- enum: ["roblox"],
822
- type: "string",
823
- },
824
- requireSync: {
825
- default: false,
826
- type: "boolean",
827
- },
828
- yieldingFunctions: {
829
- items: { minLength: 1, type: "string" },
830
- type: "array",
831
- },
832
- },
833
- required: ["opener", "closer"],
834
- type: "object",
835
- },
836
- minItems: 1,
837
- type: "array",
838
- },
839
- },
840
- required: ["pairs"],
841
- type: "object",
842
- },
843
- ],
844
- type: "problem",
845
- },
846
- };
847
- export default rule;
848
- //# sourceMappingURL=require-paired-calls.js.map