Skip to content

Commit e3be19c

Browse files
committed
fix(linter): add considerDefaultExhaustiveForUnions option to switch-exhaustiveness-check
Fixes #16182
1 parent 74cf572 commit e3be19c

File tree

6 files changed

+66
-3
lines changed

6 files changed

+66
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"categories": {
3+
"correctness": "off"
4+
},
5+
"rules": {
6+
"typescript/switch-exhaustiveness-check": [
7+
"error",
8+
{
9+
"considerDefaultExhaustiveForUnions": true
10+
}
11+
]
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type Day = 'Monday' | 'Tuesday';
2+
3+
declare const day: Day;
4+
let result = 0;
5+
6+
// This should NOT error when considerDefaultExhaustiveForUnions is true
7+
// because the default case makes it exhaustive
8+
switch (day) {
9+
case 'Monday':
10+
result = 1;
11+
break;
12+
default:
13+
result = 3;
14+
break;
15+
}
16+
17+
export { result };
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"target": "ES2020",
5+
"module": "ESNext",
6+
"moduleResolution": "node"
7+
},
8+
"include": ["*.ts"]
9+
}

apps/oxlint/src/lint.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,4 +1412,15 @@ mod test {
14121412
.with_cwd("fixtures/tsgolint_tsconfig_extends_config_err".into())
14131413
.test_and_snapshot(args);
14141414
}
1415+
1416+
#[test]
1417+
#[cfg(not(target_endian = "big"))]
1418+
fn test_tsgolint_rule_options() {
1419+
// Test that rule options are correctly passed to tsgolint
1420+
// See: https://github.com/oxc-project/oxc/issues/16182
1421+
let args = &["--type-aware"];
1422+
Tester::new()
1423+
.with_cwd("fixtures/tsgolint_rule_options".into())
1424+
.test_and_snapshot(args);
1425+
}
14151426
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
arguments: --type-aware
6+
working directory: fixtures/tsgolint_rule_options
7+
----------
8+
Found 0 warnings and 0 errors.
9+
Finished in <variable>ms on 1 file using 1 threads.
10+
----------
11+
CLI result: LintSucceeded
12+
----------

crates/oxc_linter/src/rules/typescript/switch_exhaustiveness_check.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ pub struct SwitchExhaustivenessCheckConfig {
1313
/// Whether to allow default cases on switches that are not exhaustive.
1414
/// When false, requires exhaustive switch statements without default cases.
1515
pub allow_default_case_for_exhaustive_switch: bool,
16-
/// Whether to allow this rule to run without `strictNullChecks` enabled.
17-
/// This is not recommended as the rule may produce incorrect results.
18-
pub allow_rule_to_run_without_strict_null_checks_i_know_what_i_am_doing: bool,
16+
/// Whether to consider `default` cases exhaustive for union types.
17+
/// When true, a switch statement with a `default` case is considered exhaustive
18+
/// even if not all union members are handled explicitly.
19+
pub consider_default_exhaustive_for_unions: bool,
1920
/// Regular expression pattern that when matched in a default case comment,
2021
/// will suppress the exhaustiveness check.
2122
/// Example: `"@skip-exhaustive-check"` to allow `default: // @skip-exhaustive-check`

0 commit comments

Comments
 (0)