Skip to content

Commit e605222

Browse files
committed
fix(linter/no-useless-undefined): correctly respect checkArguments option (#14369)
The rule was not correctly parsing the configuration from .oxlintrc.json. When rules are configured with options like: { "rules": { "unicorn/no-useless-undefined": ["error", { "checkArguments": false }] } } The config system passes the options as an array containing all elements after the severity: [{ "checkArguments": false }] The from_configuration method was trying to access properties directly on the array instead of getting the first element. This fix uses value.get(0) to extract the options object from the array, matching the pattern used by all other rules in the codebase. Also updated the test configuration helpers to use the correct array format, consistent with other rules' tests. Fixes #14368
1 parent f5c6acc commit e605222

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

crates/oxc_linter/src/rules/unicorn/no_useless_undefined.rs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,13 @@ fn is_has_function_return_type(node: &AstNode, ctx: &LintContext<'_>) -> bool {
140140

141141
impl Rule for NoUselessUndefined {
142142
fn from_configuration(value: serde_json::Value) -> Self {
143-
let check_arguments =
144-
value.get("checkArguments").and_then(serde_json::Value::as_bool).unwrap_or(true);
145-
let check_arrow_function_body = value
146-
.get("checkArrowFunctionBody")
143+
let config = value.get(0);
144+
let check_arguments = config
145+
.and_then(|c| c.get("checkArguments"))
146+
.and_then(serde_json::Value::as_bool)
147+
.unwrap_or(true);
148+
let check_arrow_function_body = config
149+
.and_then(|c| c.get("checkArrowFunctionBody"))
147150
.and_then(serde_json::Value::as_bool)
148151
.unwrap_or(true);
149152
Self { check_arguments, check_arrow_function_body }
@@ -321,9 +324,9 @@ impl Rule for NoUselessUndefined {
321324
#[test]
322325
fn test() {
323326
use crate::tester::Tester;
324-
let options_ignore_arguments = || Some(serde_json::json!({ "checkArguments": false }));
327+
let options_ignore_arguments = || Some(serde_json::json!([{ "checkArguments": false }]));
325328
let options_ignore_arrow_function_body =
326-
|| Some(serde_json::json!({"checkArrowFunctionBody": false}));
329+
|| Some(serde_json::json!([{ "checkArrowFunctionBody": false }]));
327330
let pass = vec![
328331
(r"function foo() {return;}", None),
329332
(r"const foo = () => {};", None),
@@ -382,6 +385,10 @@ fn test() {
382385
// `checkArguments: false`
383386
(r"foo(undefined, undefined);", options_ignore_arguments()),
384387
(r"foo.bind(undefined);", options_ignore_arguments()),
388+
(
389+
r"function run(name?: string) { return name; } run(undefined);",
390+
options_ignore_arguments(),
391+
),
385392
// `checkArrowFunctionBody: false`
386393
(r"const foo = () => undefined", options_ignore_arrow_function_body()),
387394
(r"const x = { a: undefined }", None),
@@ -674,3 +681,64 @@ fn test() {
674681
.expect_fix(fix)
675682
.test_and_snapshot();
676683
}
684+
685+
#[test]
686+
fn test_config_array_format() {
687+
use crate::tester::Tester;
688+
689+
let pass = vec![
690+
(r"foo(undefined);", Some(serde_json::json!([{ "checkArguments": false }]))),
691+
(
692+
r"const foo = () => undefined;",
693+
Some(serde_json::json!([{ "checkArrowFunctionBody": false }])),
694+
),
695+
];
696+
let fail = vec![
697+
(r"foo(undefined);", Some(serde_json::json!([{ "checkArguments": true }]))),
698+
(
699+
r"const foo = () => undefined;",
700+
Some(serde_json::json!([{ "checkArrowFunctionBody": true }])),
701+
),
702+
];
703+
let fix = vec![
704+
(r"foo(undefined);", r"foo();", Some(serde_json::json!([{ "checkArguments": true }]))),
705+
(
706+
r"const foo = () => undefined;",
707+
r"const foo = () => {};",
708+
Some(serde_json::json!([{ "checkArrowFunctionBody": true }])),
709+
),
710+
];
711+
712+
Tester::new(NoUselessUndefined::NAME, NoUselessUndefined::PLUGIN, pass, fail)
713+
.expect_fix(fix)
714+
.test();
715+
}
716+
717+
#[test]
718+
fn test_issue_14368() {
719+
use crate::tester::Tester;
720+
721+
let pass = vec![
722+
(
723+
r"function run(name) { return name; } run(undefined);",
724+
Some(serde_json::json!([{ "checkArguments": false }])),
725+
),
726+
(
727+
r"function run(name?: string) { return name; } run(undefined);",
728+
Some(serde_json::json!([{ "checkArguments": false }])),
729+
),
730+
];
731+
let fail = vec![(
732+
r"function run(name) { return name; } run(undefined);",
733+
Some(serde_json::json!([{ "checkArguments": true }])),
734+
)];
735+
let fix = vec![(
736+
r"function run(name) { return name; } run(undefined);",
737+
r"function run(name) { return name; } run();",
738+
Some(serde_json::json!([{ "checkArguments": true }])),
739+
)];
740+
741+
Tester::new(NoUselessUndefined::NAME, NoUselessUndefined::PLUGIN, pass, fail)
742+
.expect_fix(fix)
743+
.test();
744+
}

0 commit comments

Comments
 (0)