Skip to content

Commit

Permalink
fix(eslint/radix): detect yield Number.parseInt variant (#4110)
Browse files Browse the repository at this point in the history
The eslint rule perfer-numeric-literals has a test for the yield variant
which the radix testsuite did not have. See
#4109
  • Loading branch information
jelly authored Jul 8, 2024
1 parent a873522 commit ed4c54c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/oxc_linter/src/rules/eslint/radix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ impl Rule for Radix {
{
Self::check_arguments(self, call_expr, ctx);
}
} else if let Expression::ParenthesizedExpression(paren_expr) =
&member_expr.object
{
if let Expression::Identifier(ident) = &paren_expr.expression {
if ident.name == "Number"
&& member_expr.property.name == "parseInt"
&& ctx.symbols().get_symbol_id_from_name("Number").is_none()
{
Self::check_arguments(self, call_expr, ctx);
}
}
}
}
Expression::ChainExpression(chain_expr) => {
Expand Down Expand Up @@ -192,6 +203,7 @@ fn test() {
("var Number; Number.parseInt(foo, 10);", Some(json!(["as-needed"]))),
// ("/* globals parseInt:off */ parseInt(foo);", Some(json!(["always"]))),
// ("Number.parseInt(foo, 10);", Some(json!(["as-needed"]))), // { globals: { Number: "off" } }
(r#"function *f(){ yield(Number).parseInt("10", foo) }"#, None), // { "ecmaVersion": 6 },
];

let fail = vec![
Expand Down Expand Up @@ -220,6 +232,7 @@ fn test() {
(r#"Number.parseInt?.("10");"#, None),
(r#"Number?.parseInt("10");"#, None),
(r#"(Number?.parseInt)("10");"#, None),
("function *f(){ yield(Number).parseInt() }", None), // { "ecmaVersion": 6 },
];

Tester::new(Radix::NAME, pass, fail).test_and_snapshot();
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/snapshots/radix.snap
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,9 @@ source: crates/oxc_linter/src/tester.rs
1 │ (Number?.parseInt)("10");
· ────────────────────────
╰────

eslint(radix): Missing parameters.
╭─[radix.tsx:1:21]
1function *f(){ yield(Number).parseInt() }
· ───────────────────
╰────

0 comments on commit ed4c54c

Please sign in to comment.