Skip to content
Merged
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
18 changes: 14 additions & 4 deletions crates/oxc_linter/src/rules/oxc/no_map_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ fn no_map_spread_diagnostic(
assert!(!spans.is_empty());
let mut spread_labels = spread.spread_spans().into_iter();
let first_message = if spans.len() == 1 {
"It should be mutated in place"
"This spread allocates a new value on each iteration"
} else {
"They should be mutated in place"
"These spreads allocate new values on each iteration"
};
let first = spread_labels.next().unwrap().label(first_message);
let others = spread_labels.map(LabeledSpan::from);
Expand All @@ -51,13 +51,23 @@ fn no_map_spread_diagnostic(
"Spreading to modify object properties in `map` calls is inefficient",
)
.with_labels([map_call.label("This map call spreads an object"), first])
.with_help("Consider using `Object.assign` instead"),
.with_help(
"If in-place mutation is acceptable, use `Object.assign` or direct property assignment instead of spreading",
)
.with_note(
"`Object.assign` mutates the first argument. Disable this rule if copy-on-write behavior is required.",
),
// Array
Spread::Array(_) => OxcDiagnostic::warn(
"Spreading to modify array elements in `map` calls is inefficient",
)
.with_labels([map_call.label("This map call spreads an array"), first])
.with_help("Consider using `Array.prototype.concat` or `Array.prototype.push` instead"),
.with_help(
"If in-place mutation is acceptable, use `push` (or `concat` when semantics match) instead of spreading",
)
.with_note(
"`push` mutates the array. `concat` returns a new array and is not equivalent for every iterable.",
),
};

diagnostic.and_labels(others).and_labels(returned_label)
Expand Down
Loading
Loading