Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"typescript/no-base-to-string": [
"error",
{
"checkUnknown": true,
"ignoredTypeNames": ["Error", "RegExp", "URL", "URLSearchParams", "CustomStringifiable"]
}
],
Expand Down
7 changes: 6 additions & 1 deletion apps/oxlint/fixtures/tsgolint_rule_options/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Original file line number Diff line number Diff line change
Expand Up @@ -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 <variable>ms on 1 file using 1 threads.
----------
CLI result: LintFoundErrors
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/typescript/no_base_to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub struct NoBaseToString(Box<NoBaseToStringConfig>);
#[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.
Expand All @@ -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(),
Expand Down
Loading