From 05f6a09206cf707a8dee359c27fe3af42ca4bf79 Mon Sep 17 00:00:00 2001 From: camc314 <18101008+camc314@users.noreply.github.com> Date: Tue, 10 Mar 2026 19:14:14 +0000 Subject: [PATCH] fix(linter/no-inline-comments): deserialize rule options with serde (#20207) --- .../src/rules/eslint/no_inline_comments.rs | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_inline_comments.rs b/crates/oxc_linter/src/rules/eslint/no_inline_comments.rs index e3748a0341514..4b2e67412d66e 100644 --- a/crates/oxc_linter/src/rules/eslint/no_inline_comments.rs +++ b/crates/oxc_linter/src/rules/eslint/no_inline_comments.rs @@ -2,13 +2,17 @@ use std::cell::LazyCell; use lazy_regex::{Regex, RegexBuilder}; use schemars::JsonSchema; +use serde::Deserialize; use oxc_ast::{AstKind, Comment}; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_span::Span; -use crate::{context::LintContext, rule::Rule}; +use crate::{ + context::LintContext, + rule::{DefaultRuleConfig, Rule}, +}; fn no_inline_comments_diagnostic(span: Span) -> OxcDiagnostic { OxcDiagnostic::warn("Unexpected comment inline with code") @@ -16,11 +20,11 @@ fn no_inline_comments_diagnostic(span: Span) -> OxcDiagnostic { .with_label(span) } -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, Deserialize)] pub struct NoInlineComments(Box); -#[derive(Debug, Default, Clone, JsonSchema)] -#[serde(rename_all = "camelCase", default)] +#[derive(Debug, Default, Clone, Deserialize, JsonSchema)] +#[serde(rename_all = "camelCase", default, deny_unknown_fields)] pub struct NoInlineCommentsConfig { /// A regex pattern to ignore certain inline comments. /// @@ -32,6 +36,7 @@ pub struct NoInlineCommentsConfig { /// "no-inline-comments": ["error", { "ignorePattern": "webpackChunkName" }] /// } /// ``` + #[serde(default, deserialize_with = "deserialize_ignore_pattern")] ignore_pattern: Option, } @@ -43,6 +48,18 @@ impl std::ops::Deref for NoInlineComments { } } +fn deserialize_ignore_pattern<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + use serde::de::Error; + + Option::::deserialize(deserializer)? + .map(|pattern| RegexBuilder::new(&pattern).build()) + .transpose() + .map_err(D::Error::custom) +} + declare_oxc_lint!( /// ### What it does /// @@ -78,14 +95,7 @@ declare_oxc_lint!( impl Rule for NoInlineComments { fn from_configuration(value: serde_json::Value) -> Result { - let ignore_pattern = value - .get(0) - .and_then(|config| config.get("ignorePattern")) - .and_then(serde_json::Value::as_str) - .and_then(|pattern| RegexBuilder::new(pattern).build().ok()); - let config = NoInlineCommentsConfig { ignore_pattern }; - - Ok(Self(Box::new(config))) + serde_json::from_value::>(value).map(DefaultRuleConfig::into_inner) } fn run_once(&self, ctx: &LintContext) {