From 20a34b529488bb089426038e9c0cb5315a93394f Mon Sep 17 00:00:00 2001 From: Hulk <33158585+byteHulk@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:45:32 +0800 Subject: [PATCH] feat: introduce --react-perf-plugin CLI flag, update rules to correctness (#2119) https://github.com/oxc-project/oxc/issues/2041#issuecomment-1903316240 Closes #2041 --- crates/oxc_cli/src/command.rs | 4 ++++ crates/oxc_cli/src/lint/mod.rs | 3 ++- crates/oxc_linter/src/options.rs | 11 +++++++++++ .../oxc_linter/src/rules/react_perf/no_jsx_as_prop.rs | 4 ++-- .../src/rules/react_perf/no_new_array_as_prop.rs | 6 ++++-- .../src/rules/react_perf/no_new_function_as_props.rs | 6 ++++-- .../src/rules/react_perf/no_new_object_as_prop.rs | 6 ++++-- crates/oxc_linter/src/tester.rs | 10 +++++++++- npm/oxlint/README.md | 3 +++ 9 files changed, 43 insertions(+), 10 deletions(-) diff --git a/crates/oxc_cli/src/command.rs b/crates/oxc_cli/src/command.rs index 3ca052cdfc0da..7f7865429078a 100644 --- a/crates/oxc_cli/src/command.rs +++ b/crates/oxc_cli/src/command.rs @@ -93,6 +93,10 @@ pub struct EnablePlugins { /// Enable the Next.js plugin and detect Next.js problems #[bpaf(switch, hide_usage)] pub nextjs_plugin: bool, + + /// Enable the React performance plugin and detect rendering performance problems + #[bpaf(switch, hide_usage)] + pub react_perf_plugin: bool, } #[derive(Debug, Clone, Bpaf)] diff --git a/crates/oxc_cli/src/lint/mod.rs b/crates/oxc_cli/src/lint/mod.rs index dceaab58d7cd2..2f9a8319ec730 100644 --- a/crates/oxc_cli/src/lint/mod.rs +++ b/crates/oxc_cli/src/lint/mod.rs @@ -100,7 +100,8 @@ impl Runner for LintRunner { .with_import_plugin(enable_plugins.import_plugin) .with_jest_plugin(enable_plugins.jest_plugin) .with_jsx_a11y_plugin(enable_plugins.jsx_a11y_plugin) - .with_nextjs_plugin(enable_plugins.nextjs_plugin); + .with_nextjs_plugin(enable_plugins.nextjs_plugin) + .with_react_perf_plugin(enable_plugins.react_perf_plugin); let linter = match Linter::from_options(lint_options) { Ok(lint_service) => lint_service, diff --git a/crates/oxc_linter/src/options.rs b/crates/oxc_linter/src/options.rs index 921bc4ac653b6..ab0a9320cf75c 100644 --- a/crates/oxc_linter/src/options.rs +++ b/crates/oxc_linter/src/options.rs @@ -27,6 +27,7 @@ pub struct LintOptions { pub jest_plugin: bool, pub jsx_a11y_plugin: bool, pub nextjs_plugin: bool, + pub react_perf_plugin: bool, } impl Default for LintOptions { @@ -40,6 +41,7 @@ impl Default for LintOptions { jest_plugin: false, jsx_a11y_plugin: false, nextjs_plugin: false, + react_perf_plugin: false, } } } @@ -94,6 +96,12 @@ impl LintOptions { self.nextjs_plugin = yes; self } + + #[must_use] + pub fn with_react_perf_plugin(mut self, yes: bool) -> Self { + self.react_perf_plugin = yes; + self + } } #[derive(Debug, Clone, Copy, Eq, PartialEq)] @@ -154,6 +162,7 @@ impl TryFrom<&Number> for AllowWarnDeny { const JEST_PLUGIN_NAME: &str = "jest"; const JSX_A11Y_PLUGIN_NAME: &str = "jsx_a11y"; const NEXTJS_PLUGIN_NAME: &str = "nextjs"; +const REACT_PERF_PLUGIN_NAME: &str = "react_perf"; impl LintOptions { /// # Errors @@ -209,6 +218,7 @@ impl LintOptions { let mut rules = rules.into_iter().collect::>(); // for stable diagnostics output ordering rules.sort_unstable_by_key(RuleEnum::name); + Ok((rules, config.map(ESLintConfig::settings).unwrap_or_default())) } @@ -225,6 +235,7 @@ impl LintOptions { may_exclude_plugin_rules(self.jest_plugin, JEST_PLUGIN_NAME); may_exclude_plugin_rules(self.jsx_a11y_plugin, JSX_A11Y_PLUGIN_NAME); may_exclude_plugin_rules(self.nextjs_plugin, NEXTJS_PLUGIN_NAME); + may_exclude_plugin_rules(self.react_perf_plugin, REACT_PERF_PLUGIN_NAME); rules } diff --git a/crates/oxc_linter/src/rules/react_perf/no_jsx_as_prop.rs b/crates/oxc_linter/src/rules/react_perf/no_jsx_as_prop.rs index 2fa131d5aca0f..8eacf4857aca3 100644 --- a/crates/oxc_linter/src/rules/react_perf/no_jsx_as_prop.rs +++ b/crates/oxc_linter/src/rules/react_perf/no_jsx_as_prop.rs @@ -37,7 +37,7 @@ declare_oxc_lint!( /// /// ``` NoJsxAsProp, - restriction + correctness ); impl Rule for NoJsxAsProp { @@ -91,5 +91,5 @@ fn test() { r")} />", ]; - Tester::new(NoJsxAsProp::NAME, pass, fail).test_and_snapshot(); + Tester::new(NoJsxAsProp::NAME, pass, fail).with_react_perf_plugin(true).test_and_snapshot(); } diff --git a/crates/oxc_linter/src/rules/react_perf/no_new_array_as_prop.rs b/crates/oxc_linter/src/rules/react_perf/no_new_array_as_prop.rs index bc45773d8f14d..eeb4b62d7a3ef 100644 --- a/crates/oxc_linter/src/rules/react_perf/no_new_array_as_prop.rs +++ b/crates/oxc_linter/src/rules/react_perf/no_new_array_as_prop.rs @@ -43,7 +43,7 @@ declare_oxc_lint!( /// /// ``` NoNewArrayAsProp, - restriction + correctness ); impl Rule for NoNewArrayAsProp { @@ -113,5 +113,7 @@ fn test() { r"", ]; - Tester::new(NoNewArrayAsProp::NAME, pass, fail).test_and_snapshot(); + Tester::new(NoNewArrayAsProp::NAME, pass, fail) + .with_react_perf_plugin(true) + .test_and_snapshot(); } diff --git a/crates/oxc_linter/src/rules/react_perf/no_new_function_as_props.rs b/crates/oxc_linter/src/rules/react_perf/no_new_function_as_props.rs index bf04a31982318..81ca51ab3f5fd 100644 --- a/crates/oxc_linter/src/rules/react_perf/no_new_function_as_props.rs +++ b/crates/oxc_linter/src/rules/react_perf/no_new_function_as_props.rs @@ -41,7 +41,7 @@ declare_oxc_lint!( /// /// ``` NoNewFunctionAsProps, - restriction + correctness ); impl Rule for NoNewFunctionAsProps { @@ -139,5 +139,7 @@ fn test() { r"", ]; - Tester::new(NoNewFunctionAsProps::NAME, pass, fail).test_and_snapshot(); + Tester::new(NoNewFunctionAsProps::NAME, pass, fail) + .with_react_perf_plugin(true) + .test_and_snapshot(); } diff --git a/crates/oxc_linter/src/rules/react_perf/no_new_object_as_prop.rs b/crates/oxc_linter/src/rules/react_perf/no_new_object_as_prop.rs index 58eb0a3325301..aca76b00107e6 100644 --- a/crates/oxc_linter/src/rules/react_perf/no_new_object_as_prop.rs +++ b/crates/oxc_linter/src/rules/react_perf/no_new_object_as_prop.rs @@ -42,7 +42,7 @@ declare_oxc_lint!( /// /// ``` NoNewObjectAsProp, - restriction + correctness ); impl Rule for NoNewObjectAsProp { @@ -113,5 +113,7 @@ fn test() { r"", ]; - Tester::new(NoNewObjectAsProp::NAME, pass, fail).test_and_snapshot(); + Tester::new(NoNewObjectAsProp::NAME, pass, fail) + .with_react_perf_plugin(true) + .test_and_snapshot(); } diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index 37edc6072576a..0fc7dc5dee97c 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -66,6 +66,7 @@ pub struct Tester { jest_plugin: bool, jsx_a11y_plugin: bool, nextjs_plugin: bool, + react_perf_plugin: bool, } impl Tester { @@ -91,6 +92,7 @@ impl Tester { jest_plugin: false, jsx_a11y_plugin: false, nextjs_plugin: false, + react_perf_plugin: false, } } @@ -120,6 +122,11 @@ impl Tester { self } + pub fn with_react_perf_plugin(mut self, yes: bool) -> Self { + self.react_perf_plugin = yes; + self + } + pub fn expect_fix>(mut self, expect_fix: Vec<(S, S, Option)>) -> Self { self.expect_fix = expect_fix.into_iter().map(|(s1, s2, r)| (s1.into(), s2.into(), r)).collect::>(); @@ -188,7 +195,8 @@ impl Tester { .with_import_plugin(self.import_plugin) .with_jest_plugin(self.jest_plugin) .with_jsx_a11y_plugin(self.jsx_a11y_plugin) - .with_nextjs_plugin(self.nextjs_plugin); + .with_nextjs_plugin(self.nextjs_plugin) + .with_react_perf_plugin(self.react_perf_plugin); let linter = Linter::from_options(options) .unwrap() .with_rules(vec![rule]) diff --git a/npm/oxlint/README.md b/npm/oxlint/README.md index 8b5194dd37909..5743d39d29cd6 100644 --- a/npm/oxlint/README.md +++ b/npm/oxlint/README.md @@ -68,6 +68,9 @@ Enable Plugins --import-plugin Enable the experimental import plugin and detect ESM problems --jest-plugin Enable the Jest plugin and detect test problems --jsx-a11y-plugin Enable the JSX-a11y plugin and detect accessibility problems + --nextjs-plugin Enable the Next.js plugin and detect Next.js problems + --react-perf-plugin Enable the React performance plugin and detect rendering performance problems + Fix Problems --fix Fix as many issues as possible. Only unfixed issues are reported in the