diff --git a/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json b/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json index dd3f1f0b04f53..cb84d8e7d47e9 100644 --- a/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json +++ b/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json @@ -12,6 +12,7 @@ "typescript/no-base-to-string": [ "error", { + "checkUnknown": true, "ignoredTypeNames": ["Error", "RegExp", "URL", "URLSearchParams", "CustomStringifiable"] } ], diff --git a/apps/oxlint/fixtures/tsgolint_rule_options/test.ts b/apps/oxlint/fixtures/tsgolint_rule_options/test.ts index 2efd9c4bdcd28..db8ee86250083 100644 --- a/apps/oxlint/fixtures/tsgolint_rule_options/test.ts +++ b/apps/oxlint/fixtures/tsgolint_rule_options/test.ts @@ -57,4 +57,9 @@ const optionalAccess = anyValue?.foo; // This SHOULD error because it's not using optional chaining const unsafeAccess = anyValue.bar; -export { result, customStr, allowedSpread, notAllowedSpread, literalConst, optionalAccess, unsafeAccess }; +// Test no-base-to-string with checkUnknown option +declare const unknownValue: unknown; +// This SHOULD error because checkUnknown is true +const unknownStr = unknownValue.toString(); + +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 795556a3dc830..2a5717793b952 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 @@ -30,7 +30,15 @@ working directory: fixtures/tsgolint_rule_options 59 | `---- -Found 0 warnings and 3 errors. + x typescript-eslint(no-base-to-string): 'unknownValue' may use Object's default stringification format ('[object Object]') when stringified. + ,-[test.ts:63:20] + 62 | // This SHOULD error because checkUnknown is true + 63 | const unknownStr = unknownValue.toString(); + : ^^^^^^^^^^^^ + 64 | + `---- + +Found 0 warnings and 4 errors. Finished in ms on 1 file using 1 threads. ---------- CLI result: LintFoundErrors diff --git a/crates/oxc_linter/src/rules/typescript/no_base_to_string.rs b/crates/oxc_linter/src/rules/typescript/no_base_to_string.rs index 29655d72eee18..650358c245137 100644 --- a/crates/oxc_linter/src/rules/typescript/no_base_to_string.rs +++ b/crates/oxc_linter/src/rules/typescript/no_base_to_string.rs @@ -10,6 +10,11 @@ pub struct NoBaseToString(Box); #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "camelCase", default)] pub struct NoBaseToStringConfig { + /// Whether to also check values of type `unknown`. + /// When `true`, calling toString on `unknown` values will be flagged. + /// Default is `false`. + pub check_unknown: bool, + /// A list of type names to ignore when checking for unsafe toString usage. /// These types are considered safe to call toString on even if they don't /// provide a custom implementation. @@ -19,6 +24,7 @@ pub struct NoBaseToStringConfig { impl Default for NoBaseToStringConfig { fn default() -> Self { Self { + check_unknown: false, ignored_type_names: vec![ "Error".to_string(), "RegExp".to_string(),