-
-
Notifications
You must be signed in to change notification settings - Fork 722
Description
There are more than a dozen rules with config options which cannot be serialized using serde/schemars because they use lazy_regex, see this PR for an example of the problem: #15159
Code like this will not work because of using Regex with JsonSchema:
#[derive(Debug, Deserialize, Clone, Serialize, JsonSchema, Default)]
#[serde(rename_all = "camelCase", default)]
pub struct ParamNamesConfig {
/// Regex pattern used to validate the `resolve` parameter name. If provided, this pattern
/// is used instead of the default `^_?resolve$` check.
resolve_pattern: Option<Regex>,
/// Regex pattern used to validate the `reject` parameter name. If provided, this pattern
/// is used instead of the default `^_?reject$` check.
reject_pattern: Option<Regex>,
}See also GREsau/schemars#476, which potentially solves the problem upstream? Although oxc uses a schemars fork currently, so we'll need to pull it in ourselves regardless. (Unfortunately this will require some rework to make it work with the older schemars version we're on because the schemars API was completely changed, and anyway we're using lazy_regex which is distinct from Regex, so it may not be necessarily compatible with this usage)
There is also a SerdeRegexWrapper implemented here, which could potentially be extracted as a solution:
oxc/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Lines 198 to 223 in 69babde
| /// A wrapper type which implements `Serialize` and `Deserialize` for | |
| /// types involving `Regex` | |
| #[derive(Debug, Clone, Eq, Hash, PartialEq)] | |
| pub struct SerdeRegexWrapper<T>(pub T); | |
| impl std::ops::Deref for SerdeRegexWrapper<Regex> { | |
| type Target = Regex; | |
| fn deref(&self) -> &Self::Target { | |
| &self.0 | |
| } | |
| } | |
| impl<'de> Deserialize<'de> for SerdeRegexWrapper<Regex> { | |
| fn deserialize<D>(d: D) -> Result<SerdeRegexWrapper<Regex>, D::Error> | |
| where | |
| D: Deserializer<'de>, | |
| { | |
| let s = <Cow<str>>::deserialize(d)?; | |
| match s.parse() { | |
| Ok(regex) => Ok(SerdeRegexWrapper(regex)), | |
| Err(err) => Err(D::Error::custom(err)), | |
| } | |
| } | |
| } |
This will block the completion of #14743 until it's solved.
This has also required the usage of schemars(skip) on some config options, such as in no-unused-vars. This results in the options not showing up in the generated documentation on the website. This should be fixed after this issue is resolved.