Skip to main content

react_auditor/rules/react/
no_direct_mutation.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 NoDirectMutation;
8
9const RULE_META: RuleMeta = RuleMeta {
10    id: "no-direct-mutation",
11    default_severity: Severity::Warning,
12    category: "react",
13    description: "Avoid direct mutation of state or props — use setState or dispatch",
14};
15
16impl Rule for NoDirectMutation {
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 = DirectMutationCollector {
23            findings: Vec::new(),
24            source: source_text,
25        };
26        collector.visit_program(program);
27        collector.findings
28    }
29}
30
31struct DirectMutationCollector<'a> {
32    findings: Vec<RuleFinding>,
33    source: &'a str,
34}
35
36impl<'a> DirectMutationCollector<'a> {
37    fn add_finding(&mut self, start: usize, msg: String) {
38        let line = self.source[..start].lines().count().max(1);
39        let col = start - self.source[..start].rfind('\n').map(|i| i + 1).unwrap_or(0);
40        self.findings.push(RuleFinding {
41            line,
42            column: col + 1,
43            message: msg,
44        });
45    }
46}
47
48impl<'a> Visit<'a> for DirectMutationCollector<'a> {
49    fn visit_assignment_expression(&mut self, expr: &oxc_ast::ast::AssignmentExpression<'a>) {
50        if let oxc_ast::ast::AssignmentTarget::AssignmentTargetIdentifier(ident) = &expr.left {
51            let name = ident.name.as_str();
52            if name == "props" || name == "state" {
53                self.add_finding(
54                    expr.span.start as usize,
55                    format!(
56                        "Direct mutation of `{name}` — use setState or immutable patterns instead"
57                    ),
58                );
59            }
60        }
61        if let oxc_ast::ast::AssignmentTarget::ComputedMemberExpression(member) = &expr.left
62            && let oxc_ast::ast::Expression::Identifier(ident) = &member.object
63        {
64            let name = ident.name.as_str();
65            if name == "props" || name == "state" {
66                self.add_finding(
67                    expr.span.start as usize,
68                    format!(
69                        "Direct mutation of `{name}` — use setState or immutable patterns instead"
70                    ),
71                );
72            }
73        }
74        if let oxc_ast::ast::AssignmentTarget::StaticMemberExpression(member) = &expr.left
75            && let oxc_ast::ast::Expression::StaticMemberExpression(inner) = &member.object
76            && (inner.property.name.as_str() == "state" || inner.property.name.as_str() == "props")
77            && let oxc_ast::ast::Expression::ThisExpression(_) = &inner.object
78        {
79            self.add_finding(
80                expr.span.start as usize,
81                format!(
82                    "Direct mutation of `this.{}.{}` — use setState or immutable patterns",
83                    inner.property.name, member.property.name
84                ),
85            );
86        }
87    }
88}