Skip to main content

react_auditor/rules/react/
no_forward_ref.rs

1use oxc_ast::ast::Program;
2use oxc_ast_visit::Visit;
3use oxc_semantic::Semantic;
4
5use crate::rules::{Rule, RuleFinding, RuleMeta, Severity};
6
7pub struct NoForwardRef;
8
9const RULE_META: RuleMeta = RuleMeta {
10    id: "no-forward-ref",
11    default_severity: Severity::Warning,
12    category: "react",
13    description: "forwardRef is deprecated in React 19; use ref as a prop instead",
14};
15
16impl Rule for NoForwardRef {
17    fn meta(&self) -> &RuleMeta {
18        &RULE_META
19    }
20
21    fn run(&self, program: &Program, _semantic: &Semantic, source_text: &str) -> Vec<RuleFinding> {
22        let mut collector = ForwardRefCollector {
23            findings: Vec::new(),
24            source: source_text,
25            imports_react: false,
26            has_forward_ref_import: false,
27        };
28        collector.visit_program(program);
29        collector.findings
30    }
31}
32
33struct ForwardRefCollector<'a> {
34    findings: Vec<RuleFinding>,
35    source: &'a str,
36    imports_react: bool,
37    has_forward_ref_import: bool,
38}
39
40impl<'a> Visit<'a> for ForwardRefCollector<'a> {
41    fn visit_import_declaration(&mut self, decl: &oxc_ast::ast::ImportDeclaration<'a>) {
42        if decl.source.value.as_str() == "react" {
43            self.imports_react = true;
44            if let Some(specifiers) = &decl.specifiers {
45                for spec in specifiers.iter() {
46                    if let oxc_ast::ast::ImportDeclarationSpecifier::ImportSpecifier(s) = spec
47                        && s.imported.name().as_str() == "forwardRef"
48                    {
49                        self.has_forward_ref_import = true;
50                    }
51                }
52            }
53        }
54    }
55
56    fn visit_call_expression(&mut self, call: &oxc_ast::ast::CallExpression<'a>) {
57        let is_forward_ref = match &call.callee {
58            oxc_ast::ast::Expression::Identifier(ident) => {
59                self.has_forward_ref_import && ident.name.as_str() == "forwardRef"
60            }
61            oxc_ast::ast::Expression::StaticMemberExpression(member) => {
62                let is_react = matches!(
63                    &member.object,
64                    oxc_ast::ast::Expression::Identifier(id) if id.name.as_str() == "React"
65                );
66                self.imports_react && is_react && member.property.name.as_str() == "forwardRef"
67            }
68            _ => false,
69        };
70        if is_forward_ref {
71            let start = call.span.start as usize;
72            let line = self.source[..start].lines().count().max(1);
73            let col = start - self.source[..start].rfind('\n').map(|i| i + 1).unwrap_or(0);
74            self.findings.push(RuleFinding {
75                line,
76                column: col + 1,
77                message: "forwardRef is deprecated in React 19; pass ref as a regular prop instead"
78                    .to_string(),
79            });
80        }
81    }
82}