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
35 changes: 30 additions & 5 deletions crates/oxc_linter/src/rules/oxc/no_rest_spread_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,37 @@ use oxc_span::Span;
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Clone, Copy)]
enum SpreadKind {
ObjectSpreadProperty,
ObjectRestProperty,
}

impl SpreadKind {
fn details(self) -> (&'static str, &'static str) {
match self {
Self::ObjectSpreadProperty => (
"object spread property",
"Use `Object.assign()` to combine objects instead of object spread syntax.",
),
Self::ObjectRestProperty => (
"object rest property",
"List the properties you need explicitly instead of using object rest syntax.",
),
}
}
}

fn no_rest_spread_properties_diagnostic(
span: Span,
spread_kind: &str,
spread_kind: SpreadKind,
message_suffix: &str,
) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("{spread_kind} are not allowed. {message_suffix}")).with_label(span)
let (kind, help) = spread_kind.details();

OxcDiagnostic::warn(format!("{kind} are not allowed. {message_suffix}"))
.with_help(help)
.with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize)]
Expand Down Expand Up @@ -75,7 +100,7 @@ impl Rule for NoRestSpreadProperties {
if matches!(ctx.nodes().parent_kind(node.id()), AstKind::ObjectExpression(_)) {
ctx.diagnostic(no_rest_spread_properties_diagnostic(
spread_element.span,
"object spread property",
SpreadKind::ObjectSpreadProperty,
self.object_spread_message.as_str(),
));
}
Expand All @@ -84,7 +109,7 @@ impl Rule for NoRestSpreadProperties {
if matches!(ctx.nodes().parent_kind(node.id()), AstKind::ObjectPattern(_)) {
ctx.diagnostic(no_rest_spread_properties_diagnostic(
rest_element.span,
"object rest property",
SpreadKind::ObjectRestProperty,
self.object_rest_message.as_str(),
));
}
Expand All @@ -96,7 +121,7 @@ impl Rule for NoRestSpreadProperties {

ctx.diagnostic(no_rest_spread_properties_diagnostic(
rest.span,
"object rest property",
SpreadKind::ObjectRestProperty,
self.object_rest_message.as_str(),
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,39 @@ source: crates/oxc_linter/src/tester.rs
1 │ ({...a})
· ────
╰────
help: Use `Object.assign()` to combine objects instead of object spread syntax.

⚠ oxc(no-rest-spread-properties): object rest property are not allowed.
╭─[no_rest_spread_properties.tsx:1:3]
1 │ ({...a} = obj)
· ────
╰────
help: List the properties you need explicitly instead of using object rest syntax.

⚠ oxc(no-rest-spread-properties): object rest property are not allowed.
╭─[no_rest_spread_properties.tsx:1:7]
1 │ for ({...a} in foo) {}
· ────
╰────
help: List the properties you need explicitly instead of using object rest syntax.

⚠ oxc(no-rest-spread-properties): object rest property are not allowed.
╭─[no_rest_spread_properties.tsx:1:13]
1 │ function f({...a}) {}
· ────
╰────
help: List the properties you need explicitly instead of using object rest syntax.

⚠ oxc(no-rest-spread-properties): object spread property are not allowed. Our codebase does not allow object spread properties.
╭─[no_rest_spread_properties.tsx:1:3]
1 │ ({...a})
· ────
╰────
help: Use `Object.assign()` to combine objects instead of object spread syntax.

⚠ oxc(no-rest-spread-properties): object rest property are not allowed. Our codebase does not allow object rest properties.
╭─[no_rest_spread_properties.tsx:1:3]
1 │ ({...a} = obj)
· ────
╰────
help: List the properties you need explicitly instead of using object rest syntax.
Loading