From 9e8ec78db5deae435f1336de90fcdd32780239b7 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Thu, 1 Jan 2026 16:12:36 +0000 Subject: [PATCH] feat(linter/only-throw-error rule): add `allowRethrowing` option for (#17554) I've implemented this on the tsgolint side in https://github.com/oxc-project/tsgolint/pull/552. This PR adds the options to the rust impl --- .../oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json | 8 ++++++++ apps/oxlint/fixtures/tsgolint_rule_options/test.ts | 8 ++++++++ ...tures__tsgolint_rule_options_--type-aware@oxlint.snap | 2 +- .../oxc_linter/src/rules/typescript/only_throw_error.rs | 9 ++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json b/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json index cb84d8e7d47e9..1840e61c469cf 100644 --- a/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json +++ b/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json @@ -43,6 +43,14 @@ { "allowOptionalChaining": true } + ], + "typescript/only-throw-error": [ + "error", + { + "allowRethrowing": false, + "allowThrowingAny": false, + "allowThrowingUnknown": true + } ] } } diff --git a/apps/oxlint/fixtures/tsgolint_rule_options/test.ts b/apps/oxlint/fixtures/tsgolint_rule_options/test.ts index db8ee86250083..5a0f994670270 100644 --- a/apps/oxlint/fixtures/tsgolint_rule_options/test.ts +++ b/apps/oxlint/fixtures/tsgolint_rule_options/test.ts @@ -62,4 +62,12 @@ declare const unknownValue: unknown; // This SHOULD error because checkUnknown is true const unknownStr = unknownValue.toString(); +// Test only-throw-error with allowRethrowing option +// When allowRethrowing is false, rethrowing a caught error SHOULD error +try { + throw new Error('test'); +} catch (e) { + throw e; // This SHOULD error because allowRethrowing is false +} + export { result, customStr, allowedSpread, notAllowedSpread, literalConst, optionalAccess, unsafeAccess, unknownStr }; diff --git a/apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap b/apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap index f9cbc1eeb1a27..8a10ebbe92e2d 100644 --- a/apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap +++ b/apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap @@ -39,7 +39,7 @@ working directory: fixtures/tsgolint_rule_options `---- Found 0 warnings and 4 errors. -Finished in ms on 1 file with 6 rules using 1 threads. +Finished in ms on 1 file with 7 rules using 1 threads. ---------- CLI result: LintFoundErrors ---------- diff --git a/crates/oxc_linter/src/rules/typescript/only_throw_error.rs b/crates/oxc_linter/src/rules/typescript/only_throw_error.rs index addab8193423a..e1cd6040ad595 100644 --- a/crates/oxc_linter/src/rules/typescript/only_throw_error.rs +++ b/crates/oxc_linter/src/rules/typescript/only_throw_error.rs @@ -16,6 +16,8 @@ pub struct OnlyThrowErrorConfig { /// An array of type or value specifiers for additional types that are allowed to be thrown. /// Use this to allow throwing custom error types. pub allow: Vec, + /// Whether to allow rethrowing caught values that are not Error objects. + pub allow_rethrowing: bool, /// Whether to allow throwing values typed as `any`. pub allow_throwing_any: bool, /// Whether to allow throwing values typed as `unknown`. @@ -24,7 +26,12 @@ pub struct OnlyThrowErrorConfig { impl Default for OnlyThrowErrorConfig { fn default() -> Self { - Self { allow: Vec::new(), allow_throwing_any: true, allow_throwing_unknown: true } + Self { + allow: Vec::new(), + allow_rethrowing: true, + allow_throwing_any: true, + allow_throwing_unknown: true, + } } }