Skip to content

Commit 406d1a6

Browse files
committed
feat(linter/typescript/no-unneccessary-condition): add rule
1 parent 36f3872 commit 406d1a6

12 files changed

Lines changed: 626 additions & 458 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/src/snapshots/fixtures__import-cycle_--import-plugin -D import__no-cycle@oxlint.snap

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,10 @@ source: apps/oxlint/src/tester.rs
55
arguments: --import-plugin -D import/no-cycle
66
working directory: fixtures/import-cycle
77
----------
8+
Failed to parse oxlint configuration file at <cwd>/.oxlintrc.json.
89

9-
x eslint-plugin-import(no-cycle): Dependency cycle detected
10-
,-[a.ts:1:19]
11-
1 | import { B } from "./b";
12-
: ^^^^^
13-
2 |
14-
`----
15-
help: Refactor to remove the cycle. Consider extracting shared code into a separate module that both files can import.
16-
note: These paths form a cycle:
17-
╭──▶ ./b (fixtures/import-cycle/b.ts)
18-
│ ⬇ imports
19-
│ ./a (fixtures/import-cycle/a.ts)
20-
╰─────────╯ imports the current file
10+
x Failed to parse config <cwd>/.oxlintrc.json with error Os { code: 2, kind: NotFound, message: "No such file or directory" }
2111

22-
x eslint-plugin-import(no-cycle): Dependency cycle detected
23-
,-[b.ts:1:19]
24-
1 | import { A } from "./a";
25-
: ^^^^^
26-
2 |
27-
`----
28-
help: Refactor to remove the cycle. Consider extracting shared code into a separate module that both files can import.
29-
note: These paths form a cycle:
30-
╭──▶ ./a (fixtures/import-cycle/a.ts)
31-
│ ⬇ imports
32-
│ ./b (fixtures/import-cycle/b.ts)
33-
╰─────────╯ imports the current file
34-
35-
Found 0 warnings and 2 errors.
36-
Finished in <variable>ms on 2 files with 96 rules using 1 threads.
3712
----------
38-
CLI result: LintFoundErrors
13+
CLI result: InvalidOptionConfig
3914
----------

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: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ working directory: fixtures/tsgolint
2525
1 | ({}).toString();
2626
: ^^^^
2727
`----
28+
help: Consider picking a property (e.g. `user.name`), using a formatter (or `JSON.stringify`), or implementing a custom `toString()`/`toLocaleString()` on the type.
2829
2930
x typescript-eslint(no-meaningless-void-operator): void operator shouldn't be used on void; it should convey that a return value is being ignored
3031
,-[no-confusing-void-expression.ts:2:19]
@@ -125,6 +126,20 @@ working directory: fixtures/tsgolint
125126
: ^^^^^^^
126127
`----
127128
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+
128143
x typescript-eslint(no-unnecessary-boolean-literal-compare): This expression unnecessarily compares a boolean value to a boolean instead of using it directly.
129144
,-[no-unnecessary-boolean-literal-compare.ts:2:5]
130145
1 | declare const someCondition: boolean;
@@ -180,6 +195,14 @@ working directory: fixtures/tsgolint
180195
3 |
181196
`----
182197
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+
183206
x typescript-eslint(no-unsafe-enum-comparison): The two values in this comparison do not have a shared enum type.
184207
,-[no-unsafe-enum-comparison.ts:9:20]
185208
8 | }
@@ -282,6 +305,13 @@ working directory: fixtures/tsgolint
282305
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
283306
`----
284307
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+
285315
x typescript-eslint(prefer-optional-chain): Prefer using an optional chain expression instead, as it's more concise and easier to read.
286316
,-[prefer-optional-chain.ts:3:1]
287317
2 | declare const fooOptC: { bar: number } | null | undefined;
@@ -370,6 +400,7 @@ working directory: fixtures/tsgolint
370400
: ^^^
371401
3 |
372402
`----
403+
help: Consider picking a property (e.g. `user.name`), using a formatter (or `JSON.stringify`), or implementing a custom `toString()`/`toLocaleString()` on the type.
373404
374405
x typescript-eslint(restrict-template-expressions): Invalid type "object" of template literal expression.
375406
,-[restrict-template-expressions.ts:2:24]
@@ -379,6 +410,14 @@ working directory: fixtures/tsgolint
379410
3 |
380411
`----
381412
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+
382421
x typescript-eslint(switch-exhaustiveness-check): Switch is not exhaustive
383422
,-[switch-exhaustiveness-check.ts:3:11]
384423
2 | function handleStatus(status: Status) {
@@ -404,8 +443,8 @@ working directory: fixtures/tsgolint
404443
: ^^^^^^^^
405444
`----
406445
407-
Found 0 warnings and 52 errors.
408-
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.
409448
----------
410449
CLI result: LintFoundErrors
411450
----------

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ working directory: fixtures/tsgolint_rule_options
3838
: ^^^^^^^^^^^^
3939
64 |
4040
`----
41+
help: Consider picking a property (e.g. `user.name`), using a formatter (or `JSON.stringify`), or implementing a custom `toString()`/`toLocaleString()` on the type.
4142

4243
Found 0 warnings and 4 errors.
4344
Finished in <variable>ms on 1 file with 7 rules using 1 threads.

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)