diff --git a/crates/oxc_linter/src/rules/oxc/no_rest_spread_properties.rs b/crates/oxc_linter/src/rules/oxc/no_rest_spread_properties.rs index 55552695c80fe..42f0d136eac81 100644 --- a/crates/oxc_linter/src/rules/oxc/no_rest_spread_properties.rs +++ b/crates/oxc_linter/src/rules/oxc/no_rest_spread_properties.rs @@ -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)] @@ -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(), )); } @@ -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(), )); } @@ -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(), )); } diff --git a/crates/oxc_linter/src/snapshots/oxc_no_rest_spread_properties.snap b/crates/oxc_linter/src/snapshots/oxc_no_rest_spread_properties.snap index 2d0a96595dfa3..3083cacf41574 100644 --- a/crates/oxc_linter/src/snapshots/oxc_no_rest_spread_properties.snap +++ b/crates/oxc_linter/src/snapshots/oxc_no_rest_spread_properties.snap @@ -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.