From 87d8da7f18ecfe3f403ef03a85b6092ee85194ed Mon Sep 17 00:00:00 2001 From: Sysix Date: Thu, 26 Dec 2024 19:59:13 +0100 Subject: [PATCH] fix(linter): rule no-restricted-imports support missing options - build regex in `from_configuration` --- .../src/rules/eslint/no_restricted_imports.rs | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs b/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs index 0e7b169ee5df1..055bb6afb73a6 100644 --- a/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs +++ b/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs @@ -4,8 +4,9 @@ use oxc_macros::declare_oxc_lint; use oxc_span::{CompactStr, Span}; use regex::Regex; use rustc_hash::FxHashMap; -use serde::Deserialize; +use serde::{de::Error, Deserialize, Deserializer}; use serde_json::Value; +use std::borrow::Cow; use crate::{ context::LintContext, @@ -58,15 +59,42 @@ struct RestrictedPath { #[serde(rename_all = "camelCase")] struct RestrictedPattern { group: Option>, - regex: Option, + regex: Option>, import_names: Option>, - import_name_pattern: Option, + import_name_pattern: Option>, allow_import_names: Option>, - allow_import_name_pattern: Option, + allow_import_name_pattern: Option>, case_sensitive: Option, message: Option, } +/// A wrapper type which implements `Serialize` and `Deserialize` for +/// types involving `Regex` +#[derive(Debug, Clone, Eq, Hash, PartialEq)] +pub struct SerdeRegexWrapper(pub T); + +impl std::ops::Deref for SerdeRegexWrapper { + type Target = Regex; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl<'de> Deserialize<'de> for SerdeRegexWrapper { + fn deserialize(d: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let s = >::deserialize(d)?; + + match s.parse() { + Ok(regex) => Ok(SerdeRegexWrapper(regex)), + Err(err) => Err(D::Error::custom(err)), + } + } +} + #[derive(Debug)] enum GlobResult { Found, @@ -323,11 +351,7 @@ impl RestrictedPattern { return false; }; - let Ok(reg_exp) = Regex::new(regex.as_str()) else { - return false; - }; - - reg_exp.find(name.name()).is_some() + regex.find(name.name()).is_some() } fn get_import_name_pattern_result(&self, name: &CompactStr) -> bool { @@ -335,11 +359,7 @@ impl RestrictedPattern { return false; }; - let Ok(reg_exp) = Regex::new(import_name_pattern.as_str()) else { - return false; - }; - - reg_exp.find(name).is_some() + import_name_pattern.find(name).is_some() } fn get_allow_import_name_pattern_result(&self, name: &CompactStr) -> bool { @@ -347,11 +367,7 @@ impl RestrictedPattern { return false; }; - let Ok(reg_exp) = Regex::new(allow_import_names.as_str()) else { - return false; - }; - - reg_exp.find(name).is_some() + allow_import_names.find(name).is_some() } }