react_auditor/rules/performance/
no_bind_in_jsx.rs1use oxc_ast::ast::Program;
2use oxc_ast_visit::Visit;
3use oxc_semantic::Semantic;
4
5use crate::rules::{Rule, RuleFinding, RuleMeta, Severity};
6
7pub struct NoBindInJsx;
8
9const RULE_META: RuleMeta = RuleMeta {
10 id: "no-bind-in-jsx",
11 default_severity: Severity::Warning,
12 category: "performance",
13 description: "Avoid `.bind()` or arrow functions in JSX props",
14};
15
16impl Rule for NoBindInJsx {
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 = BindCollector {
23 findings: Vec::new(),
24 source: source_text,
25 };
26 collector.visit_program(program);
27 collector.findings
28 }
29}
30
31struct BindCollector<'a> {
32 findings: Vec<RuleFinding>,
33 source: &'a str,
34}
35
36impl<'a> Visit<'a> for BindCollector<'a> {
37 fn visit_jsx_opening_element(&mut self, el: &oxc_ast::ast::JSXOpeningElement<'a>) {
38 for attr_item in &el.attributes {
39 if let oxc_ast::ast::JSXAttributeItem::Attribute(attr) = attr_item
40 && let Some(val) = &attr.value
41 && let oxc_ast::ast::JSXAttributeValue::ExpressionContainer(container) = val
42 {
43 let has_bind = match &container.expression {
44 oxc_ast::ast::JSXExpression::CallExpression(call) => {
45 if let Some(member) = call.callee.as_member_expression() {
46 member.static_property_name() == Some("bind")
47 } else {
48 false
49 }
50 }
51 _ => false,
52 };
53 if has_bind {
54 let start = attr.span.start as usize;
55 let line = self.source[..start].lines().count().max(1);
56 let col = start - self.source[..start].rfind('\n').map(|i| i + 1).unwrap_or(0);
57 self.findings.push(RuleFinding {
58 line,
59 column: col + 1,
60 message: "Avoid `.bind()` in JSX – use a class property or useCallback"
61 .to_string(),
62 });
63 }
64 }
65 }
66 }
67}