-
-
Notifications
You must be signed in to change notification settings - Fork 726
feat(linter): add react/no-redundant-should-component-update rule #16147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| use oxc_ast::{ | ||
| AstKind, | ||
| ast::{Class, Expression}, | ||
| }; | ||
| use oxc_diagnostics::OxcDiagnostic; | ||
| use oxc_macros::declare_oxc_lint; | ||
| use oxc_span::Span; | ||
|
|
||
| use crate::{AstNode, context::LintContext, rule::Rule}; | ||
|
|
||
| fn no_redundant_should_component_update_diagnostic( | ||
| span: Span, | ||
| component_name: &str, | ||
| ) -> OxcDiagnostic { | ||
| let msg = format!( | ||
| "{component_name} does not need shouldComponentUpdate when extending React.PureComponent." | ||
| ); | ||
| OxcDiagnostic::warn(msg).with_label(span) | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Clone)] | ||
| pub struct NoRedundantShouldComponentUpdate; | ||
|
|
||
| declare_oxc_lint!( | ||
| /// ### What it does | ||
| /// | ||
| /// Disallow usage of shouldComponentUpdate when extending React.PureComponent | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// React.PureComponent automatically implements shouldComponentUpdate with a shallow prop and state comparison. | ||
| /// Defining shouldComponentUpdate in a class that extends PureComponent is redundant and defeats the purpose | ||
| /// of using PureComponent. If you need custom comparison logic, extend React.Component instead. | ||
| /// | ||
| /// ### Examples | ||
| /// | ||
| /// Examples of **incorrect** code for this rule: | ||
| /// ```jsx | ||
| /// class Foo extends React.PureComponent { | ||
| /// shouldComponentUpdate() { | ||
| /// // do check | ||
| /// } | ||
| /// | ||
| /// render() { | ||
| /// return <div>Radical!</div> | ||
| /// } | ||
| ///} | ||
| /// | ||
| ///function Bar() { | ||
| /// return class Baz extends React.PureComponent { | ||
| /// shouldComponentUpdate() { | ||
| /// // do check | ||
| /// } | ||
| /// | ||
| /// render() { | ||
| /// return <div>Groovy!</div> | ||
| /// } | ||
| /// } | ||
| ///} | ||
| /// ``` | ||
| /// | ||
| /// Examples of **correct** code for this rule: | ||
| /// ```jsx | ||
| /// class Foo extends React.Component { | ||
| /// shouldComponentUpdate() { | ||
| /// // do check | ||
| /// } | ||
| /// | ||
| /// render() { | ||
| /// return <div>Radical!</div> | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// function Bar() { | ||
| /// return class Baz extends React.Component { | ||
| /// shouldComponentUpdate() { | ||
| /// // do check | ||
| /// } | ||
| /// | ||
| /// render() { | ||
| /// return <div>Groovy!</div> | ||
| /// } | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// class Qux extends React.PureComponent { | ||
| /// render() { | ||
| /// return <div>Tubular!</div> | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| NoRedundantShouldComponentUpdate, | ||
| react, | ||
| style, | ||
| ); | ||
|
|
||
| impl Rule for NoRedundantShouldComponentUpdate { | ||
| fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
|
Comment on lines
+97
to
+98
|
||
| let Some(class_node) = is_react_pure_component(node) else { | ||
| return; | ||
| }; | ||
|
|
||
| if has_should_component_update(class_node) { | ||
| let component_name = class_node | ||
| .name() | ||
| .or_else(|| { | ||
| // e.g. var Foo = class extends PureComponent | ||
| let parent = ctx.nodes().parent_node(node.id()); | ||
| if let AstKind::VariableDeclarator(declarator) = parent.kind() { | ||
| declarator.id.get_identifier_name() | ||
| } else { | ||
| None | ||
| } | ||
| }) | ||
| .map_or("", |name| name.as_str()); | ||
|
|
||
| ctx.diagnostic(no_redundant_should_component_update_diagnostic( | ||
| class_node.span, | ||
| component_name, | ||
| )); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn has_should_component_update(class: &Class<'_>) -> bool { | ||
| class | ||
| .body | ||
| .body | ||
| .iter() | ||
| .any(|prop| prop.static_name().is_some_and(|name| name == "shouldComponentUpdate")) | ||
| } | ||
|
|
||
| /// Checks if class is React.PureComponent and returns this class if true | ||
| fn is_react_pure_component<'a>(node: &'a AstNode) -> Option<&'a Class<'a>> { | ||
| let AstKind::Class(class_expr) = node.kind() else { | ||
| return None; | ||
| }; | ||
| if let Some(super_class) = &class_expr.super_class { | ||
| if let Some(member_expr) = super_class.as_member_expression() | ||
| && let Expression::Identifier(ident) = member_expr.object() | ||
| { | ||
| let is_pure = ident.name == "React" | ||
| && member_expr.static_property_name().is_some_and(|name| name == "PureComponent"); | ||
|
|
||
| return if is_pure { Some(class_expr) } else { None }; | ||
| } | ||
|
|
||
| if let Some(ident_reference) = super_class.get_identifier_reference() { | ||
| let is_pure = ident_reference.name == "PureComponent"; | ||
| return if is_pure { Some(class_expr) } else { None }; | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| #[test] | ||
| fn test() { | ||
| use crate::tester::Tester; | ||
|
|
||
| let pass = vec![ | ||
| " | ||
| class Foo extends React.Component { | ||
| shouldComponentUpdate() { | ||
| return true; | ||
| } | ||
| } | ||
| ", | ||
| " | ||
| class Foo extends React.Component { | ||
| shouldComponentUpdate = () => { | ||
| return true; | ||
| } | ||
| } | ||
| ", | ||
| " | ||
| function Foo() { | ||
| return class Bar extends React.Component { | ||
| shouldComponentUpdate() { | ||
| return true; | ||
| } | ||
| }; | ||
| } | ||
| ", | ||
| ]; | ||
|
|
||
| let fail = vec![ | ||
| " | ||
| class Foo extends React.PureComponent { | ||
| shouldComponentUpdate() { | ||
| return true; | ||
| } | ||
| } | ||
| ", | ||
| " | ||
| class Foo extends PureComponent { | ||
| shouldComponentUpdate() { | ||
| return true; | ||
| } | ||
| } | ||
| ", | ||
| " | ||
| class Foo extends React.PureComponent { | ||
| shouldComponentUpdate = () => { | ||
| return true; | ||
| } | ||
| } | ||
| ", | ||
| " | ||
| function Foo() { | ||
| return class Bar extends React.PureComponent { | ||
| shouldComponentUpdate() { | ||
| return true; | ||
| } | ||
| }; | ||
| } | ||
| ", | ||
| " | ||
| function Foo() { | ||
| return class Bar extends PureComponent { | ||
| shouldComponentUpdate() { | ||
| return true; | ||
| } | ||
| }; | ||
| } | ||
| ", | ||
| " | ||
| var Foo = class extends PureComponent { | ||
| shouldComponentUpdate() { | ||
| return true; | ||
| } | ||
| } | ||
| ", | ||
| ]; | ||
|
|
||
| Tester::new( | ||
| NoRedundantShouldComponentUpdate::NAME, | ||
| NoRedundantShouldComponentUpdate::PLUGIN, | ||
| pass, | ||
| fail, | ||
| ) | ||
| .test_and_snapshot(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| --- | ||
| source: crates/oxc_linter/src/tester.rs | ||
| --- | ||
| ⚠ eslint-plugin-react(no-redundant-should-component-update): Foo does not need shouldComponentUpdate when extending React.PureComponent. | ||
| ╭─[no_redundant_should_component_update.tsx:2:12] | ||
| 1 │ | ||
| 2 │ ╭─▶ class Foo extends React.PureComponent { | ||
| 3 │ │ shouldComponentUpdate() { | ||
| 4 │ │ return true; | ||
| 5 │ │ } | ||
| 6 │ ╰─▶ } | ||
| 7 │ | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-react(no-redundant-should-component-update): Foo does not need shouldComponentUpdate when extending React.PureComponent. | ||
| ╭─[no_redundant_should_component_update.tsx:2:12] | ||
| 1 │ | ||
| 2 │ ╭─▶ class Foo extends PureComponent { | ||
| 3 │ │ shouldComponentUpdate() { | ||
| 4 │ │ return true; | ||
| 5 │ │ } | ||
| 6 │ ╰─▶ } | ||
| 7 │ | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-react(no-redundant-should-component-update): Foo does not need shouldComponentUpdate when extending React.PureComponent. | ||
| ╭─[no_redundant_should_component_update.tsx:2:12] | ||
| 1 │ | ||
| 2 │ ╭─▶ class Foo extends React.PureComponent { | ||
| 3 │ │ shouldComponentUpdate = () => { | ||
| 4 │ │ return true; | ||
| 5 │ │ } | ||
| 6 │ ╰─▶ } | ||
| 7 │ | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-react(no-redundant-should-component-update): Bar does not need shouldComponentUpdate when extending React.PureComponent. | ||
| ╭─[no_redundant_should_component_update.tsx:3:21] | ||
| 2 │ function Foo() { | ||
| 3 │ ╭─▶ return class Bar extends React.PureComponent { | ||
| 4 │ │ shouldComponentUpdate() { | ||
| 5 │ │ return true; | ||
| 6 │ │ } | ||
| 7 │ ╰─▶ }; | ||
| 8 │ } | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-react(no-redundant-should-component-update): Bar does not need shouldComponentUpdate when extending React.PureComponent. | ||
| ╭─[no_redundant_should_component_update.tsx:3:21] | ||
| 2 │ function Foo() { | ||
| 3 │ ╭─▶ return class Bar extends PureComponent { | ||
| 4 │ │ shouldComponentUpdate() { | ||
| 5 │ │ return true; | ||
| 6 │ │ } | ||
| 7 │ ╰─▶ }; | ||
| 8 │ } | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-react(no-redundant-should-component-update): Foo does not need shouldComponentUpdate when extending React.PureComponent. | ||
| ╭─[no_redundant_should_component_update.tsx:2:22] | ||
| 1 │ | ||
| 2 │ ╭─▶ var Foo = class extends PureComponent { | ||
| 3 │ │ shouldComponentUpdate() { | ||
| 4 │ │ return true; | ||
| 5 │ │ } | ||
| 6 │ ╰─▶ } | ||
| 7 │ | ||
| ╰──── |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import is missing
ContextHostwhich is needed for theshould_runmethod implementation. Update the import to:\n\nrust\nuse crate::{AstNode, context::{ContextHost, LintContext}, rule::Rule};\n