Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Object spread operator is unnecessarily transformed when used with React #7054

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _object_spread from "@swc/helpers/src/_object_spread.mjs";
import _object_spread_props from "@swc/helpers/src/_object_spread_props.mjs";
import _object_without_properties from "@swc/helpers/src/_object_without_properties.mjs";
import Head from "next/head";
import Image from "next/image";
Expand Down Expand Up @@ -50,9 +52,9 @@ export default function Home() {
var href = _param.href, linkProps = _object_without_properties(_param, [
"href"
]);
return React.createElement("link", Object.assign({
return React.createElement("link", _object_spread_props(_object_spread({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a regression.

href: href
}, linkProps, {
}, linkProps), {
rel: "icon",
key: href,
__source: {
Expand Down
14 changes: 3 additions & 11 deletions crates/swc_ecma_transforms_react/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ use swc_config::merge::Merge;
use swc_ecma_ast::*;
use swc_ecma_parser::{parse_file_as_expr, Syntax};
use swc_ecma_transforms_base::helper;
use swc_ecma_utils::{
drop_span, member_expr, prepend_stmt, private_ident, quote_ident, undefined, ExprFactory,
};
use swc_ecma_utils::{drop_span, prepend_stmt, private_ident, quote_ident, undefined, ExprFactory};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

use self::static_check::should_use_create_element;
Expand Down Expand Up @@ -832,7 +830,7 @@ where
return Box::new(Expr::Lit(Lit::Null(Null { span: DUMMY_SP })));
}

if self.use_spread {
if self.use_spread || self.use_builtins {
return self.fold_attrs_for_next_classic(attrs);
}

Expand Down Expand Up @@ -872,13 +870,7 @@ where
// calls `_extends` or `Object.assign`
Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: {
if self.use_builtins {
member_expr!(DUMMY_SP, Object.assign).as_callee()
} else {
helper!(extends, "extends")
}
},
callee: helper!(extends, "extends"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be reverted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand the difference between configuring use_buildin and use_spread. It looks like the public is the same, only one is {... } One is Object.assign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can support issue without fixing it, just configure use_spread

args,
type_args: None,
}))
Expand Down
9 changes: 5 additions & 4 deletions crates/swc_ecma_transforms_react/src/jsx/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,11 +1172,12 @@ test!(
Mark::fresh(Mark::root())
),
use_builtins_assignment,
r#"var div = <Component {...props} foo="bar" />"#,
r#"var div = <Component {...props} foo="bar"/>"#,
r#"
var div = React.createElement(Component, Object.assign({}, props, {
foo: "bar"
}));
var div = React.createElement(Component, {
...props,
foo: "bar"
});
"#
);

Expand Down