Skip to content

Commit 505ceb1

Browse files
committed
fix(linter): Fix the radix rule docs to correctly render as a string-only config option (#16248)
And also reorganize/refactor the code to simplify it. This fixes a mistake I made when initially setting up the docs generation for this rule. Generated docs: ```md ## Configuration This rule accepts one of the following string values: ### `"always"` Always require the radix parameter when using `parseInt()`. ### `"as-needed"` Only require the radix parameter when necessary. ```
1 parent be65f92 commit 505ceb1

File tree

1 file changed

+27
-34
lines changed
  • crates/oxc_linter/src/rules/eslint

1 file changed

+27
-34
lines changed

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

Lines changed: 27 additions & 34 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 schemars::JsonSchema;
99
use serde::{Deserialize, Serialize};
10+
use serde_json::Value;
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 missing_parameters(span: Span) -> OxcDiagnostic {
1419
OxcDiagnostic::warn("Missing parameters.").with_label(span)
@@ -31,9 +36,16 @@ fn invalid_radix(span: Span) -> OxcDiagnostic {
3136

3237
#[derive(Debug, Default, Clone, JsonSchema, Deserialize, Serialize)]
3338
#[serde(rename_all = "camelCase", default)]
34-
pub struct Radix {
35-
/// Configuration option to specify when to require the radix parameter.
36-
radix_type: RadixType,
39+
pub struct Radix(RadixType);
40+
41+
#[derive(Debug, Default, Clone, JsonSchema, Deserialize, Serialize)]
42+
#[serde(rename_all = "kebab-case")]
43+
enum RadixType {
44+
/// Always require the radix parameter when using `parseInt()`.
45+
#[default]
46+
Always,
47+
/// Only require the radix parameter when necessary.
48+
AsNeeded,
3749
}
3850

3951
// doc: https://github.com/eslint/eslint/blob/v9.9.1/docs/src/rules/radix.md
@@ -47,7 +59,12 @@ declare_oxc_lint!(
4759
///
4860
/// ### Why is this bad?
4961
///
50-
/// Using the `parseInt()` function without specifying the radix can lead to unexpected results.
62+
/// Using the `parseInt()` function without specifying
63+
/// the radix can lead to unexpected results.
64+
///
65+
/// See the
66+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#radix)
67+
/// for more information.
5168
///
5269
/// ### Examples
5370
///
@@ -64,19 +81,12 @@ declare_oxc_lint!(
6481
eslint,
6582
pedantic,
6683
conditional_fix_dangerous,
67-
config = Radix,
84+
config = RadixType,
6885
);
6986

7087
impl Rule for Radix {
71-
fn from_configuration(value: serde_json::Value) -> Self {
72-
let obj = value.get(0);
73-
74-
Self {
75-
radix_type: obj
76-
.and_then(serde_json::Value::as_str)
77-
.map(RadixType::from)
78-
.unwrap_or_default(),
79-
}
88+
fn from_configuration(value: Value) -> Self {
89+
serde_json::from_value::<DefaultRuleConfig<Radix>>(value).unwrap_or_default().into_inner()
8090
}
8191

8292
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
@@ -124,7 +134,7 @@ impl Radix {
124134
match call_expr.arguments.len() {
125135
0 => ctx.diagnostic(missing_parameters(call_expr.span)),
126136
1 => {
127-
if matches!(&self.radix_type, RadixType::Always) {
137+
if matches!(&self.0, RadixType::Always) {
128138
let first_arg = &call_expr.arguments[0];
129139
let end = call_expr.span.end;
130140
let check_span = Span::new(first_arg.span().start, end);
@@ -141,7 +151,7 @@ impl Radix {
141151
}
142152
_ => {
143153
let radix_arg = &call_expr.arguments[1];
144-
if matches!(&self.radix_type, RadixType::AsNeeded) && is_default_radix(radix_arg) {
154+
if matches!(&self.0, RadixType::AsNeeded) && is_default_radix(radix_arg) {
145155
ctx.diagnostic(redundant_radix(radix_arg.span()));
146156
} else if !is_valid_radix(radix_arg) {
147157
ctx.diagnostic(invalid_radix(radix_arg.span()));
@@ -151,23 +161,6 @@ impl Radix {
151161
}
152162
}
153163

154-
#[derive(Debug, Default, Clone, JsonSchema, Deserialize, Serialize)]
155-
#[serde(rename_all = "kebab-case")]
156-
enum RadixType {
157-
#[default]
158-
Always,
159-
AsNeeded,
160-
}
161-
162-
impl RadixType {
163-
pub fn from(raw: &str) -> Self {
164-
match raw {
165-
"as-needed" => Self::AsNeeded,
166-
_ => Self::Always,
167-
}
168-
}
169-
}
170-
171164
fn is_default_radix(node: &Argument) -> bool {
172165
node.to_expression().is_specific_raw_number_literal("10")
173166
}

0 commit comments

Comments
 (0)