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
6 changes: 6 additions & 0 deletions apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
{
"checkLiteralConstAssertions": false
}
],
"typescript/no-unsafe-member-access": [
"error",
{
"allowOptionalChaining": true
}
]
}
}
9 changes: 8 additions & 1 deletion apps/oxlint/fixtures/tsgolint_rule_options/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ const notAllowedSpread = { ...notAllowedFunc };
// When checkLiteralConstAssertions is true, this SHOULD error
const literalConst = 'hello' as const;

export { result, customStr, allowedSpread, notAllowedSpread, literalConst };
// Test no-unsafe-member-access with allowOptionalChaining option
declare const anyValue: any;
// This should NOT error because allowOptionalChaining is true and we use ?.
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 };
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ working directory: fixtures/tsgolint_rule_options
48 |
`----

Found 0 warnings and 2 errors.
x typescript-eslint(no-unsafe-member-access): Unsafe member access .bar on an `any` value.
,-[test.ts:58:31]
57 | // This SHOULD error because it's not using optional chaining
58 | const unsafeAccess = anyValue.bar;
: ^^^
59 |
`----

Found 0 warnings and 3 errors.
Finished in <variable>ms on 1 file using 1 threads.
----------
CLI result: LintFoundErrors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
use oxc_macros::declare_oxc_lint;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::rule::Rule;
use crate::rule::{DefaultRuleConfig, Rule};

#[derive(Debug, Default, Clone)]
pub struct NoUnsafeMemberAccess;
#[derive(Debug, Default, Clone, Deserialize)]
pub struct NoUnsafeMemberAccess(Box<NoUnsafeMemberAccessConfig>);

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Default)]
#[serde(rename_all = "camelCase", default)]
pub struct NoUnsafeMemberAccessConfig {
/// Whether to allow `?.` optional chains on `any` values.
/// When `true`, optional chaining on `any` values will not be flagged.
/// Default is `false`.
pub allow_optional_chaining: bool,
}

declare_oxc_lint!(
/// ### What it does
Expand Down Expand Up @@ -52,6 +63,17 @@ declare_oxc_lint!(
typescript,
pedantic,
pending,
config = NoUnsafeMemberAccessConfig,
);

impl Rule for NoUnsafeMemberAccess {}
impl Rule for NoUnsafeMemberAccess {
fn from_configuration(value: serde_json::Value) -> Self {
serde_json::from_value::<DefaultRuleConfig<NoUnsafeMemberAccess>>(value)
.unwrap_or_default()
.into_inner()
}

fn to_configuration(&self) -> Option<Result<serde_json::Value, serde_json::Error>> {
Some(serde_json::to_value(&*self.0))
}
}
Loading