Skip to content

Commit

Permalink
fix(linter): Rename react_perf/jsx_no_new_function_as_props to jsx_no…
Browse files Browse the repository at this point in the history
…_new_function_as_prop
  • Loading branch information
leaysgur committed Jan 26, 2024
1 parent ee5b968 commit f96ceb9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ mod react {
mod react_perf {
pub mod jsx_no_jsx_as_prop;
pub mod jsx_no_new_array_as_prop;
pub mod jsx_no_new_function_as_props;
pub mod jsx_no_new_function_as_prop;
pub mod jsx_no_new_object_as_prop;
}

Expand Down Expand Up @@ -524,7 +524,7 @@ oxc_macros::declare_all_lint_rules! {
react::require_render_return,
react_perf::jsx_no_jsx_as_prop,
react_perf::jsx_no_new_array_as_prop,
react_perf::jsx_no_new_function_as_props,
react_perf::jsx_no_new_function_as_prop,
react_perf::jsx_no_new_object_as_prop,
import::default,
import::no_named_as_default_member,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ use crate::{
};

#[derive(Debug, Error, Diagnostic)]
#[error("eslint-plugin-react-perf(jsx-no-new-function-as-props): JSX attribute values should not contain functions created in the same scope.")]
#[error("eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.")]
#[diagnostic(severity(warning), help(r"simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array)."))]
struct JsxNoNewFunctionAsPropsDiagnostic(#[label] pub Span);
struct JsxNoNewFunctionAsPropDiagnostic(#[label] pub Span);

#[derive(Debug, Default, Clone)]
pub struct JsxNoNewFunctionAsProps;
pub struct JsxNoNewFunctionAsProp;

declare_oxc_lint!(
/// ### What it does
Expand All @@ -40,11 +40,11 @@ declare_oxc_lint!(
/// // Good
/// <Item callback={this.props.callback} />
/// ```
JsxNoNewFunctionAsProps,
JsxNoNewFunctionAsProp,
correctness
);

impl Rule for JsxNoNewFunctionAsProps {
impl Rule for JsxNoNewFunctionAsProp {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::JSXElement(jsx_elem) = node.kind() {
check_jsx_element(jsx_elem, ctx);
Expand All @@ -61,7 +61,7 @@ fn check_jsx_element<'a>(jsx_elem: &JSXElement<'a>, ctx: &LintContext<'a>) {
..
})) => {
if let Some(span) = check_expression(expr) {
ctx.diagnostic(JsxNoNewFunctionAsPropsDiagnostic(span));
ctx.diagnostic(JsxNoNewFunctionAsPropDiagnostic(span));
}
}
_ => {}
Expand Down Expand Up @@ -139,7 +139,7 @@ fn test() {
r"<Item prop={this.props.callback || (this.props.cb ? this.props.cb : function(){})} />",
];

Tester::new(JsxNoNewFunctionAsProps::NAME, pass, fail)
Tester::new(JsxNoNewFunctionAsProp::NAME, pass, fail)
.with_react_perf_plugin(true)
.test_and_snapshot();
}
68 changes: 68 additions & 0 deletions crates/oxc_linter/src/snapshots/jsx_no_new_function_as_prop.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
source: crates/oxc_linter/src/tester.rs
expression: jsx_no_new_function_as_prop
---
eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item prop={function(){return true}} />
· ───────────────────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).

eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item prop={() => true} />
· ──────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).

eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item prop={new Function('a', 'alert(a)')}/>
· ─────────────────────────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).

eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item prop={Function()}/>
· ──────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).

eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item onClick={this.clickHandler.bind(this)} />
· ────────────────────────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).

eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item callback={this.props.callback || function() {}} />
· ─────────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).

eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item callback={this.props.callback ? this.props.callback : function() {}} />
· ─────────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).

eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item prop={this.props.callback || this.props.callback ? this.props.callback : function(){}} />
· ────────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).

eslint-plugin-react-perf(jsx-no-new-function-as-prop): JSX attribute values should not contain functions created in the same scope.
╭─[jsx_no_new_function_as_prop.tsx:1:1]
1<Item prop={this.props.callback || (this.props.cb ? this.props.cb : function(){})} />
· ────────────
╰────
help: simplify props or memoize props in the parent component (https://react.dev/reference/react/memo#my-component-rerenders-when-a-prop-is-an-object-or-array).


0 comments on commit f96ceb9

Please sign in to comment.