Skip to content

Commit efc08d3

Browse files
committed
fix(linter): Fix config docs for no-return-assign and unicode-bom rules. (#16260)
These rules both take string options and were incorrectly documented as objects. ``` ## Configuration This rule accepts one of the following string values: ### `"always"` Disallow all assignments in return statements. ### `"except-parens"` Allow assignments in return statements only if they are enclosed in parentheses. This is the default mode. ``` ```md ## Configuration This rule accepts one of the following string values: ### `"always"` Always require a Unicode BOM (Byte Order Mark) at the beginning of the file. ### `"never"` Never allow a Unicode BOM (Byte Order Mark) at the beginning of the file. This is the default option. ```
1 parent dc96d3f commit efc08d3

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,39 @@ use oxc_diagnostics::OxcDiagnostic;
33
use oxc_macros::declare_oxc_lint;
44
use oxc_span::{GetSpan, Span};
55
use schemars::JsonSchema;
6+
use serde::Deserialize;
67
use serde_json::Value;
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 no_return_assign_diagnostic(span: Span, message: &'static str) -> OxcDiagnostic {
1116
OxcDiagnostic::warn(message)
1217
.with_label(span)
1318
.with_help("Did you mean to use `==` instead of `=`?")
1419
}
1520

16-
#[derive(Debug, Default, Clone, JsonSchema)]
17-
#[serde(rename_all = "camelCase", default)]
18-
pub struct NoReturnAssign {
19-
/// Whether to always disallow assignment in return statements.
20-
always_disallow_assignment_in_return: bool,
21+
#[derive(Debug, Default, Clone, Deserialize)]
22+
pub struct NoReturnAssign(NoReturnAssignMode);
23+
24+
#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
25+
#[serde(rename_all = "kebab-case")]
26+
pub enum NoReturnAssignMode {
27+
/// Disallow all assignments in return statements.
28+
Always,
29+
/// Allow assignments in return statements only if they are enclosed in parentheses.
30+
/// This is the default mode.
31+
#[default]
32+
ExceptParens,
2133
}
2234

2335
declare_oxc_lint!(
2436
/// ### What it does
2537
///
26-
/// Disallows assignment operators in return statements
38+
/// Disallows assignment operators in return statements.
2739
///
2840
/// ### Why is this bad?
2941
///
@@ -48,7 +60,7 @@ declare_oxc_lint!(
4860
eslint,
4961
style,
5062
pending, // TODO: add a suggestion
51-
config = NoReturnAssign,
63+
config = NoReturnAssignMode,
5264
);
5365

5466
fn is_sentinel_node(ast_kind: AstKind) -> bool {
@@ -61,18 +73,18 @@ fn is_sentinel_node(ast_kind: AstKind) -> bool {
6173

6274
impl Rule for NoReturnAssign {
6375
fn from_configuration(value: Value) -> Self {
64-
let always_disallow_assignment_in_return = value
65-
.get(0)
66-
.and_then(Value::as_str)
67-
.map_or_else(|| false, |value| value != "except-parens");
68-
Self { always_disallow_assignment_in_return }
76+
serde_json::from_value::<DefaultRuleConfig<NoReturnAssign>>(value)
77+
.unwrap_or_default()
78+
.into_inner()
6979
}
7080

7181
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
7282
let AstKind::AssignmentExpression(assign) = node.kind() else {
7383
return;
7484
};
75-
if !self.always_disallow_assignment_in_return
85+
86+
// Skip if mode is ExceptParens and the assignment is parenthesized
87+
if matches!(self.0, NoReturnAssignMode::ExceptParens)
7688
&& ctx.nodes().parent_kind(node.id()).as_parenthesized_expression().is_some()
7789
{
7890
return;

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

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use oxc_span::{SPAN, Span};
44
use schemars::JsonSchema;
55
use serde::Deserialize;
66

7-
use crate::{context::LintContext, rule::Rule};
7+
use crate::{
8+
context::LintContext,
9+
rule::{DefaultRuleConfig, Rule},
10+
};
811

912
fn unexpected_unicode_bom_diagnostic(span: Span) -> OxcDiagnostic {
1013
OxcDiagnostic::warn("Unexpected Unicode BOM (Byte Order Mark)")
@@ -18,11 +21,18 @@ fn expected_unicode_bom_diagnostic(span: Span) -> OxcDiagnostic {
1821
.with_label(span)
1922
}
2023

24+
#[derive(Debug, Default, Clone, Deserialize)]
25+
pub struct UnicodeBom(BomOptionType);
26+
2127
#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]
22-
#[serde(rename_all = "camelCase", default)]
23-
pub struct UnicodeBom {
24-
/// Configuration option to specify whether to require or disallow Unicode BOM.
25-
bom_option: BomOptionType,
28+
#[serde(rename_all = "kebab-case")]
29+
enum BomOptionType {
30+
/// Always require a Unicode BOM (Byte Order Mark) at the beginning of the file.
31+
Always,
32+
/// Never allow a Unicode BOM (Byte Order Mark) at the beginning of the file.
33+
/// This is the default option.
34+
#[default]
35+
Never,
2636
}
2737

2838
declare_oxc_lint!(
@@ -48,63 +58,43 @@ declare_oxc_lint!(
4858
eslint,
4959
restriction,
5060
fix,
51-
config = UnicodeBom,
61+
config = BomOptionType,
5262
);
5363

5464
impl Rule for UnicodeBom {
5565
fn from_configuration(value: serde_json::Value) -> Self {
56-
let obj = value.get(0);
57-
58-
Self {
59-
bom_option: obj
60-
.and_then(serde_json::Value::as_str)
61-
.map(BomOptionType::from)
62-
.unwrap_or_default(),
63-
}
66+
serde_json::from_value::<DefaultRuleConfig<UnicodeBom>>(value)
67+
.unwrap_or_default()
68+
.into_inner()
6469
}
6570

6671
fn run_once(&self, ctx: &LintContext) {
6772
let source = ctx.source_text();
6873
let has_bomb = source.starts_with('');
6974

70-
if has_bomb && matches!(self.bom_option, BomOptionType::Never) {
75+
if has_bomb && matches!(self.0, BomOptionType::Never) {
7176
ctx.diagnostic_with_fix(unexpected_unicode_bom_diagnostic(SPAN), |fixer| {
7277
fixer.delete_range(Span::new(0, 3))
7378
});
7479
}
7580

76-
if !has_bomb && matches!(self.bom_option, BomOptionType::Always) {
81+
if !has_bomb && matches!(self.0, BomOptionType::Always) {
7782
ctx.diagnostic_with_fix(expected_unicode_bom_diagnostic(SPAN), |fixer| {
7883
fixer.replace(SPAN, "")
7984
});
8085
}
8186
}
8287
}
8388

84-
#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]
85-
#[serde(rename_all = "kebab-case")]
86-
enum BomOptionType {
87-
Always,
88-
#[default]
89-
Never,
90-
}
91-
92-
impl BomOptionType {
93-
pub fn from(raw: &str) -> Self {
94-
match raw {
95-
"always" => Self::Always,
96-
_ => Self::Never,
97-
}
98-
}
99-
}
100-
10189
#[test]
10290
fn test() {
10391
use crate::tester::Tester;
10492

10593
let pass = vec![
10694
(" var a = 123;", Some(serde_json::json!(["always"]))),
10795
("var a = 123;", Some(serde_json::json!(["never"]))),
96+
// Ensure default config works
97+
("var a = 123;", None),
10898
("var a = 123; ", Some(serde_json::json!(["never"]))),
10999
];
110100

0 commit comments

Comments
 (0)