Skip to content

Commit b2bc5b4

Browse files
committed
fix(linter/react-perf/jsx-no-new-object-as-prop): skip as/satisfies exprs (#13718)
fixes #13717
1 parent 99a7638 commit b2bc5b4

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

crates/oxc_linter/src/rules/react_perf/jsx_no_new_object_as_prop.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl ReactPerfRule for JsxNoNewObjectAsProp {
7777
}
7878

7979
fn check_expression(expr: &Expression) -> Option<Span> {
80-
match expr.without_parentheses() {
80+
match expr.get_inner_expression() {
8181
Expression::ObjectExpression(expr) => Some(expr.span),
8282
Expression::CallExpression(expr) => {
8383
if is_constructor_matching_name(&expr.callee, "Object")
@@ -158,6 +158,10 @@ fn test() {
158158
r"const Foo = () => (<Item config={this.props.config || (this.props.default ? this.props.default : {})} />)",
159159
r"const Foo = () => { const x = {}; return <Bar x={x} /> }",
160160
r"const Foo = ({ x = {} }) => <Item x={x} />",
161+
r"const Foo = () => { const x: Foo = {}; return <Bar x={x} /> }",
162+
r"const Foo = () => { const x: Foo = {} as Foo; return <Bar x={x} /> }",
163+
r"const Foo = () => { const x: Foo = {} satisfies Foo; return <Bar x={x} /> }",
164+
r"const Foo = () => { const x: Foo = {} as const; return <Bar x={x} /> }",
161165
];
162166

163167
Tester::new(JsxNoNewObjectAsProp::NAME, JsxNoNewObjectAsProp::PLUGIN, pass, fail)

crates/oxc_linter/src/snapshots/react_perf_jsx_no_new_object_as_prop.snap

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,43 @@ source: crates/oxc_linter/src/tester.rs
8383
· ╰── The prop was declared here
8484
╰────
8585
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).
86+
87+
eslint-plugin-react-perf(jsx-no-new-object-as-prop): JSX attribute values should not contain objects created in the same scope.
88+
╭─[jsx_no_new_object_as_prop.tsx:1:27]
89+
1const Foo = () => { const x: Foo = {}; return <Bar x={x} /> }
90+
· ───┬── ─┬ ┬
91+
· │ │ ╰── And used here
92+
· │ ╰── And assigned a new value here
93+
· ╰── The prop was declared here
94+
╰────
95+
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).
96+
97+
eslint-plugin-react-perf(jsx-no-new-object-as-prop): JSX attribute values should not contain objects created in the same scope.
98+
╭─[jsx_no_new_object_as_prop.tsx:1:27]
99+
1const Foo = () => { const x: Foo = {} as Foo; return <Bar x={x} /> }
100+
· ───┬── ─┬ ┬
101+
· │ │ ╰── And used here
102+
· │ ╰── And assigned a new value here
103+
· ╰── The prop was declared here
104+
╰────
105+
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).
106+
107+
eslint-plugin-react-perf(jsx-no-new-object-as-prop): JSX attribute values should not contain objects created in the same scope.
108+
╭─[jsx_no_new_object_as_prop.tsx:1:27]
109+
1const Foo = () => { const x: Foo = {} satisfies Foo; return <Bar x={x} /> }
110+
· ───┬── ─┬ ┬
111+
· │ │ ╰── And used here
112+
· │ ╰── And assigned a new value here
113+
· ╰── The prop was declared here
114+
╰────
115+
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).
116+
117+
eslint-plugin-react-perf(jsx-no-new-object-as-prop): JSX attribute values should not contain objects created in the same scope.
118+
╭─[jsx_no_new_object_as_prop.tsx:1:27]
119+
1const Foo = () => { const x: Foo = {} as const; return <Bar x={x} /> }
120+
· ───┬── ─┬ ┬
121+
· │ │ ╰── And used here
122+
· │ ╰── And assigned a new value here
123+
· ╰── The prop was declared here
124+
╰────
125+
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 commit comments

Comments
 (0)