Skip to content

Commit a63bfb7

Browse files
committed
refactor(linter): Use DefaultRuleConfig for 2 more rules.
1 parent 1eef28b commit a63bfb7

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

crates/oxc_linter/src/rules/eslint/use_isnan.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ use oxc_macros::declare_oxc_lint;
77
use oxc_span::{GetSpan, Span};
88
use oxc_syntax::operator::BinaryOperator;
99
use schemars::JsonSchema;
10+
use serde::Deserialize;
1011

11-
use crate::{AstNode, context::LintContext, rule::Rule};
12+
use crate::{
13+
AstNode,
14+
context::LintContext,
15+
rule::{DefaultRuleConfig, Rule},
16+
};
1217

1318
fn comparison_with_na_n(span: Span) -> OxcDiagnostic {
1419
OxcDiagnostic::warn("Requires calls to `isNaN()` when checking for NaN")
@@ -36,7 +41,7 @@ fn index_of_na_n(method_name: &str, span: Span) -> OxcDiagnostic {
3641
.with_label(span)
3742
}
3843

39-
#[derive(Debug, Clone, JsonSchema)]
44+
#[derive(Debug, Clone, JsonSchema, Deserialize)]
4045
#[serde(rename_all = "camelCase", default)]
4146
pub struct UseIsnan {
4247
/// Whether to disallow NaN in switch cases and discriminants
@@ -139,21 +144,9 @@ impl Rule for UseIsnan {
139144
}
140145

141146
fn from_configuration(value: serde_json::Value) -> Self {
142-
let (enforce_for_switch_case, enforce_for_index_of) =
143-
value.get(0).map_or((true, false), |config| {
144-
(
145-
config
146-
.get("enforceForSwitchCase")
147-
.and_then(serde_json::Value::as_bool)
148-
.unwrap_or(true),
149-
config
150-
.get("enforceForIndexOf")
151-
.and_then(serde_json::Value::as_bool)
152-
.unwrap_or_default(),
153-
)
154-
});
155-
156-
Self { enforce_for_switch_case, enforce_for_index_of }
147+
serde_json::from_value::<DefaultRuleConfig<UseIsnan>>(value)
148+
.unwrap_or_default()
149+
.into_inner()
157150
}
158151
}
159152

crates/oxc_linter/src/rules/eslint/valid_typeof.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ use oxc_macros::declare_oxc_lint;
44
use oxc_span::{GetSpan, Span};
55
use oxc_syntax::operator::UnaryOperator;
66
use schemars::JsonSchema;
7+
use serde::Deserialize;
78

8-
use crate::{AstNode, context::LintContext, rule::Rule};
9+
use crate::{
10+
AstNode,
11+
context::LintContext,
12+
rule::{DefaultRuleConfig, Rule},
13+
};
914

1015
fn not_string(help: Option<&'static str>, span: Span) -> OxcDiagnostic {
1116
let mut d =
@@ -24,7 +29,7 @@ fn invalid_value(help: Option<&'static str>, span: Span) -> OxcDiagnostic {
2429
d
2530
}
2631

27-
#[derive(Debug, Clone, Default, JsonSchema)]
32+
#[derive(Debug, Clone, Default, JsonSchema, Deserialize)]
2833
#[serde(rename_all = "camelCase", default)]
2934
pub struct ValidTypeof {
3035
/// The `requireStringLiterals` option when set to `true`, allows the comparison of `typeof`
@@ -154,14 +159,9 @@ impl Rule for ValidTypeof {
154159
}
155160

156161
fn from_configuration(value: serde_json::Value) -> Self {
157-
let require_string_literals = value.get(0).is_some_and(|config| {
158-
config
159-
.get("requireStringLiterals")
160-
.and_then(serde_json::Value::as_bool)
161-
.unwrap_or(false)
162-
});
163-
164-
Self { require_string_literals }
162+
serde_json::from_value::<DefaultRuleConfig<ValidTypeof>>(value)
163+
.unwrap_or_default()
164+
.into_inner()
165165
}
166166
}
167167

0 commit comments

Comments
 (0)