Skip to content

Conversation

@mdevils
Copy link
Contributor

@mdevils mdevils commented May 24, 2025

Summary

Prefer object spread over Object.assign() when constructing new objects.

Object spread syntax is more concise, more readable, and performs better
than Object.assign() when creating a new object from existing objects.
It also has better TypeScript integration.

Examples

Invalid

Object.assign({}, foo);
Object.assign({}, { foo: 'bar' });
Object.assign({ foo: 'bar' }, baz);
Object.assign({}, baz, { foo: 'bar' });

Valid

({ ...foo });
({ ...baz, foo: 'bar' });

Modifying an existing object is allowed:

Object.assign(foo, { bar: baz });

Test Plan

Tests are included

Closes #4319

@github-actions github-actions bot added A-CLI Area: CLI A-Project Area: project A-Linter Area: linter L-JavaScript Language: JavaScript and super languages A-Diagnostic Area: diagnostocis labels May 24, 2025
@mdevils mdevils force-pushed the feat/use-object-spread branch from ef07839 to 4863080 Compare May 24, 2025 20:23
@codspeed-hq
Copy link

codspeed-hq bot commented May 24, 2025

CodSpeed Performance Report

Merging #6129 will not alter performance

Comparing mdevils:feat/use-object-spread (8ee6c0b) with main (c113eb6)

Summary

✅ 95 untouched benchmarks

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

The diagnostic doesn't follow the rule pillars. We must change it. Also, we could add more tests

Comment on lines 98 to 184
"Object spread should be used instead of "<Emphasis>"Object.assign"</Emphasis>
" when constructing new objects."
},
)
.note(markup! {
"Replace "<Emphasis>"Object.assign({...}, <object>)"</Emphasis>
" with "<Emphasis>"{ ...<object> }"</Emphasis>"."
Copy link
Member

Choose a reason for hiding this comment

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

These diagnostics don't follow the three rule pillars https://biomejs.dev/linter/#rule-pillars

However, I'm not sure if what's missing is the error, or the reason why it's an error. Probably the latter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the diagnostic, thanks.

Comment on lines 2 to 7
({ ...foo });
({ ...baz, foo: 'bar' });
Object.assign(foo, { bar: baz });
Object.assign(foo, bar);
Object.assign(foo, { bar, baz });
Object.assign(foo, { ...baz });
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added more tests from eslint.

@mdevils
Copy link
Contributor Author

mdevils commented May 28, 2025

I've also added the fix action.

@mdevils mdevils requested a review from ematipico May 28, 2025 10:07
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Thank you!

@mdevils mdevils force-pushed the feat/use-object-spread branch from 0b079f4 to 8ee6c0b Compare May 28, 2025 13:43
@mdevils
Copy link
Contributor Author

mdevils commented May 28, 2025

Rebased.

@ematipico ematipico merged commit 67dfa88 into biomejs:main May 28, 2025
14 checks passed
ematipico added a commit that referenced this pull request May 29, 2025
@mdevils mdevils deleted the feat/use-object-spread branch May 29, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-CLI Area: CLI A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

📎 Implement eslint/prefer-object-spread

2 participants