diff --git a/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json b/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json index 3de532dc9b41f..8d51d5389e4c8 100644 --- a/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json +++ b/apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json @@ -22,6 +22,14 @@ { "from": "file", "name": "allowedDeprecated" } ] } + ], + "typescript/no-misused-spread": [ + "error", + { + "allow": [ + { "from": "file", "name": "allowedFunc" } + ] + } ] } } diff --git a/apps/oxlint/fixtures/tsgolint_rule_options/test.ts b/apps/oxlint/fixtures/tsgolint_rule_options/test.ts index 23f61b8b4b544..82929c64618be 100644 --- a/apps/oxlint/fixtures/tsgolint_rule_options/test.ts +++ b/apps/oxlint/fixtures/tsgolint_rule_options/test.ts @@ -35,4 +35,15 @@ allowedDeprecated(); // This SHOULD error because notAllowedDeprecated is NOT in the allow list notAllowedDeprecated(); -export { result, customStr }; +// Test no-misused-spread with allow option +// Spreading a function triggers the rule +function allowedFunc() { return 1; } +function notAllowedFunc() { return 2; } + +// This should NOT error because allowedFunc is in the allow list +const allowedSpread = { ...allowedFunc }; + +// This SHOULD error because notAllowedFunc is NOT in the allow list +const notAllowedSpread = { ...notAllowedFunc }; + +export { result, customStr, allowedSpread, notAllowedSpread }; diff --git a/apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap b/apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap index 1607af1a0b378..2b714c554fae7 100644 --- a/apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap +++ b/apps/oxlint/src/snapshots/fixtures__tsgolint_rule_options_--type-aware@oxlint.snap @@ -14,7 +14,15 @@ working directory: fixtures/tsgolint_rule_options 37 | `---- -Found 0 warnings and 1 error. + x typescript-eslint(no-misused-spread): Using the spread operator on a function without additional properties can cause unexpected behavior. Did you forget to call the function? + ,-[test.ts:47:28] + 46 | // This SHOULD error because notAllowedFunc is NOT in the allow list + 47 | const notAllowedSpread = { ...notAllowedFunc }; + : ^^^^^^^^^^^^^^^^^ + 48 | + `---- + +Found 0 warnings and 2 errors. Finished in ms on 1 file using 1 threads. ---------- CLI result: LintFoundErrors diff --git a/crates/oxc_linter/src/rules/typescript/no_misused_spread.rs b/crates/oxc_linter/src/rules/typescript/no_misused_spread.rs index e642f6639510e..fb1dc6a590a4f 100644 --- a/crates/oxc_linter/src/rules/typescript/no_misused_spread.rs +++ b/crates/oxc_linter/src/rules/typescript/no_misused_spread.rs @@ -1,9 +1,22 @@ use oxc_macros::declare_oxc_lint; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; -use crate::rule::Rule; +use crate::{ + rule::{DefaultRuleConfig, Rule}, + utils::TypeOrValueSpecifier, +}; -#[derive(Debug, Default, Clone)] -pub struct NoMisusedSpread; +#[derive(Debug, Default, Clone, Deserialize)] +pub struct NoMisusedSpread(Box); + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Default)] +#[serde(rename_all = "camelCase", default)] +pub struct NoMisusedSpreadConfig { + /// An array of type or value specifiers that are allowed to be spread + /// even if they would normally be flagged as misused. + pub allow: Vec, +} declare_oxc_lint!( /// ### What it does @@ -53,6 +66,17 @@ declare_oxc_lint!( typescript, correctness, pending, + config = NoMisusedSpreadConfig, ); -impl Rule for NoMisusedSpread {} +impl Rule for NoMisusedSpread { + fn from_configuration(value: serde_json::Value) -> Self { + serde_json::from_value::>(value) + .unwrap_or_default() + .into_inner() + } + + fn to_configuration(&self) -> Option> { + Some(serde_json::to_value(&*self.0)) + } +}