Skip to content

Commit 2e531d9

Browse files
committed
fix: rule
1 parent bcd9644 commit 2e531d9

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

src/rules/@typescript-eslint/no-unnecessary-condition.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ export default createRule("@typescript-eslint/no-unnecessary-condition", {
307307
// Since typescript array index signature types don't represent the
308308
// possibility of out-of-bounds access, if we're indexing into an array
309309
// just skip the check, to avoid false positives
310+
if (isArrayIndexExpression(node)) {
311+
return
312+
}
313+
314+
// When checking logical expressions, only check the right side
315+
// as the left side has been checked by checkLogicalExpressionForUnnecessaryConditionals
310316
//
311317
// Unless the node is nullish coalescing, as it's common to use patterns like `nullBool ?? true` to to strict
312318
// boolean checks if we inspect the right here, it'll usually be a constant condition on purpose.

src/utils/ts-utils/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ export function getTypeScript(context: RuleContext): TypeScript | undefined {
6868
try {
6969
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
7070
cacheTypeScript ??= require("typescript")
71-
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- ignore
72-
} catch (e: any) {
73-
if (e.code !== "MODULE_NOT_FOUND") {
74-
throw e
75-
}
71+
} catch {
72+
// ignore
7673
}
7774
return cacheTypeScript
7875
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Unnecessary conditional, value is always falsy.
2+
line: 4
3+
column: 11
4+
suggestions: null
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
export let foo: number | null = null
3+
/* ✗ BAD */
4+
let b = foo || 42
5+
/* ✓ GOOD */
6+
$: a = foo || 42
7+
</script>
8+
9+
<!-- ✓ GOOD -->
10+
{foo || 42}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
export let foo: number | null = null
3+
/* ✗ BAD */
4+
let b = foo || 42
5+
/* ✓ GOOD */
6+
$: a = foo || 42
7+
</script>
8+
9+
<!-- ✓ GOOD -->
10+
{foo || 42}

0 commit comments

Comments
 (0)