-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathno-unnecessary-condition.ts
797 lines (739 loc) · 25.7 KB
/
no-unnecessary-condition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
// This rule is based on typescript-eslint's no-unnecessary-condition rule
// and modified to work well with Svelte components.
// https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
import type { TSESTree } from "@typescript-eslint/types"
import type { AST } from "svelte-eslint-parser"
import { createRule } from "../../utils"
import {
isFalsyType,
getConstrainedTypeAtLocation,
isTruthyLiteral,
isPossiblyFalsyType,
isNullishType,
isBooleanLiteralType,
getTypeScriptTools,
isAnyType,
isUnknownType,
isNeverType,
getCallSignaturesOfType,
isNullableType,
getTypeOfPropertyOfType,
getTypeName,
isTupleType,
} from "../../utils/ts-utils"
import type { TS, TSTools } from "../../utils/ts-utils"
/**
* Returns all types of a union type or an array containing `type` itself if it's no union type.
* This method is heavily inspired by tsutils. https://github.com/ajafff/tsutils
* The MIT License (MIT) Copyright (c) 2017 Klaus Meinhardt
* https://github.com/ajafff/tsutils/blob/master/LICENSE
*/
function unionTypeParts(type: TS.Type): TS.Type[] {
return [...iterate(type)]
/**
* iterate
*/
function* iterate(t: TS.Type): Iterable<TS.Type> {
if (t.isUnion()) {
for (const type of t.types) {
yield* iterate(type)
}
} else {
yield t
}
}
}
/**
* Check whether the given type can be a falsy type or not.
*/
function isPossiblyFalsy(type: TS.Type, tsTools: TSTools): boolean {
return (
unionTypeParts(type)
// PossiblyFalsy flag includes literal values, so exclude ones that
// are definitely truthy
.filter((t) => !isTruthyLiteral(t, tsTools))
.some((type) => isPossiblyFalsyType(type, tsTools.ts))
)
}
/**
* Check whether the given type can be a truthy type or not.
*/
function isPossiblyTruthy(type: TS.Type, tsTools: TSTools): boolean {
return unionTypeParts(type).some((type) => !isFalsyType(type, tsTools))
}
/**
* Check whether the given type can be a nullish type or not.
*/
function isPossiblyNullish(type: TS.Type, tsTools: TSTools): boolean {
return isNullableType(type, tsTools.ts)
}
/**
* Check whether the given type is a nullish type or not.
*/
function isAlwaysNullish(type: TS.Type, tsTools: TSTools): boolean {
return isNullishType(type, tsTools.ts)
}
/**
* Check whether the given type is a literal type or not.
*/
function isLiteral(type: TS.Type, tsTools: TSTools): boolean {
return (
isBooleanLiteralType(type, tsTools.ts) ||
isNullishType(type, tsTools.ts) ||
type.isLiteral()
)
}
export default createRule("@typescript-eslint/no-unnecessary-condition", {
meta: {
docs: {
description:
"disallow conditionals where the type is always truthy or always falsy",
category: "Extension Rules",
recommended: false,
extensionRule: {
plugin: "@typescript-eslint/eslint-plugin",
url: "https://typescript-eslint.io/rules/no-unnecessary-condition/",
},
},
schema: [
{
type: "object",
properties: {
allowConstantLoopConditions: {
description:
"Whether to ignore constant loop conditions, such as `while (true)`.",
type: "boolean",
},
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: {
description:
"Whether to not error when running with a tsconfig that has strictNullChecks turned.",
type: "boolean",
},
},
additionalProperties: false,
},
],
fixable: "code",
messages: {
alwaysTruthy: "Unnecessary conditional, value is always truthy.",
alwaysFalsy: "Unnecessary conditional, value is always falsy.",
alwaysTruthyFunc:
"This callback should return a conditional, but return is always truthy.",
alwaysFalsyFunc:
"This callback should return a conditional, but return is always falsy.",
neverNullish:
"Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined.",
alwaysNullish:
"Unnecessary conditional, left-hand side of `??` operator is always `null` or `undefined`.",
literalBooleanExpression:
"Unnecessary conditional, both sides of the expression are literal values.",
noOverlapBooleanExpression:
"Unnecessary conditional, the types have no overlap.",
never: "Unnecessary conditional, value is `never`.",
neverOptionalChain: "Unnecessary optional chain on a non-nullish value.",
noStrictNullCheck:
"This rule requires the `strictNullChecks` compiler option to be turned on to function correctly.",
},
type: "suggestion", // "problem", or "layout",
deprecated: true,
replacedBy: {
note: "This rule is no longer needed when using svelte-eslint-parser>=v0.19.0.",
},
},
create(context) {
const {
allowConstantLoopConditions = false,
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing = false,
} = (context.options[0] || {}) as {
allowConstantLoopConditions?: boolean
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean
}
const tools = getTypeScriptTools(context)
if (!tools) {
return {}
}
const { service, ts } = tools
const checker = service.program.getTypeChecker()
const sourceCode = context.getSourceCode()
const compilerOptions = service.program.getCompilerOptions()
const isStrictNullChecks = compilerOptions.strict
? compilerOptions.strictNullChecks !== false
: compilerOptions.strictNullChecks
if (
!isStrictNullChecks &&
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing !== true
) {
context.report({
loc: {
start: { line: 0, column: 0 },
end: { line: 0, column: 0 },
},
messageId: "noStrictNullCheck",
})
}
const mutableVarReferenceIds: TSESTree.Identifier[] = []
const scriptElements: AST.SvelteScriptElement[] = []
let inSvelteReactiveStatement = false
// Extract references to mutable variables in the root scope.
for (const scope of [
sourceCode.scopeManager.globalScope,
sourceCode.scopeManager.globalScope?.childScopes.find(
(scope) => scope.type === "module",
),
]) {
if (!scope) continue
for (const variable of scope.variables) {
if (
variable.defs.some(
(def) =>
def.type === "Variable" &&
(def.parent.kind === "var" || def.parent.kind === "let"),
)
) {
for (const reference of variable.references) {
mutableVarReferenceIds.push(reference.identifier)
}
}
}
}
// Extract <script> ranges.
for (const body of sourceCode.ast.body) {
if (body.type === "SvelteScriptElement") {
scriptElements.push(body)
}
}
/**
* Checks whether the given expression node is in Svelte reactive scope
* and the variables that make up the given expression node use
* mutable variables declared in component root scope.
*/
function hasSvelteReactiveVar(
node: TSESTree.Expression | TSESTree.SpreadElement,
): boolean {
const inReactiveScope =
inSvelteReactiveStatement ||
(scriptElements.length &&
scriptElements.every(
(elem) =>
node.range[1] <= elem.range[0] || elem.range[1] <= node.range[0],
))
if (!inReactiveScope) {
// The given expression node is neither in a reactive scope nor in a template scope.
return false
}
return mutableVarReferenceIds.some(
(id) => node.range[0] <= id.range[0] && id.range[1] <= node.range[1],
)
}
/** Get the TS type from ES Node */
function getNodeType(
node:
| TSESTree.Expression
| TSESTree.PrivateIdentifier
| TSESTree.SpreadElement,
): TS.Type | undefined {
const tsNode = service.esTreeNodeToTSNodeMap.get(node)
return tsNode && getConstrainedTypeAtLocation(checker, tsNode)
}
/**
* Check whether the given node is an array type or not.
*/
function nodeIsArrayType(node: TSESTree.Expression): boolean {
const nodeType = getNodeType(node)
if (!nodeType) {
return false
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- isArrayType is an internal API of TS.
return (checker as any).isArrayType(nodeType)
}
/**
* Check whether the given node is an tuple type or not.
*/
function nodeIsTupleType(node: TSESTree.Expression): boolean {
const nodeType = getNodeType(node)
return Boolean(nodeType && isTupleType(nodeType, ts))
}
/**
* Check whether the given node is an array index signature or not.
*/
function isArrayIndexExpression(node: TSESTree.Expression): boolean {
return (
// Is an index signature
node.type === "MemberExpression" &&
node.computed &&
// ...into an array type
(nodeIsArrayType(node.object) ||
// ... or a tuple type
(nodeIsTupleType(node.object) &&
// Exception: literal index into a tuple - will have a sound type
node.property.type !== "Literal"))
)
}
/**
* Checks if a conditional node is necessary:
* if the type of the node is always true or always false, it's not necessary.
*/
function checkNode(
node: TSESTree.Expression,
isUnaryNotArgument = false,
): void {
if (hasSvelteReactiveVar(node)) {
return
}
// Check if the node is Unary Negation expression and handle it
if (node.type === "UnaryExpression" && node.operator === "!") {
checkNode(node.argument, true)
return
}
// Since typescript array index signature types don't represent the
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (isArrayIndexExpression(node)) {
return
}
// When checking logical expressions, only check the right side
// as the left side has been checked by checkLogicalExpressionForUnnecessaryConditionals
//
// Unless the node is nullish coalescing, as it's common to use patterns like `nullBool ?? true` to to strict
// boolean checks if we inspect the right here, it'll usually be a constant condition on purpose.
// In this case it's better to inspect the type of the expression as a whole.
if (node.type === "LogicalExpression" && node.operator !== "??") {
checkNode(node.right)
return
}
const type = getNodeType(node)
// Conditional is always necessary if it involves:
// `any` or `unknown` or a naked type parameter
if (
!type ||
unionTypeParts(type).some(
(part) =>
isAnyType(part, ts) ||
isUnknownType(part, ts) ||
part.isTypeParameter(),
)
) {
return
}
let messageId: string | null = null
if (unionTypeParts(type).some((part) => isNeverType(part, ts))) {
messageId = "never"
} else if (!isPossiblyTruthy(type, tools!)) {
messageId = !isUnaryNotArgument ? "alwaysFalsy" : "alwaysTruthy"
} else if (!isPossiblyFalsy(type, tools!)) {
messageId = !isUnaryNotArgument ? "alwaysTruthy" : "alwaysFalsy"
}
if (messageId) {
context.report({ node, messageId })
}
}
/**
* Checks if a conditional node is necessary from the given lhs of nullish coalescing.
*/
function checkNodeForNullish(node: TSESTree.Expression): void {
if (hasSvelteReactiveVar(node)) {
return
}
const type = getNodeType(node)
// Conditional is always necessary if it involves `any` or `unknown`
if (!type || isAnyType(type, ts) || isUnknownType(type, ts)) {
return
}
let messageId: string | null = null
if (unionTypeParts(type).some((part) => isNeverType(part, ts))) {
messageId = "never"
} else if (!isPossiblyNullish(type, tools!)) {
// Since typescript array index signature types don't represent the
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (
!isArrayIndexExpression(node) &&
!(
node.type === "ChainExpression" &&
node.expression.type !== "TSNonNullExpression" &&
optionChainContainsOptionArrayIndex(node.expression)
)
) {
messageId = "neverNullish"
}
} else if (isAlwaysNullish(type, tools!)) {
messageId = "alwaysNullish"
}
if (messageId) {
context.report({ node, messageId })
}
}
/**
* Checks that a binary expression is necessarily conditional, reports otherwise.
* If both sides of the binary expression are literal values, it's not a necessary condition.
*
* NOTE: It's also unnecessary if the types that don't overlap at all
* but that case is handled by the Typescript compiler itself.
* Known exceptions:
* * https://github.com/microsoft/TypeScript/issues/32627
* * https://github.com/microsoft/TypeScript/issues/37160 (handled)
*/
const BOOL_OPERATORS = new Set([
"<",
">",
"<=",
">=",
"==",
"===",
"!=",
"!==",
])
/**
* Checks if a conditional node is necessary from the given binary expression.
*/
function checkIfBinaryExpressionIsNecessaryConditional(
node: TSESTree.BinaryExpression,
): void {
if (hasSvelteReactiveVar(node)) {
return
}
if (!BOOL_OPERATORS.has(node.operator)) {
return
}
const leftType = getNodeType(node.left)
const rightType = getNodeType(node.right)
if (!leftType || !rightType) {
return
}
if (isLiteral(leftType, tools!) && isLiteral(rightType, tools!)) {
context.report({ node, messageId: "literalBooleanExpression" })
return
}
// Workaround for https://github.com/microsoft/TypeScript/issues/37160
if (isStrictNullChecks) {
const UNDEFINED = ts.TypeFlags.Undefined
const NULL = ts.TypeFlags.Null
// eslint-disable-next-line func-style -- ignore
const isComparable = (type: TS.Type, f: TS.TypeFlags): boolean => {
let flag = f
// Allow comparison to `any`, `unknown` or a naked type parameter.
flag |=
ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter
// Allow loose comparison to nullish values.
if (node.operator === "==" || node.operator === "!=") {
flag |= NULL | UNDEFINED
}
return unionTypeParts(type).some((t) => (t.flags & flag) !== 0)
}
if (
(leftType.flags === UNDEFINED &&
!isComparable(rightType, UNDEFINED)) ||
(rightType.flags === UNDEFINED &&
!isComparable(leftType, UNDEFINED)) ||
(leftType.flags === NULL && !isComparable(rightType, NULL)) ||
(rightType.flags === NULL && !isComparable(leftType, NULL))
) {
context.report({ node, messageId: "noOverlapBooleanExpression" })
}
}
}
/**
* Checks that a logical expression contains a boolean, reports otherwise.
*/
function checkLogicalExpressionForUnnecessaryConditionals(
node: TSESTree.LogicalExpression,
): void {
if (node.operator === "??") {
checkNodeForNullish(node.left)
return
}
// Only checks the left side, since the right side might not be "conditional" at all.
// The right side will be checked if the LogicalExpression is used in a conditional context
checkNode(node.left)
}
/**
* Checks that a testable expression of a loop is necessarily conditional, reports otherwise.
*/
function checkIfLoopIsNecessaryConditional(
node:
| TSESTree.DoWhileStatement
| TSESTree.ForStatement
| TSESTree.WhileStatement,
): void {
if (node.test === null) {
// e.g. `for(;;)`
return
}
/**
* Allow:
* while (true) {}
* for (;true;) {}
* do {} while (true)
*/
if (allowConstantLoopConditions) {
const nodeType = getNodeType(node.test)
if (
nodeType &&
isBooleanLiteralType(nodeType, ts) &&
checker.typeToString(nodeType) === "true"
)
return
}
checkNode(node.test)
}
const ARRAY_PREDICATE_FUNCTIONS = new Set([
"filter",
"find",
"some",
"every",
])
/**
* Checks whether the given call expression is an array method that takes a predicate as an argument.
*/
function isArrayPredicateFunction(node: TSESTree.CallExpression): boolean {
const { callee } = node
return (
// looks like `something.filter` or `something.find`
callee.type === "MemberExpression" &&
callee.property.type === "Identifier" &&
ARRAY_PREDICATE_FUNCTIONS.has(callee.property.name) &&
// and the left-hand side is an array, according to the types
(nodeIsArrayType(callee.object) || nodeIsTupleType(callee.object))
)
}
/**
* Checks if a conditional node is necessary from the given call expression.
*/
function checkCallExpression(node: TSESTree.CallExpression): void {
// If this is something like arr.filter(x => /*condition*/), check `condition`
if (isArrayPredicateFunction(node) && node.arguments.length) {
const callback = node.arguments[0]
// Inline defined functions
if (
(callback.type === "ArrowFunctionExpression" ||
callback.type === "FunctionExpression") &&
callback.body
) {
// Two special cases, where we can directly check the node that's returned:
// () => something
if (callback.body.type !== "BlockStatement") {
checkNode(callback.body)
return
}
// () => { return something; }
const callbackBody = callback.body.body
if (
callbackBody.length === 1 &&
callbackBody[0].type === "ReturnStatement" &&
callbackBody[0].argument
) {
checkNode(callbackBody[0].argument)
return
}
// Potential enhancement: could use code-path analysis to check
// any function with a single return statement
// (Value to complexity ratio is dubious however)
}
const nodeType = getNodeType(callback)
if (!nodeType) {
return
}
// Otherwise just do type analysis on the function as a whole.
const returnTypes = getCallSignaturesOfType(nodeType).map((sig) =>
sig.getReturnType(),
)
/* istanbul ignore if */ if (returnTypes.length === 0) {
// Not a callable function
return
}
// Predicate is always necessary if it involves `any` or `unknown`
if (returnTypes.some((t) => isAnyType(t, ts) || isUnknownType(t, ts))) {
return
}
if (!returnTypes.some((t) => isPossiblyFalsy(t, tools!))) {
context.report({
node: callback,
messageId: "alwaysTruthyFunc",
})
return
}
if (!returnTypes.some((t) => isPossiblyTruthy(t, tools!))) {
context.report({
node: callback,
messageId: "alwaysFalsyFunc",
})
}
}
}
/**
*
* Recursively searches an optional chain for an array index expression
* Has to search the entire chain, because an array index will "infect" the rest of the types
* Example:
* ```
* [{x: {y: "z"} }][n] // type is {x: {y: "z"}}
* ?.x // type is {y: "z"}
* ?.y // This access is considered "unnecessary" according to the types
* ```
*/
function optionChainContainsOptionArrayIndex(
node: TSESTree.MemberExpression | TSESTree.CallExpression,
): boolean {
const lhsNode = node.type === "CallExpression" ? node.callee : node.object
if (node.optional && isArrayIndexExpression(lhsNode)) {
return true
}
if (
lhsNode.type === "MemberExpression" ||
lhsNode.type === "CallExpression"
) {
return optionChainContainsOptionArrayIndex(lhsNode)
}
return false
}
/**
* Check whether the given property is a nullable or not.
*/
function isNullablePropertyType(
objType: TS.Type,
propertyType: TS.Type,
): boolean {
if (propertyType.isUnion()) {
return propertyType.types.some((type) =>
isNullablePropertyType(objType, type),
)
}
if (propertyType.isNumberLiteral() || propertyType.isStringLiteral()) {
const propType = getTypeOfPropertyOfType(
objType,
propertyType.value.toString(),
checker,
)
if (propType) {
return isNullableType(propType, ts)
}
}
const typeName = getTypeName(propertyType, tools!)
return Boolean(
(typeName === "string" &&
checker.getIndexInfoOfType(objType, ts.IndexKind.String)) ||
(typeName === "number" &&
checker.getIndexInfoOfType(objType, ts.IndexKind.Number)),
)
}
/**
* Checks whether a member expression is nullable or not regardless of it's previous node.
* Example:
* ```
* // 'bar' is nullable if 'foo' is null.
* // but this function checks regardless of 'foo' type, so returns 'true'.
* declare const foo: { bar : { baz: string } } | null
* foo?.bar;
* ```
*/
function isNullableOriginFromPrev(
node: TSESTree.MemberExpression,
): boolean {
const prevType = getNodeType(node.object)
const property = node.property
if (prevType && prevType.isUnion() && property.type === "Identifier") {
const isOwnNullable = prevType.types.some((type) => {
if (node.computed) {
const propertyType = getNodeType(node.property)
return Boolean(
propertyType && isNullablePropertyType(type, propertyType),
)
}
const propType = getTypeOfPropertyOfType(type, property.name, checker)
return propType && isNullableType(propType, ts)
})
return !isOwnNullable && isNullableType(prevType, ts)
}
return false
}
/**
* Checks whether a lhs expression is optionable or not.
*/
function isOptionableExpression(
node: TSESTree.LeftHandSideExpression,
): boolean {
const type = getNodeType(node)
if (!type) {
return false
}
const isOwnNullable =
node.type === "MemberExpression"
? !isNullableOriginFromPrev(node)
: true
return (
isAnyType(type, ts) ||
isUnknownType(type, ts) ||
(isNullableType(type, ts) && isOwnNullable)
)
}
/**
* Checks if a conditional node is necessary from the given optional chaining expression.
*/
function checkOptionalChain(
node: TSESTree.MemberExpression | TSESTree.CallExpression,
beforeOperator: TSESTree.Node,
fix: "" | ".",
): void {
// We only care if this step in the chain is optional. If just descend
// from an optional chain, then that's fine.
if (!node.optional) {
return
}
// Since typescript array index signature types don't represent the
// possibility of out-of-bounds access, if we're indexing into an array
// just skip the check, to avoid false positives
if (optionChainContainsOptionArrayIndex(node)) {
return
}
const nodeToCheck =
node.type === "CallExpression" ? node.callee : node.object
if (hasSvelteReactiveVar(nodeToCheck)) {
return
}
if (isOptionableExpression(nodeToCheck)) {
return
}
const questionDotOperator = sourceCode.getTokenAfter(beforeOperator, {
includeComments: false,
filter: (token) => token.type === "Punctuator" && token.value === "?.",
})!
context.report({
node,
loc: questionDotOperator.loc,
messageId: "neverOptionalChain",
fix(fixer) {
return fixer.replaceText(questionDotOperator, fix)
},
})
}
/**
* Checks for optional member expression
*/
function checkOptionalMemberExpression(
node: TSESTree.MemberExpression,
): void {
checkOptionalChain(node, node.object, node.computed ? "" : ".")
}
/**
* Checks for optional call expression
*/
function checkOptionalCallExpression(node: TSESTree.CallExpression): void {
checkOptionalChain(node, node.callee, "")
}
return {
SvelteReactiveStatement: () => (inSvelteReactiveStatement = true),
"SvelteReactiveStatement:exit": () => (inSvelteReactiveStatement = false),
BinaryExpression: checkIfBinaryExpressionIsNecessaryConditional,
CallExpression: checkCallExpression,
ConditionalExpression: (node: TSESTree.ConditionalExpression): void =>
checkNode(node.test),
DoWhileStatement: checkIfLoopIsNecessaryConditional,
ForStatement: checkIfLoopIsNecessaryConditional,
IfStatement: (node: TSESTree.IfStatement): void => checkNode(node.test),
LogicalExpression: checkLogicalExpressionForUnnecessaryConditionals,
WhileStatement: checkIfLoopIsNecessaryConditional,
"MemberExpression[optional = true]": checkOptionalMemberExpression,
"CallExpression[optional = true]": checkOptionalCallExpression,
}
},
})