Skip to content

Commit 31b5fa4

Browse files
committed
feat(linter/typescript/no-unneccessary-condition): add rule
1 parent 65b94f9 commit 31b5fa4

13 files changed

Lines changed: 650 additions & 432 deletions

apps/oxlint/fixtures/tsgolint/.oxlintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"typescript/no-mixed-enums": "error",
1818
"typescript/no-redundant-type-constituents": "error",
1919
"typescript/no-unnecessary-boolean-literal-compare": "error",
20+
"typescript/no-unnecessary-condition": "error",
2021
"typescript/no-unnecessary-template-expression": "error",
2122
"typescript/no-unnecessary-type-arguments": "error",
2223
"typescript/no-unnecessary-type-assertion": "error",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare const b1: object;
2+
declare const b2: boolean;
3+
export const t1 = b1 && b2;

apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
"allowOptionalChaining": true
4545
}
4646
],
47+
"typescript/no-unnecessary-condition": [
48+
"error",
49+
{
50+
"allowConstantLoopConditions": true,
51+
"checkTypePredicates": false,
52+
"allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing": false
53+
}
54+
],
4755
"typescript/only-throw-error": [
4856
"error",
4957
{

apps/oxlint/fixtures/tsgolint_rule_options/test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ declare const unknownValue: unknown;
6262
// This SHOULD error because checkUnknown is true
6363
const unknownStr = unknownValue.toString();
6464

65+
// Test no-unnecessary-condition with allowConstantLoopConditions option
66+
declare const alwaysTruthyObject: object;
67+
// This SHOULD error because this object is always truthy
68+
if (alwaysTruthyObject) {
69+
result += 1;
70+
}
71+
72+
// This should NOT error because allowConstantLoopConditions is true
73+
while (true) {
74+
break;
75+
}
76+
6577
// Test only-throw-error with allowRethrowing option
6678
// When allowRethrowing is false, rethrowing a caught error SHOULD error
6779
try {

apps/oxlint/src/snapshots/fixtures__tsgolint_--type-aware --silent@oxlint.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ arguments: --type-aware --silent
66
working directory: fixtures/tsgolint
77
----------
88

9-
Found 0 warnings and 52 errors.
10-
Finished in <variable>ms on 46 files with 45 rules using 1 threads.
9+
Found 0 warnings and 57 errors.
10+
Finished in <variable>ms on 47 files with 46 rules using 1 threads.
1111
----------
1212
CLI result: LintFoundErrors
1313
----------

apps/oxlint/src/snapshots/fixtures__tsgolint_--type-aware -c config-test.json@oxlint.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ working directory: fixtures/tsgolint
4040
help: Remove the debugger statement
4141
4242
Found 2 warnings and 2 errors.
43-
Finished in <variable>ms on 46 files with 1 rules using 1 threads.
43+
Finished in <variable>ms on 47 files with 1 rules using 1 threads.
4444
----------
4545
CLI result: LintFoundErrors
4646
----------

apps/oxlint/src/snapshots/fixtures__tsgolint_--type-aware test.svelte@oxlint.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ working directory: fixtures/tsgolint
1616
help: Remove the debugger statement
1717

1818
Found 0 warnings and 1 error.
19-
Finished in <variable>ms on 1 file with 45 rules using 1 threads.
19+
Finished in <variable>ms on 1 file with 46 rules using 1 threads.
2020
----------
2121
CLI result: LintFoundErrors
2222
----------

apps/oxlint/src/snapshots/fixtures__tsgolint_--type-aware@oxlint.snap

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ working directory: fixtures/tsgolint
126126
: ^^^^^^^
127127
`----
128128
129+
x typescript-eslint(no-unnecessary-condition): Unnecessary conditional, value is always truthy.
130+
,-[no-unneccessary-condition.ts:3:19]
131+
2 | declare const b2: boolean;
132+
3 | export const t1 = b1 && b2;
133+
: ^^
134+
`----
135+
136+
x typescript-eslint(strict-boolean-expressions): Unexpected object value in conditional. An object is always truthy.
137+
,-[no-unneccessary-condition.ts:3:19]
138+
2 | declare const b2: boolean;
139+
3 | export const t1 = b1 && b2;
140+
: ^^
141+
`----
142+
129143
x typescript-eslint(no-unnecessary-boolean-literal-compare): This expression unnecessarily compares a boolean value to a boolean instead of using it directly.
130144
,-[no-unnecessary-boolean-literal-compare.ts:2:5]
131145
1 | declare const someCondition: boolean;
@@ -181,6 +195,14 @@ working directory: fixtures/tsgolint
181195
3 |
182196
`----
183197
198+
x typescript-eslint(no-unnecessary-condition): Unnecessary comparison between literal values.
199+
,-[no-unsafe-enum-comparison.ts:9:20]
200+
8 | }
201+
9 | const comparison = Status.Open === Color.Red;
202+
: ^^^^^^^^^^^^^^^^^^^^^^^^^
203+
10 |
204+
`----
205+
184206
x typescript-eslint(no-unsafe-enum-comparison): The two values in this comparison do not have a shared enum type.
185207
,-[no-unsafe-enum-comparison.ts:9:20]
186208
8 | }
@@ -283,6 +305,13 @@ working directory: fixtures/tsgolint
283305
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
284306
`----
285307
308+
x typescript-eslint(no-unnecessary-condition): This condition will always return the same value since the types have no overlap.
309+
,-[prefer-nullish-coalescing.ts:2:50]
310+
1 | declare const nullableString: string | null;
311+
2 | const nullishResult = nullableString !== null && nullableString !== undefined ? nullableString : 'default';
312+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
313+
`----
314+
286315
x typescript-eslint(prefer-optional-chain): Prefer using an optional chain expression instead, as it's more concise and easier to read.
287316
,-[prefer-optional-chain.ts:3:1]
288317
2 | declare const fooOptC: { bar: number } | null | undefined;
@@ -381,6 +410,14 @@ working directory: fixtures/tsgolint
381410
3 |
382411
`----
383412
413+
x typescript-eslint(no-unnecessary-condition): Unnecessary conditional, value is always truthy.
414+
,-[strict-boolean-expressions.ts:2:5]
415+
1 | const str = 'hello';
416+
2 | if (str) {
417+
: ^^^
418+
3 | }
419+
`----
420+
384421
x typescript-eslint(switch-exhaustiveness-check): Switch is not exhaustive
385422
,-[switch-exhaustiveness-check.ts:3:11]
386423
2 | function handleStatus(status: Status) {
@@ -406,8 +443,8 @@ working directory: fixtures/tsgolint
406443
: ^^^^^^^^
407444
`----
408445
409-
Found 0 warnings and 52 errors.
410-
Finished in <variable>ms on 46 files with 45 rules using 1 threads.
446+
Found 0 warnings and 57 errors.
447+
Finished in <variable>ms on 47 files with 46 rules using 1 threads.
411448
----------
412449
CLI result: LintFoundErrors
413450
----------

apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ working directory: fixtures/tsgolint_rule_options
4040
`----
4141
help: Consider picking a property (e.g. `user.name`), using a formatter (or `JSON.stringify`), or implementing a custom `toString()`/`toLocaleString()` on the type.
4242

43-
Found 0 warnings and 4 errors.
44-
Finished in <variable>ms on 1 file with 7 rules using 1 threads.
43+
x typescript-eslint(no-unnecessary-condition): Unnecessary conditional, value is always truthy.
44+
,-[test.ts:68:5]
45+
67 | // This SHOULD error because this object is always truthy
46+
68 | if (alwaysTruthyObject) {
47+
: ^^^^^^^^^^^^^^^^^^
48+
69 | result += 1;
49+
`----
50+
51+
Found 0 warnings and 5 errors.
52+
Finished in <variable>ms on 1 file with 8 rules using 1 threads.
4553
----------
4654
CLI result: LintFoundErrors
4755
----------

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)