diff --git a/crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs b/crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs index 0a7fd0edbd917..ca771595d2623 100644 --- a/crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs +++ b/crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs @@ -77,7 +77,7 @@ impl ReactPerfRule for JsxNoNewObjectAsProp { } fn check_expression(expr: &Expression) -> Option { - match expr.without_parentheses() { + match expr.get_inner_expression() { Expression::ObjectExpression(expr) => Some(expr.span), Expression::CallExpression(expr) => { if is_constructor_matching_name(&expr.callee, "Object") @@ -158,6 +158,10 @@ fn test() { r"const Foo = () => ()", r"const Foo = () => { const x = {}; return }", r"const Foo = ({ x = {} }) => ", + r"const Foo = () => { const x: Foo = {}; return }", + r"const Foo = () => { const x: Foo = {} as Foo; return }", + r"const Foo = () => { const x: Foo = {} satisfies Foo; return }", + r"const Foo = () => { const x: Foo = {} as const; return }", ]; Tester::new(JsxNoNewObjectAsProp::NAME, JsxNoNewObjectAsProp::PLUGIN, pass, fail) diff --git a/crates/oxc_linter/src/snapshots/react_perf_jsx_no_new_object_as_prop.snap b/crates/oxc_linter/src/snapshots/react_perf_jsx_no_new_object_as_prop.snap index cbfe2e3f935b6..41f6d7fa75b8a 100644 --- a/crates/oxc_linter/src/snapshots/react_perf_jsx_no_new_object_as_prop.snap +++ b/crates/oxc_linter/src/snapshots/react_perf_jsx_no_new_object_as_prop.snap @@ -83,3 +83,43 @@ source: crates/oxc_linter/src/tester.rs · ╰── The prop was declared here ╰──── 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-object-as-prop): JSX attribute values should not contain objects created in the same scope. + ╭─[jsx_no_new_object_as_prop.tsx:1:27] + 1 │ const Foo = () => { const x: Foo = {}; return } + · ───┬── ─┬ ┬ + · │ │ ╰── And used here + · │ ╰── And assigned a new value here + · ╰── The prop was declared here + ╰──── + 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-object-as-prop): JSX attribute values should not contain objects created in the same scope. + ╭─[jsx_no_new_object_as_prop.tsx:1:27] + 1 │ const Foo = () => { const x: Foo = {} as Foo; return } + · ───┬── ─┬ ┬ + · │ │ ╰── And used here + · │ ╰── And assigned a new value here + · ╰── The prop was declared here + ╰──── + 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-object-as-prop): JSX attribute values should not contain objects created in the same scope. + ╭─[jsx_no_new_object_as_prop.tsx:1:27] + 1 │ const Foo = () => { const x: Foo = {} satisfies Foo; return } + · ───┬── ─┬ ┬ + · │ │ ╰── And used here + · │ ╰── And assigned a new value here + · ╰── The prop was declared here + ╰──── + 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-object-as-prop): JSX attribute values should not contain objects created in the same scope. + ╭─[jsx_no_new_object_as_prop.tsx:1:27] + 1 │ const Foo = () => { const x: Foo = {} as const; return } + · ───┬── ─┬ ┬ + · │ │ ╰── And used here + · │ ╰── And assigned a new value here + · ╰── The prop was declared here + ╰──── + 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).