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
8 changes: 8 additions & 0 deletions apps/oxlint/fixtures/tsgolint_rule_options/.oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
{ "from": "file", "name": "allowedDeprecated" }
]
}
],
"typescript/no-misused-spread": [
"error",
{
"allow": [
{ "from": "file", "name": "allowedFunc" }
]
}
]
}
}
13 changes: 12 additions & 1 deletion apps/oxlint/fixtures/tsgolint_rule_options/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Original file line number Diff line number Diff line change
Expand Up @@ -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 <variable>ms on 1 file using 1 threads.
----------
CLI result: LintFoundErrors
Expand Down
32 changes: 28 additions & 4 deletions crates/oxc_linter/src/rules/typescript/no_misused_spread.rs
Original file line number Diff line number Diff line change
@@ -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<NoMisusedSpreadConfig>);

#[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<TypeOrValueSpecifier>,
}

declare_oxc_lint!(
/// ### What it does
Expand Down Expand Up @@ -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::<DefaultRuleConfig<NoMisusedSpread>>(value)
.unwrap_or_default()
.into_inner()
}

fn to_configuration(&self) -> Option<Result<serde_json::Value, serde_json::Error>> {
Some(serde_json::to_value(&*self.0))
}
}
Loading