diff --git a/crates/oxc_linter/src/generated/rule_runner_impls.rs b/crates/oxc_linter/src/generated/rule_runner_impls.rs index 240208e4b0..7487118a6a 100644 --- a/crates/oxc_linter/src/generated/rule_runner_impls.rs +++ b/crates/oxc_linter/src/generated/rule_runner_impls.rs @@ -2928,12 +2928,8 @@ impl RuleRunner for crate::rules::react::no_set_state::NoSetState { } impl RuleRunner for crate::rules::react::no_string_refs::NoStringRefs { - const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[ - AstType::ComputedMemberExpression, - AstType::JSXAttribute, - AstType::PrivateFieldExpression, - AstType::StaticMemberExpression, - ])); + const NODE_TYPES: Option<&AstTypesBitset> = + Some(&AstTypesBitset::from_types(&[AstType::JSXAttribute, AstType::ThisExpression])); const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run; } diff --git a/crates/oxc_linter/src/rules/react/no_string_refs.rs b/crates/oxc_linter/src/rules/react/no_string_refs.rs index 2e1e97f290..291344551d 100644 --- a/crates/oxc_linter/src/rules/react/no_string_refs.rs +++ b/crates/oxc_linter/src/rules/react/no_string_refs.rs @@ -1,8 +1,7 @@ use oxc_ast::{ AstKind, ast::{ - Expression, JSXAttribute, JSXAttributeName, JSXAttributeValue, JSXExpression, - JSXExpressionContainer, + JSXAttribute, JSXAttributeName, JSXAttributeValue, JSXExpression, JSXExpressionContainer, }, }; use oxc_diagnostics::OxcDiagnostic; @@ -131,11 +130,12 @@ impl Rule for NoStringRefs { { ctx.diagnostic(string_in_ref_deprecated(attr.span)); } - member_expr if member_expr.is_member_expression_kind() => { - let Some(member_expr) = member_expr.as_member_expression_kind() else { + AstKind::ThisExpression(this) => { + let node = ctx.nodes().parent_node(node.id()); + let Some(member_expr) = node.kind().as_member_expression_kind() else { return; }; - if matches!(member_expr.object(), Expression::ThisExpression(_)) + if member_expr.object().span() == this.span && member_expr.static_property_name().is_some_and(|name| name == "refs") && get_parent_component(node, ctx).is_some() { @@ -156,6 +156,8 @@ fn test() { use crate::tester::Tester; let pass = vec![ + ("class C extends React.Component { method() { return other[this]; } }", None), + ("class C extends React.Component { method() { return (this).refs; } }", None), ( " var Hello = function() { @@ -226,6 +228,8 @@ fn test() { ]; let fail = vec![ + ("class C extends React.Component { method() { return this['refs']; } }", None), + ("class C extends React.Component { method() { return this?.refs; } }", None), ( " var Hello = createReactClass({ diff --git a/crates/oxc_linter/src/snapshots/react_no_string_refs.snap b/crates/oxc_linter/src/snapshots/react_no_string_refs.snap index 516b7668a9..382bc7715f 100644 --- a/crates/oxc_linter/src/snapshots/react_no_string_refs.snap +++ b/crates/oxc_linter/src/snapshots/react_no_string_refs.snap @@ -2,6 +2,20 @@ source: crates/oxc_linter/src/tester.rs --- + ⚠ react(no-string-refs): Using this.refs is deprecated. + ╭─[no_string_refs.tsx:1:53] + 1 │ class C extends React.Component { method() { return this['refs']; } } + · ──────────── + ╰──── + help: Using this.xxx instead of this.refs.xxx + + ⚠ react(no-string-refs): Using this.refs is deprecated. + ╭─[no_string_refs.tsx:1:53] + 1 │ class C extends React.Component { method() { return this?.refs; } } + · ────────── + ╰──── + help: Using this.xxx instead of this.refs.xxx + ⚠ react(no-string-refs): Using this.refs is deprecated. ╭─[no_string_refs.tsx:4:35] 3 │ componentDidMount: function() { diff --git a/tasks/track_linter_timings/linter_timings.snap b/tasks/track_linter_timings/linter_timings.snap index 98cd744e2c..6c19b153c7 100644 --- a/tasks/track_linter_timings/linter_timings.snap +++ b/tasks/track_linter_timings/linter_timings.snap @@ -428,7 +428,7 @@ react/no-react-children | 62606 react/no-redundant-should-component-update | 300 react/no-render-return-value | 6567 react/no-set-state | 6567 -react/no-string-refs | 15882 +react/no-string-refs | 5112 react/no-this-in-sfc | 4308 react/no-unescaped-entities | 672 react/no-unknown-property | 452