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