@murky-web/typebuddy 3.0.1 → 3.1.0
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/README.md +20 -7
- package/dist/oxlint.js +2 -0
- package/dist/rules/async_rule.js +18 -1
- package/dist/rules/maybe_promise_rule.js +25 -7
- package/dist/rules/maybe_rule.js +5 -2
- package/dist/rules/nullable_rule.js +5 -2
- package/dist/rules/optional_rule.js +5 -2
- package/dist/rules/useless_undefined_rule.js +512 -0
- package/jsr.json +1 -1
- package/oxlint/index.ts +5 -1
- package/package.json +3 -3
- package/rules/async_rule.ts +28 -1
- package/rules/maybe_promise_rule.ts +27 -7
- package/rules/maybe_rule.ts +7 -3
- package/rules/nullable_rule.ts +6 -3
- package/rules/optional_rule.ts +6 -3
- package/rules/useless_undefined_rule.ts +982 -0
- package/src/result.ts +0 -2
|
@@ -0,0 +1,982 @@
|
|
|
1
|
+
// A fork of unicorn's `no-useless-undefined` (MIT, sindresorhus/eslint-plugin-
|
|
2
|
+
// unicorn), with the behaviour and options of oxlint's port of it
|
|
3
|
+
// (`oxc_linter/src/rules/unicorn/no_useless_undefined.rs`, oxlint 1.94), and
|
|
4
|
+
// one change: `let x: Optional<T> = undefined` is not reported. typebuddy wants
|
|
5
|
+
// every `let` initialised (`eslint/init-declarations`), and `undefined` is the
|
|
6
|
+
// honest start value of an `Optional<T>`, as `null` is of a `Maybe<T>`.
|
|
7
|
+
//
|
|
8
|
+
// Where oxlint and this fork differ, it is on purpose:
|
|
9
|
+
// - an un-annotated `let x = undefined` is reported without an autofix: the
|
|
10
|
+
// fix is a type or a real default, not `let x;`;
|
|
11
|
+
// - trailing `undefined` arguments are reported without an autofix (oxlint's
|
|
12
|
+
// is a dangerous fix that `--fix` does not apply either).
|
|
13
|
+
|
|
14
|
+
type Range = [number, number];
|
|
15
|
+
|
|
16
|
+
type Node = {
|
|
17
|
+
readonly type: string;
|
|
18
|
+
readonly range: Range;
|
|
19
|
+
readonly parent: Node;
|
|
20
|
+
readonly alternate?: Node;
|
|
21
|
+
readonly argument?: Node;
|
|
22
|
+
readonly arguments?: readonly Node[];
|
|
23
|
+
readonly bigint?: string;
|
|
24
|
+
readonly body?: Node;
|
|
25
|
+
readonly callee?: Node;
|
|
26
|
+
readonly computed?: boolean;
|
|
27
|
+
readonly consequent?: Node;
|
|
28
|
+
readonly delegate?: boolean;
|
|
29
|
+
readonly expression?: Node;
|
|
30
|
+
readonly expressions?: readonly Node[];
|
|
31
|
+
readonly id?: Node;
|
|
32
|
+
readonly init?: Node | null;
|
|
33
|
+
readonly kind?: string;
|
|
34
|
+
readonly left?: Node;
|
|
35
|
+
readonly name?: string;
|
|
36
|
+
readonly object?: Node;
|
|
37
|
+
readonly operator?: string;
|
|
38
|
+
readonly optional?: boolean;
|
|
39
|
+
readonly parameter?: Node;
|
|
40
|
+
readonly params?: readonly Node[];
|
|
41
|
+
readonly property?: Node;
|
|
42
|
+
readonly quasis?: readonly Node[];
|
|
43
|
+
readonly raw?: string;
|
|
44
|
+
readonly regex?: { pattern: string; flags: string };
|
|
45
|
+
readonly returnType?: Node | null;
|
|
46
|
+
readonly right?: Node;
|
|
47
|
+
readonly tag?: Node;
|
|
48
|
+
readonly test?: Node;
|
|
49
|
+
readonly typeAnnotation?: Node | null;
|
|
50
|
+
readonly typeName?: Node;
|
|
51
|
+
readonly types?: readonly Node[];
|
|
52
|
+
readonly value?: unknown;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
type Token = { readonly value: string; readonly range: Range };
|
|
56
|
+
|
|
57
|
+
type RuleFixer = {
|
|
58
|
+
removeRange(range: Range): unknown;
|
|
59
|
+
replaceTextRange(range: Range, text: string): unknown;
|
|
60
|
+
insertTextAfterRange(range: Range, text: string): unknown;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
type Fix = (fixer: RuleFixer) => unknown;
|
|
64
|
+
|
|
65
|
+
type Scope = {
|
|
66
|
+
readonly upper: Scope | null;
|
|
67
|
+
readonly references: readonly {
|
|
68
|
+
readonly identifier: unknown;
|
|
69
|
+
readonly resolved: { readonly defs: readonly unknown[] } | null;
|
|
70
|
+
}[];
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
type Options = {
|
|
74
|
+
checkArguments?: boolean;
|
|
75
|
+
checkArrowFunctionBody?: boolean;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
type RuleContext = {
|
|
79
|
+
readonly filename: string;
|
|
80
|
+
readonly options: readonly (Options | undefined)[];
|
|
81
|
+
readonly sourceCode: {
|
|
82
|
+
readonly text: string;
|
|
83
|
+
getText(node: Node): string;
|
|
84
|
+
getAllComments(): readonly { range: Range }[];
|
|
85
|
+
getTokenBefore(node: { range: Range }): Token | null;
|
|
86
|
+
getTokenAfter(node: { range: Range }): Token | null;
|
|
87
|
+
getLastToken(node: Node): Token | null;
|
|
88
|
+
getScope(node: Node): Scope;
|
|
89
|
+
};
|
|
90
|
+
report(descriptor: {
|
|
91
|
+
node: Node;
|
|
92
|
+
messageId: string;
|
|
93
|
+
data?: Record<string, string>;
|
|
94
|
+
fix?: Fix;
|
|
95
|
+
suggest?: {
|
|
96
|
+
messageId: string;
|
|
97
|
+
data?: Record<string, string>;
|
|
98
|
+
fix: Fix;
|
|
99
|
+
}[];
|
|
100
|
+
}): void;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const FUNCTION_TYPES = new Set([
|
|
104
|
+
"ArrowFunctionExpression",
|
|
105
|
+
"FunctionDeclaration",
|
|
106
|
+
"FunctionExpression"
|
|
107
|
+
]);
|
|
108
|
+
|
|
109
|
+
// Calls whose trailing `undefined` means something: assertions comparing with
|
|
110
|
+
// `undefined`, collection methods storing it, `createContext`/`useRef`/`ref`
|
|
111
|
+
// starting with it. oxlint's list; names starting with `set` count too.
|
|
112
|
+
const IGNORED_CALLEES = new Set([
|
|
113
|
+
"add",
|
|
114
|
+
"createContext",
|
|
115
|
+
"equal",
|
|
116
|
+
"has",
|
|
117
|
+
"include",
|
|
118
|
+
"includes",
|
|
119
|
+
"is",
|
|
120
|
+
"not",
|
|
121
|
+
"notEqual",
|
|
122
|
+
"notPropertyVal",
|
|
123
|
+
"notSame",
|
|
124
|
+
"notStrictEqual",
|
|
125
|
+
"property",
|
|
126
|
+
"propertyVal",
|
|
127
|
+
"push",
|
|
128
|
+
"ref",
|
|
129
|
+
"same",
|
|
130
|
+
"set",
|
|
131
|
+
"strictEqual",
|
|
132
|
+
"strictNotSame",
|
|
133
|
+
"strictSame",
|
|
134
|
+
"toBe",
|
|
135
|
+
"toContain",
|
|
136
|
+
"toContainEqual",
|
|
137
|
+
"toEqual",
|
|
138
|
+
"toHaveBeenCalledWith",
|
|
139
|
+
"unshift",
|
|
140
|
+
"useRef"
|
|
141
|
+
]);
|
|
142
|
+
|
|
143
|
+
const TS_WRAPPERS = new Set([
|
|
144
|
+
"TSAsExpression",
|
|
145
|
+
"TSInstantiationExpression",
|
|
146
|
+
"TSNonNullExpression",
|
|
147
|
+
"TSSatisfiesExpression",
|
|
148
|
+
"TSTypeAssertion"
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
const RELATIONAL = new Set(["<", "<=", ">", ">="]);
|
|
152
|
+
const FLIPPED: Record<string, string> = {
|
|
153
|
+
"<": ">",
|
|
154
|
+
"<=": ">=",
|
|
155
|
+
">": "<",
|
|
156
|
+
">=": "<="
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// Expressions that may run code or read something that changes, so dropping
|
|
160
|
+
// the bounds check in front of them could change behaviour.
|
|
161
|
+
const UNSAFE_IN_ACCESS = new Set([
|
|
162
|
+
"AssignmentExpression",
|
|
163
|
+
"AwaitExpression",
|
|
164
|
+
"CallExpression",
|
|
165
|
+
"ChainExpression",
|
|
166
|
+
"ClassExpression",
|
|
167
|
+
"ImportExpression",
|
|
168
|
+
"JSXElement",
|
|
169
|
+
"JSXFragment",
|
|
170
|
+
"MemberExpression",
|
|
171
|
+
"NewExpression",
|
|
172
|
+
"SpreadElement",
|
|
173
|
+
"TaggedTemplateExpression",
|
|
174
|
+
"UpdateExpression",
|
|
175
|
+
"YieldExpression"
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
// Parents through which the conditional can still start its statement.
|
|
179
|
+
const STATEMENT_START_PARENTS = new Set([
|
|
180
|
+
"AssignmentExpression",
|
|
181
|
+
"AwaitExpression",
|
|
182
|
+
"BinaryExpression",
|
|
183
|
+
"CallExpression",
|
|
184
|
+
"ChainExpression",
|
|
185
|
+
"ConditionalExpression",
|
|
186
|
+
"LogicalExpression",
|
|
187
|
+
"MemberExpression",
|
|
188
|
+
"SequenceExpression",
|
|
189
|
+
"TaggedTemplateExpression",
|
|
190
|
+
...TS_WRAPPERS
|
|
191
|
+
]);
|
|
192
|
+
|
|
193
|
+
const UNBRACED_BODY_PARENTS = new Set([
|
|
194
|
+
"DoWhileStatement",
|
|
195
|
+
"ForInStatement",
|
|
196
|
+
"ForOfStatement",
|
|
197
|
+
"ForStatement",
|
|
198
|
+
"IfStatement",
|
|
199
|
+
"LabeledStatement",
|
|
200
|
+
"WhileStatement",
|
|
201
|
+
"WithStatement"
|
|
202
|
+
]);
|
|
203
|
+
|
|
204
|
+
function isNode(value: unknown): value is Node {
|
|
205
|
+
return typeof value === "object" && value !== null && "type" in value;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function children(node: Node): Node[] {
|
|
209
|
+
const result: Node[] = [];
|
|
210
|
+
for (const [key, value] of Object.entries(node)) {
|
|
211
|
+
if (key === "parent") continue;
|
|
212
|
+
if (Array.isArray(value)) {
|
|
213
|
+
result.push(...value.filter(isNode));
|
|
214
|
+
} else if (isNode(value)) {
|
|
215
|
+
result.push(value);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function containsChain(node: Node): boolean {
|
|
222
|
+
return (
|
|
223
|
+
node.type === "ChainExpression" || children(node).some(containsChain)
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function isSafeAccessPart(node: Node): boolean {
|
|
228
|
+
if (UNSAFE_IN_ACCESS.has(node.type)) return false;
|
|
229
|
+
if (node.type === "UnaryExpression" && node.operator === "delete") {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
if (
|
|
233
|
+
node.type === "FunctionExpression" ||
|
|
234
|
+
node.type === "ArrowFunctionExpression"
|
|
235
|
+
) {
|
|
236
|
+
return !containsChain(node);
|
|
237
|
+
}
|
|
238
|
+
return children(node).every(isSafeAccessPart);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function inner(node: Node): Node {
|
|
242
|
+
let current = node;
|
|
243
|
+
while (TS_WRAPPERS.has(current.type) && current.expression) {
|
|
244
|
+
current = current.expression;
|
|
245
|
+
}
|
|
246
|
+
return current;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function staticNumber(node: Node): number | undefined {
|
|
250
|
+
const current = inner(node);
|
|
251
|
+
if (current.type === "Literal" && typeof current.value === "number") {
|
|
252
|
+
return current.value;
|
|
253
|
+
}
|
|
254
|
+
if (current.type === "UnaryExpression" && current.argument) {
|
|
255
|
+
const value = staticNumber(current.argument);
|
|
256
|
+
if (value === undefined) return undefined;
|
|
257
|
+
if (current.operator === "+") return value;
|
|
258
|
+
if (current.operator === "-") return -value;
|
|
259
|
+
}
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function isNumberLiteral(node: Node): boolean {
|
|
264
|
+
return node.type === "Literal" && typeof node.value === "number";
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function isBigIntLiteral(node: Node): boolean {
|
|
268
|
+
return node.type === "Literal" && typeof node.bigint === "string";
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function singleQuasi(node: Node): string | undefined {
|
|
272
|
+
if (node.expressions?.length !== 0 || node.quasis?.length !== 1) {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
const [quasi] = node.quasis;
|
|
276
|
+
const value = quasi?.value;
|
|
277
|
+
if (typeof value === "object" && value !== null && "cooked" in value) {
|
|
278
|
+
return String(value.cooked);
|
|
279
|
+
}
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function isSameSimpleExpression(left: Node, right: Node): boolean {
|
|
284
|
+
if (left.type === "Identifier" && right.type === "Identifier") {
|
|
285
|
+
return left.name === right.name;
|
|
286
|
+
}
|
|
287
|
+
if (left.type === "ThisExpression" || left.type === "Super") {
|
|
288
|
+
return left.type === right.type;
|
|
289
|
+
}
|
|
290
|
+
if (left.type === "TemplateLiteral" || right.type === "TemplateLiteral") {
|
|
291
|
+
const leftText =
|
|
292
|
+
left.type === "TemplateLiteral" ? singleQuasi(left) : left.value;
|
|
293
|
+
const rightText =
|
|
294
|
+
right.type === "TemplateLiteral" ? singleQuasi(right) : right.value;
|
|
295
|
+
return typeof leftText === "string" && leftText === rightText;
|
|
296
|
+
}
|
|
297
|
+
if (left.type !== "Literal" || right.type !== "Literal") return false;
|
|
298
|
+
if (left.regex || right.regex) {
|
|
299
|
+
return (
|
|
300
|
+
left.regex?.pattern === right.regex?.pattern &&
|
|
301
|
+
left.regex?.flags === right.regex?.flags
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
if (isNumberLiteral(left) || isNumberLiteral(right)) {
|
|
305
|
+
return left.raw === right.raw;
|
|
306
|
+
}
|
|
307
|
+
return left.value === right.value;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function isSameIndexReference(leftNode: Node, rightNode: Node): boolean {
|
|
311
|
+
const left = inner(leftNode);
|
|
312
|
+
const right = inner(rightNode);
|
|
313
|
+
if (isNumberLiteral(left) && isNumberLiteral(right)) {
|
|
314
|
+
return left.value === right.value;
|
|
315
|
+
}
|
|
316
|
+
if (isBigIntLiteral(left) && isBigIntLiteral(right)) {
|
|
317
|
+
return BigInt(left.bigint ?? "") === BigInt(right.bigint ?? "");
|
|
318
|
+
}
|
|
319
|
+
const simple =
|
|
320
|
+
left.type === "Literal" ||
|
|
321
|
+
left.type === "Identifier" ||
|
|
322
|
+
left.type === "ThisExpression" ||
|
|
323
|
+
left.type === "Super" ||
|
|
324
|
+
(left.type === "TemplateLiteral" && left.expressions?.length === 0);
|
|
325
|
+
return simple && isSameSimpleExpression(left, right);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Whether `test` makes `access` an in-bounds read (`true`) or an out-of-bounds
|
|
330
|
+
* one (`false`) when it passes; `undefined` when it says nothing about that.
|
|
331
|
+
*/
|
|
332
|
+
function boundsTestValidity(testNode: Node, access: Node): boolean | undefined {
|
|
333
|
+
const test = inner(testNode);
|
|
334
|
+
if (
|
|
335
|
+
test.type !== "BinaryExpression" ||
|
|
336
|
+
!test.left ||
|
|
337
|
+
!test.right ||
|
|
338
|
+
!test.operator ||
|
|
339
|
+
!RELATIONAL.has(test.operator) ||
|
|
340
|
+
!access.property ||
|
|
341
|
+
!access.object
|
|
342
|
+
) {
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
let index = inner(access.property);
|
|
347
|
+
let offset = 0;
|
|
348
|
+
if (
|
|
349
|
+
index.type === "BinaryExpression" &&
|
|
350
|
+
index.operator === "-" &&
|
|
351
|
+
index.left &&
|
|
352
|
+
index.right
|
|
353
|
+
) {
|
|
354
|
+
const subtracted = staticNumber(index.right);
|
|
355
|
+
if (
|
|
356
|
+
subtracted !== undefined &&
|
|
357
|
+
Number.isSafeInteger(subtracted) &&
|
|
358
|
+
subtracted > 0
|
|
359
|
+
) {
|
|
360
|
+
index = inner(index.left);
|
|
361
|
+
offset = subtracted;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
let operator = test.operator;
|
|
366
|
+
let boundary: Node;
|
|
367
|
+
if (isSameIndexReference(test.left, index)) {
|
|
368
|
+
boundary = inner(test.right);
|
|
369
|
+
} else if (isSameIndexReference(test.right, index)) {
|
|
370
|
+
operator = FLIPPED[operator] ?? operator;
|
|
371
|
+
boundary = inner(test.left);
|
|
372
|
+
} else {
|
|
373
|
+
return undefined;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const bound = staticNumber(boundary);
|
|
377
|
+
if (bound !== undefined && Number.isSafeInteger(bound)) {
|
|
378
|
+
if (operator === ">=" && bound === offset) return true;
|
|
379
|
+
if (operator === ">" && bound === offset - 1) return true;
|
|
380
|
+
if (operator === "<" && bound === offset) return false;
|
|
381
|
+
if (operator === "<=" && bound === offset - 1) return false;
|
|
382
|
+
return undefined;
|
|
383
|
+
}
|
|
384
|
+
if (offset !== 0) return undefined;
|
|
385
|
+
|
|
386
|
+
let length = boundary;
|
|
387
|
+
let minusOne = false;
|
|
388
|
+
if (
|
|
389
|
+
boundary.type === "BinaryExpression" &&
|
|
390
|
+
boundary.operator === "-" &&
|
|
391
|
+
boundary.left &&
|
|
392
|
+
boundary.right &&
|
|
393
|
+
staticNumber(boundary.right) === 1
|
|
394
|
+
) {
|
|
395
|
+
length = inner(boundary.left);
|
|
396
|
+
minusOne = true;
|
|
397
|
+
}
|
|
398
|
+
if (
|
|
399
|
+
length.type !== "MemberExpression" ||
|
|
400
|
+
length.computed === true ||
|
|
401
|
+
length.optional === true ||
|
|
402
|
+
length.property?.name !== "length" ||
|
|
403
|
+
!length.object ||
|
|
404
|
+
!isSameIndexReference(length.object, access.object)
|
|
405
|
+
) {
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if ((operator === "<" && !minusOne) || (operator === "<=" && minusOne)) {
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
if ((operator === ">=" && !minusOne) || (operator === ">" && minusOne)) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
return undefined;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** `Optional<…>`, or a union with `undefined` (or an `Optional`) in it. */
|
|
419
|
+
function admitsUndefined(type: Node | null | undefined): boolean {
|
|
420
|
+
if (!type) return false;
|
|
421
|
+
if (type.type === "TSTypeReference") {
|
|
422
|
+
return (
|
|
423
|
+
type.typeName?.type === "Identifier" &&
|
|
424
|
+
type.typeName.name === "Optional"
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
if (type.type === "TSUnionType") {
|
|
428
|
+
return (type.types ?? []).some(
|
|
429
|
+
(member) =>
|
|
430
|
+
member.type === "TSUndefinedKeyword" || admitsUndefined(member)
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function hasNonVoidReturnType(node: Node): boolean {
|
|
437
|
+
let current: Node | undefined = node.parent;
|
|
438
|
+
while (current && current.type !== "Program") {
|
|
439
|
+
if (FUNCTION_TYPES.has(current.type)) {
|
|
440
|
+
const type = current.returnType?.typeAnnotation?.type;
|
|
441
|
+
return (
|
|
442
|
+
type !== undefined &&
|
|
443
|
+
type !== "TSVoidKeyword" &&
|
|
444
|
+
type !== "TSUndefinedKeyword"
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
current = current.parent;
|
|
448
|
+
}
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** A default inside a destructuring assignment target, not a binding. */
|
|
453
|
+
function isInAssignmentTarget(pattern: Node): boolean {
|
|
454
|
+
let current = pattern;
|
|
455
|
+
for (;;) {
|
|
456
|
+
const { parent } = current;
|
|
457
|
+
if (
|
|
458
|
+
parent.type === "ArrayPattern" ||
|
|
459
|
+
parent.type === "ObjectPattern" ||
|
|
460
|
+
parent.type === "RestElement" ||
|
|
461
|
+
(parent.type === "AssignmentPattern" && parent.left === current) ||
|
|
462
|
+
(parent.type === "Property" &&
|
|
463
|
+
parent.parent.type === "ObjectPattern")
|
|
464
|
+
) {
|
|
465
|
+
current = parent;
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
return (
|
|
469
|
+
(parent.type === "AssignmentExpression" ||
|
|
470
|
+
parent.type === "ForInStatement" ||
|
|
471
|
+
parent.type === "ForOfStatement") &&
|
|
472
|
+
parent.left === current
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function isOptionalParameter(parameter: Node): boolean {
|
|
478
|
+
const target =
|
|
479
|
+
parameter.type === "TSParameterProperty" && parameter.parameter
|
|
480
|
+
? parameter.parameter
|
|
481
|
+
: parameter;
|
|
482
|
+
return target.optional === true;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function hasDefault(parameter: Node): boolean {
|
|
486
|
+
const target =
|
|
487
|
+
parameter.type === "TSParameterProperty" && parameter.parameter
|
|
488
|
+
? parameter.parameter
|
|
489
|
+
: parameter;
|
|
490
|
+
return target.type === "AssignmentPattern";
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const IDENTIFIER_START = /[\p{ID_Start}$_]/u;
|
|
494
|
+
const IDENTIFIER_PART = /[\p{ID_Continue}$]/u;
|
|
495
|
+
|
|
496
|
+
const rule = {
|
|
497
|
+
create(context: RuleContext) {
|
|
498
|
+
const sourceCode = context.sourceCode;
|
|
499
|
+
const text = sourceCode.text;
|
|
500
|
+
const options = context.options[0] ?? {};
|
|
501
|
+
const checkArguments = options.checkArguments ?? true;
|
|
502
|
+
const checkArrowFunctionBody = options.checkArrowFunctionBody ?? true;
|
|
503
|
+
const isTypeScript = /\.[cm]?tsx?$/u.test(context.filename);
|
|
504
|
+
|
|
505
|
+
// The global `undefined`: a reference nothing in the file declares.
|
|
506
|
+
// (`isGlobalReference` also needs `undefined` among the configured
|
|
507
|
+
// globals, which a bare config does not have.)
|
|
508
|
+
function isGlobalUndefined(node: Node | undefined): node is Node {
|
|
509
|
+
if (node?.type !== "Identifier" || node.name !== "undefined") {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
for (
|
|
513
|
+
let scope: Scope | null = sourceCode.getScope(node);
|
|
514
|
+
scope;
|
|
515
|
+
scope = scope.upper
|
|
516
|
+
) {
|
|
517
|
+
const reference = scope.references.find(
|
|
518
|
+
(ref) => ref.identifier === node
|
|
519
|
+
);
|
|
520
|
+
if (reference) {
|
|
521
|
+
return (
|
|
522
|
+
reference.resolved === null ||
|
|
523
|
+
reference.resolved.defs.length === 0
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function hasComments([start, end]: Range): boolean {
|
|
531
|
+
return sourceCode
|
|
532
|
+
.getAllComments()
|
|
533
|
+
.some(
|
|
534
|
+
(comment) =>
|
|
535
|
+
comment.range[0] >= start && comment.range[0] < end
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** `node`'s range including the parentheses around it. */
|
|
540
|
+
function parenthesized(node: Node): Range {
|
|
541
|
+
let range: Range = node.range;
|
|
542
|
+
for (;;) {
|
|
543
|
+
const before = sourceCode.getTokenBefore({ range });
|
|
544
|
+
const after = sourceCode.getTokenAfter({ range });
|
|
545
|
+
if (before?.value !== "(" || after?.value !== ")") return range;
|
|
546
|
+
range = [before.range[0], after.range[1]];
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function removeAfter(start: number, end: number): Fix {
|
|
551
|
+
return (fixer) =>
|
|
552
|
+
hasComments([start, end])
|
|
553
|
+
? null
|
|
554
|
+
: fixer.removeRange([start, end]);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function couldBeAsiHazard(node: Node): boolean {
|
|
558
|
+
let statement: Node | undefined;
|
|
559
|
+
for (let current = node.parent; ; current = current.parent) {
|
|
560
|
+
if (current.type === "ExpressionStatement") {
|
|
561
|
+
if (UNBRACED_BODY_PARENTS.has(current.parent.type))
|
|
562
|
+
return false;
|
|
563
|
+
statement = current;
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
if (!STATEMENT_START_PARENTS.has(current.type)) return false;
|
|
567
|
+
}
|
|
568
|
+
if (
|
|
569
|
+
node.range[0] !== statement.range[0] ||
|
|
570
|
+
statement.range[0] === 0
|
|
571
|
+
) {
|
|
572
|
+
return false;
|
|
573
|
+
}
|
|
574
|
+
const last = sourceCode.getTokenBefore(statement)?.value.at(-1);
|
|
575
|
+
return (
|
|
576
|
+
last !== undefined && /[\p{L}\p{N}_$)\]}"'`+\-/.]/u.test(last)
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function isReferenceContext(node: Node): boolean {
|
|
581
|
+
let expression = node;
|
|
582
|
+
for (let parent = node.parent; ; parent = parent.parent) {
|
|
583
|
+
if (TS_WRAPPERS.has(parent.type)) {
|
|
584
|
+
expression = parent;
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
if (parent.type === "CallExpression")
|
|
588
|
+
return parent.callee === expression;
|
|
589
|
+
if (parent.type === "TaggedTemplateExpression")
|
|
590
|
+
return parent.tag === expression;
|
|
591
|
+
return (
|
|
592
|
+
parent.type === "UnaryExpression" &&
|
|
593
|
+
parent.operator === "delete"
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function reportParameter(
|
|
599
|
+
node: Node,
|
|
600
|
+
pattern: Node,
|
|
601
|
+
item: Node,
|
|
602
|
+
fn: Node
|
|
603
|
+
): void {
|
|
604
|
+
const params = fn.params ?? [];
|
|
605
|
+
const position = params.indexOf(item);
|
|
606
|
+
if (params.slice(0, position).some(isOptionalParameter)) return;
|
|
607
|
+
|
|
608
|
+
const left = pattern.left;
|
|
609
|
+
if (!left) return;
|
|
610
|
+
const annotation = left.typeAnnotation;
|
|
611
|
+
const canBeOptional = params
|
|
612
|
+
.slice(position + 1)
|
|
613
|
+
.filter((parameter) => parameter.type !== "RestElement")
|
|
614
|
+
.every(
|
|
615
|
+
(parameter) =>
|
|
616
|
+
isOptionalParameter(parameter) || hasDefault(parameter)
|
|
617
|
+
);
|
|
618
|
+
const isIdentifier = left.type === "Identifier";
|
|
619
|
+
const name = isIdentifier
|
|
620
|
+
? (left.name ?? "")
|
|
621
|
+
: sourceCode.getText(left);
|
|
622
|
+
const typeText = annotation?.typeAnnotation
|
|
623
|
+
? sourceCode.getText(annotation.typeAnnotation)
|
|
624
|
+
: undefined;
|
|
625
|
+
const suggested =
|
|
626
|
+
isTypeScript && isIdentifier && !left.optional
|
|
627
|
+
? canBeOptional
|
|
628
|
+
? `${name}?: ${typeText ?? "T"}`
|
|
629
|
+
: `${name}: ${typeText ?? "Optional<T>"}`
|
|
630
|
+
: sourceCode.getText(left);
|
|
631
|
+
const descriptor = {
|
|
632
|
+
node,
|
|
633
|
+
messageId: "parameterUndefined",
|
|
634
|
+
data: { name, fix: suggested }
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
if (
|
|
638
|
+
isTypeScript &&
|
|
639
|
+
(!isIdentifier || !annotation || item !== pattern)
|
|
640
|
+
) {
|
|
641
|
+
context.report(descriptor);
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
const start = left.range[1];
|
|
646
|
+
const end = parenthesized(node)[1];
|
|
647
|
+
context.report({
|
|
648
|
+
...descriptor,
|
|
649
|
+
fix(fixer) {
|
|
650
|
+
if (hasComments([start, end])) return null;
|
|
651
|
+
const removal = fixer.removeRange([start, end]);
|
|
652
|
+
if (
|
|
653
|
+
!isTypeScript ||
|
|
654
|
+
left.optional ||
|
|
655
|
+
!canBeOptional ||
|
|
656
|
+
!annotation
|
|
657
|
+
) {
|
|
658
|
+
return removal;
|
|
659
|
+
}
|
|
660
|
+
const nameEnd =
|
|
661
|
+
sourceCode.getTokenBefore(annotation)?.range[1];
|
|
662
|
+
if (nameEnd === undefined) return removal;
|
|
663
|
+
return [
|
|
664
|
+
removal,
|
|
665
|
+
fixer.insertTextAfterRange([nameEnd, nameEnd], "?")
|
|
666
|
+
];
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
function onUndefined(node: Node): void {
|
|
672
|
+
const { parent } = node;
|
|
673
|
+
const outer = parenthesized(node);
|
|
674
|
+
|
|
675
|
+
switch (parent.type) {
|
|
676
|
+
case "ReturnStatement": {
|
|
677
|
+
if (hasNonVoidReturnType(parent)) return;
|
|
678
|
+
const before = sourceCode
|
|
679
|
+
.getAllComments()
|
|
680
|
+
.filter(
|
|
681
|
+
(comment) =>
|
|
682
|
+
comment.range[0] >= parent.range[0] &&
|
|
683
|
+
comment.range[0] < outer[0]
|
|
684
|
+
)
|
|
685
|
+
.at(-1);
|
|
686
|
+
const start = before
|
|
687
|
+
? before.range[1]
|
|
688
|
+
: parent.range[0] + "return".length;
|
|
689
|
+
context.report({
|
|
690
|
+
node,
|
|
691
|
+
messageId: "returnUndefined",
|
|
692
|
+
fix: removeAfter(start, outer[1])
|
|
693
|
+
});
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
case "YieldExpression": {
|
|
697
|
+
if (parent.delegate === true) return;
|
|
698
|
+
context.report({
|
|
699
|
+
node,
|
|
700
|
+
messageId: "yieldUndefined",
|
|
701
|
+
fix: (fixer) =>
|
|
702
|
+
hasComments(parent.range)
|
|
703
|
+
? null
|
|
704
|
+
: fixer.replaceTextRange(parent.range, "yield")
|
|
705
|
+
});
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
case "ArrowFunctionExpression": {
|
|
709
|
+
if (
|
|
710
|
+
parent.body !== node ||
|
|
711
|
+
!checkArrowFunctionBody ||
|
|
712
|
+
parent.returnType
|
|
713
|
+
) {
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
context.report({
|
|
717
|
+
node,
|
|
718
|
+
messageId: "arrowUndefined",
|
|
719
|
+
fix: (fixer) =>
|
|
720
|
+
hasComments(outer)
|
|
721
|
+
? null
|
|
722
|
+
: fixer.replaceTextRange(outer, "{}")
|
|
723
|
+
});
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
case "VariableDeclarator": {
|
|
727
|
+
const id = parent.id;
|
|
728
|
+
if (
|
|
729
|
+
parent.init !== node ||
|
|
730
|
+
parent.parent.kind !== "let" ||
|
|
731
|
+
id?.type !== "Identifier" ||
|
|
732
|
+
// typebuddy: `undefined` is the start value of an
|
|
733
|
+
// `Optional<T>`; `eslint/init-declarations` wants it.
|
|
734
|
+
admitsUndefined(id.typeAnnotation?.typeAnnotation)
|
|
735
|
+
) {
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
context.report({
|
|
739
|
+
node,
|
|
740
|
+
messageId: "letUndefined",
|
|
741
|
+
data: {
|
|
742
|
+
declarator: sourceCode.getText(parent),
|
|
743
|
+
name: id.name ?? ""
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
case "AssignmentPattern": {
|
|
749
|
+
if (parent.right !== node) return;
|
|
750
|
+
const owner = parent.parent;
|
|
751
|
+
if (FUNCTION_TYPES.has(owner.type)) {
|
|
752
|
+
reportParameter(node, parent, parent, owner);
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
if (owner.type === "TSParameterProperty") {
|
|
756
|
+
reportParameter(node, parent, owner, owner.parent);
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
if (isInAssignmentTarget(parent) || !parent.left) return;
|
|
760
|
+
const descriptor = {
|
|
761
|
+
node,
|
|
762
|
+
messageId: "defaultUndefined",
|
|
763
|
+
data: { name: sourceCode.getText(parent.left) }
|
|
764
|
+
};
|
|
765
|
+
context.report(
|
|
766
|
+
isTypeScript
|
|
767
|
+
? descriptor
|
|
768
|
+
: {
|
|
769
|
+
...descriptor,
|
|
770
|
+
fix: removeAfter(
|
|
771
|
+
parent.left.range[1],
|
|
772
|
+
outer[1]
|
|
773
|
+
)
|
|
774
|
+
}
|
|
775
|
+
);
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
default:
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
function onCall(node: Node): void {
|
|
783
|
+
const args = node.arguments ?? [];
|
|
784
|
+
const callee = node.callee;
|
|
785
|
+
if (!callee || !isTrailingUndefined(node, args.at(-1))) return;
|
|
786
|
+
|
|
787
|
+
const calleeName =
|
|
788
|
+
callee.type === "Identifier"
|
|
789
|
+
? callee.name
|
|
790
|
+
: callee.type === "MemberExpression" &&
|
|
791
|
+
callee.computed !== true &&
|
|
792
|
+
callee.property?.type === "Identifier"
|
|
793
|
+
? callee.property.name
|
|
794
|
+
: undefined;
|
|
795
|
+
if (
|
|
796
|
+
calleeName !== undefined &&
|
|
797
|
+
(IGNORED_CALLEES.has(calleeName) ||
|
|
798
|
+
calleeName.startsWith("set"))
|
|
799
|
+
) {
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
// `fn.bind(thisArg, undefined)` binds an argument; only a lone
|
|
803
|
+
// `undefined` `this` is useless.
|
|
804
|
+
if (
|
|
805
|
+
node.optional !== true &&
|
|
806
|
+
callee.type === "MemberExpression" &&
|
|
807
|
+
staticPropertyName(callee) === "bind" &&
|
|
808
|
+
args.length !== 1
|
|
809
|
+
) {
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
let first = args.length - 1;
|
|
814
|
+
while (first > 0 && isTrailingUndefined(node, args[first - 1]))
|
|
815
|
+
first -= 1;
|
|
816
|
+
const firstUndefined = args[first];
|
|
817
|
+
if (!firstUndefined) return;
|
|
818
|
+
|
|
819
|
+
context.report({
|
|
820
|
+
node: firstUndefined,
|
|
821
|
+
messageId: "argumentUndefined",
|
|
822
|
+
data: { callee: sourceCode.getText(callee) }
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
function isTrailingUndefined(
|
|
827
|
+
call: Node,
|
|
828
|
+
argument: Node | undefined
|
|
829
|
+
): boolean {
|
|
830
|
+
if (!isGlobalUndefined(argument)) return false;
|
|
831
|
+
// `foo((undefined))` is left alone, as oxlint does.
|
|
832
|
+
const after = sourceCode.getTokenAfter(argument);
|
|
833
|
+
const last = sourceCode.getLastToken(call);
|
|
834
|
+
return !(after?.value === ")" && after.range[0] !== last?.range[0]);
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
function staticPropertyName(member: Node): string | undefined {
|
|
838
|
+
const property = member.property;
|
|
839
|
+
if (!property) return undefined;
|
|
840
|
+
if (member.computed !== true) return property.name;
|
|
841
|
+
if (
|
|
842
|
+
property.type === "Literal" &&
|
|
843
|
+
typeof property.value === "string"
|
|
844
|
+
) {
|
|
845
|
+
return property.value;
|
|
846
|
+
}
|
|
847
|
+
if (property.type === "TemplateLiteral")
|
|
848
|
+
return singleQuasi(property);
|
|
849
|
+
return undefined;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
function onConditional(node: Node): void {
|
|
853
|
+
const { consequent, alternate, test } = node;
|
|
854
|
+
if (!consequent || !alternate || !test) return;
|
|
855
|
+
const consequentUndefined = isGlobalUndefined(consequent);
|
|
856
|
+
if (consequentUndefined === isGlobalUndefined(alternate)) return;
|
|
857
|
+
|
|
858
|
+
const [undefinedNode, branch] = consequentUndefined
|
|
859
|
+
? [consequent, alternate]
|
|
860
|
+
: [alternate, consequent];
|
|
861
|
+
const access = inner(branch);
|
|
862
|
+
if (
|
|
863
|
+
access.type !== "MemberExpression" ||
|
|
864
|
+
access.computed !== true ||
|
|
865
|
+
access.optional === true ||
|
|
866
|
+
!access.object ||
|
|
867
|
+
!access.property ||
|
|
868
|
+
boundsTestValidity(test, access) !== !consequentUndefined ||
|
|
869
|
+
!isSafeAccessPart(access.object) ||
|
|
870
|
+
!isSafeAccessPart(access.property)
|
|
871
|
+
) {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
const branchRange = parenthesized(branch);
|
|
876
|
+
const accessText = text.slice(branchRange[0], branchRange[1]);
|
|
877
|
+
const descriptor = {
|
|
878
|
+
node: undefinedNode,
|
|
879
|
+
messageId: "guardedIndexedAccess",
|
|
880
|
+
data: { access: accessText }
|
|
881
|
+
};
|
|
882
|
+
if (hasComments(node.range) || isReferenceContext(node)) {
|
|
883
|
+
context.report(descriptor);
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
let replacement = accessText;
|
|
888
|
+
const objectType = inner(access.object).type;
|
|
889
|
+
if (
|
|
890
|
+
(replacement.startsWith("<") ||
|
|
891
|
+
objectType === "ObjectExpression" ||
|
|
892
|
+
objectType === "FunctionExpression") &&
|
|
893
|
+
!replacement.startsWith("(")
|
|
894
|
+
) {
|
|
895
|
+
replacement = `(${replacement})`;
|
|
896
|
+
}
|
|
897
|
+
if (/^[[(/+\-`]/u.test(replacement) && couldBeAsiHazard(node)) {
|
|
898
|
+
replacement = `;${replacement}`;
|
|
899
|
+
}
|
|
900
|
+
const [start, end] = node.range;
|
|
901
|
+
if (
|
|
902
|
+
start > 0 &&
|
|
903
|
+
IDENTIFIER_PART.test(text[start - 1] ?? "") &&
|
|
904
|
+
IDENTIFIER_PART.test(replacement[0] ?? "")
|
|
905
|
+
) {
|
|
906
|
+
replacement = ` ${replacement}`;
|
|
907
|
+
}
|
|
908
|
+
if (
|
|
909
|
+
end < text.length &&
|
|
910
|
+
IDENTIFIER_START.test(text[end] ?? "") &&
|
|
911
|
+
!/\s$/u.test(replacement)
|
|
912
|
+
) {
|
|
913
|
+
replacement = `${replacement} `;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
context.report({
|
|
917
|
+
...descriptor,
|
|
918
|
+
suggest: [
|
|
919
|
+
{
|
|
920
|
+
messageId: "useIndexedAccess",
|
|
921
|
+
data: { access: accessText },
|
|
922
|
+
fix: (fixer) =>
|
|
923
|
+
fixer.replaceTextRange(node.range, replacement)
|
|
924
|
+
}
|
|
925
|
+
]
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
return {
|
|
930
|
+
Identifier(node: Node) {
|
|
931
|
+
if (isGlobalUndefined(node)) onUndefined(node);
|
|
932
|
+
},
|
|
933
|
+
CallExpression(node: Node) {
|
|
934
|
+
if (checkArguments) onCall(node);
|
|
935
|
+
},
|
|
936
|
+
ConditionalExpression: onConditional
|
|
937
|
+
};
|
|
938
|
+
},
|
|
939
|
+
meta: {
|
|
940
|
+
type: "suggestion",
|
|
941
|
+
docs: {
|
|
942
|
+
description:
|
|
943
|
+
"Disallow useless `undefined`, except as the start value of an `Optional<T>` `let`"
|
|
944
|
+
},
|
|
945
|
+
fixable: "code",
|
|
946
|
+
hasSuggestions: true,
|
|
947
|
+
defaultOptions: [
|
|
948
|
+
{ checkArguments: true, checkArrowFunctionBody: true }
|
|
949
|
+
],
|
|
950
|
+
schema: [
|
|
951
|
+
{
|
|
952
|
+
type: "object",
|
|
953
|
+
additionalProperties: false,
|
|
954
|
+
properties: {
|
|
955
|
+
checkArguments: { type: "boolean" },
|
|
956
|
+
checkArrowFunctionBody: { type: "boolean" }
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
],
|
|
960
|
+
messages: {
|
|
961
|
+
letUndefined:
|
|
962
|
+
"`let {{declarator}}` starts `{{name}}` at `undefined`, but no `Optional<…>` type says `{{name}}` may hold it; without an annotation TypeScript types `{{name}}` from later assignments. Fix: annotate it, `let {{name}}: Optional<T> = undefined;` (`undefined` is the start value of an `Optional<T>`; a `Maybe<T>` starts at `null`: `let {{name}}: Maybe<T> = null;`), or give `{{name}}` its real default. Do not: drop the initializer (`let {{name}};`), `eslint/init-declarations` reports a `let` without one.",
|
|
963
|
+
returnUndefined:
|
|
964
|
+
"`return undefined` spells out what a bare `return` already returns. Fix: `return;`, or annotate the function's return type as `Optional<T>` when `undefined` is one of its results (then the rule keeps the `undefined`).",
|
|
965
|
+
yieldUndefined:
|
|
966
|
+
"`yield undefined` spells out what a bare `yield` already yields. Fix: `yield;`.",
|
|
967
|
+
arrowUndefined:
|
|
968
|
+
"The arrow body `undefined` spells out what an empty body already returns. Fix: `=> {}`, or annotate the return type (`(): Optional<T> => undefined`) when `undefined` is the arrow's result.",
|
|
969
|
+
argumentUndefined:
|
|
970
|
+
"The trailing `undefined` passed to `{{callee}}` is what an omitted argument already is. Fix: drop the trailing `undefined` arguments from the `{{callee}}(…)` call, unless `{{callee}}` reads `arguments.length` or a rest parameter (then keep them and disable the line).",
|
|
971
|
+
defaultUndefined:
|
|
972
|
+
"The default `{{name}} = undefined` spells out what a missing property or element already is. Fix: `{{name}}`.",
|
|
973
|
+
parameterUndefined:
|
|
974
|
+
"The default `= undefined` on `{{name}}` spells out what an omitted argument already is. Fix: `{{fix}}`.",
|
|
975
|
+
guardedIndexedAccess:
|
|
976
|
+
"`{{access}}` is already `undefined` out of bounds, so this bounds check and its `undefined` branch repeat it. Fix: `{{access}}` on its own (the editor suggestion applies it).",
|
|
977
|
+
useIndexedAccess: "Use `{{access}}` directly."
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
};
|
|
981
|
+
|
|
982
|
+
export { rule as uselessUndefinedRule };
|