Skip to content

Commit

Permalink
feat: introduce --react-perf-plugin CLI flag, update rules to correct…
Browse files Browse the repository at this point in the history
…ness (#2119)

#2041 (comment)

Closes #2041
  • Loading branch information
byteHulk authored Jan 22, 2024
1 parent 69fecac commit 20a34b5
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 10 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_cli/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_linter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -40,6 +41,7 @@ impl Default for LintOptions {
jest_plugin: false,
jsx_a11y_plugin: false,
nextjs_plugin: false,
react_perf_plugin: false,
}
}
}
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -209,6 +218,7 @@ impl LintOptions {
let mut rules = rules.into_iter().collect::<Vec<_>>();
// for stable diagnostics output ordering
rules.sort_unstable_by_key(RuleEnum::name);

Ok((rules, config.map(ESLintConfig::settings).unwrap_or_default()))
}

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/react_perf/no_jsx_as_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ declare_oxc_lint!(
/// <Item callback={this.props.jsx} />
/// ```
NoJsxAsProp,
restriction
correctness
);

impl Rule for NoJsxAsProp {
Expand Down Expand Up @@ -91,5 +91,5 @@ fn test() {
r"<Item jsx={this.props.jsx || (this.props.component ? this.props.component : <SubItem />)} />",
];

Tester::new(NoJsxAsProp::NAME, pass, fail).test_and_snapshot();
Tester::new(NoJsxAsProp::NAME, pass, fail).with_react_perf_plugin(true).test_and_snapshot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ declare_oxc_lint!(
/// <Item list={this.props.list} />
/// ```
NoNewArrayAsProp,
restriction
correctness
);

impl Rule for NoNewArrayAsProp {
Expand Down Expand Up @@ -113,5 +113,7 @@ fn test() {
r"<Item list={this.props.list || (this.props.arr ? this.props.arr : [])} />",
];

Tester::new(NoNewArrayAsProp::NAME, pass, fail).test_and_snapshot();
Tester::new(NoNewArrayAsProp::NAME, pass, fail)
.with_react_perf_plugin(true)
.test_and_snapshot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare_oxc_lint!(
/// <Item callback={this.props.callback} />
/// ```
NoNewFunctionAsProps,
restriction
correctness
);

impl Rule for NoNewFunctionAsProps {
Expand Down Expand Up @@ -139,5 +139,7 @@ fn test() {
r"<Item prop={this.props.callback || (this.props.cb ? this.props.cb : function(){})} />",
];

Tester::new(NoNewFunctionAsProps::NAME, pass, fail).test_and_snapshot();
Tester::new(NoNewFunctionAsProps::NAME, pass, fail)
.with_react_perf_plugin(true)
.test_and_snapshot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ declare_oxc_lint!(
/// <Item config={staticConfig} />
/// ```
NoNewObjectAsProp,
restriction
correctness
);

impl Rule for NoNewObjectAsProp {
Expand Down Expand Up @@ -113,5 +113,7 @@ fn test() {
r"<Item config={this.props.config || (this.props.default ? this.props.default : {})} />",
];

Tester::new(NoNewObjectAsProp::NAME, pass, fail).test_and_snapshot();
Tester::new(NoNewObjectAsProp::NAME, pass, fail)
.with_react_perf_plugin(true)
.test_and_snapshot();
}
10 changes: 9 additions & 1 deletion crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct Tester {
jest_plugin: bool,
jsx_a11y_plugin: bool,
nextjs_plugin: bool,
react_perf_plugin: bool,
}

impl Tester {
Expand All @@ -91,6 +92,7 @@ impl Tester {
jest_plugin: false,
jsx_a11y_plugin: false,
nextjs_plugin: false,
react_perf_plugin: false,
}
}

Expand Down Expand Up @@ -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<S: Into<String>>(mut self, expect_fix: Vec<(S, S, Option<Value>)>) -> Self {
self.expect_fix =
expect_fix.into_iter().map(|(s1, s2, r)| (s1.into(), s2.into(), r)).collect::<Vec<_>>();
Expand Down Expand Up @@ -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])
Expand Down
3 changes: 3 additions & 0 deletions npm/oxlint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 20a34b5

Please sign in to comment.