oxc/no-map-spread
oxc plugin · perf
Suggestion
.oxlintrc.json
{
"rules": {
"oxc/no-map-spread": "error"
}
}Disallow the use of object or array spreads in Array.prototype.map and Array.prototype.flatMap to add properties/elements to array items.
// each object in scores gets shallow-copied. Since `scores` is never
// reused, spreading is inefficient.
function getDisplayData() {
const scores: Array<{ username: string, score: number }> = getScores();
const displayData = scores.map(score => ({ ...score, rank: getRank(score) }));
return displayData
}// `score` is mutated in place and is more performant.
function getDisplayData() {
const scores: Array<{ username: string, score: number }> = getScores();
const displayData = scores.map(score => Object.assign(score, { rank: getRank(score) }));
return displayData
}const arr = [{ a: 1 }, { a: 2 }, { a: 3 }];
const arr2 = arr.map(obj => ({ ...obj, b: obj.a * 2 }));const arr = [{ a: 1 }, { a: 2 }, { a: 3 }];
arr.map(obj => Object.assign(obj, { b: obj.a * 2 }));
// instance properties are ignored
class UsersDb {
#users = [];
public get users() {
// clone users, providing caller with their own deep(ish) copy.
return this.#users.map(user => ({ ...user }));
}
}function UsersTable({ users }) {
const usersWithRoles = users.map(user => ({ ...user, role: getRole(user) }));
return (
<table>
{usersWithRoles.map(user => (
<tr>
<td>{user.name}</td>
<td>{user.role}</td>
</tr>
))}
<tfoot>
<tr>
{/* re-read of users */}
<td>Total users: {users.length}</td>
</tr>
</tfoot>
</table>
)
}Examples from the Oxlint documentation.
No tracked preset enables this rule, so it has no reach yet.
None of the tracked presets enable this rule. Turn it on with the config below.
ESLint does not have this rule.
oxc/no-map-spread
oxc plugin · perf
{
"rules": {
"oxc/no-map-spread": "error"
}
}nursery/noMapSpread
nursery · since 2.6.5
{
"linter": {
"rules": {
"nursery": {
"noMapSpread": "error"
}
}
}
}Disallows the use of this in exported functions.
Disallow the use of barrel files where the file contains export * statements, and the total number of modules exceeds a threshold.