Skip to content

Commit

Permalink
perf(linter): no-magic-numbers remove redudant checks in `is_array_…
Browse files Browse the repository at this point in the history
…index` (#6033)

- there can not be an `UnaryExpression` as a `parent_kind`, when the
`ast_kind` is `NumericLiteral`,
  `InternConfig::from` prevents it from happending
- `NumericLiteral` can not be negative or else they would be passed as
`UnaryExpression`
  • Loading branch information
Sysix authored Sep 24, 2024
1 parent 09a24cd commit f8464a3
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use oxc_ast::{
ast::{
AssignmentTarget, Expression, MemberExpression, UnaryExpression, VariableDeclarationKind,
},
ast::{AssignmentTarget, Expression, MemberExpression, VariableDeclarationKind},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
Expand Down Expand Up @@ -369,34 +367,28 @@ fn is_parse_int_radix(parent_parent_node: &AstNode<'_>) -> bool {
/// a[1e310] // (same as a["Infinity"])
/// ```
fn is_array_index<'a>(ast_kind: &AstKind<'a>, parent_kind: &AstKind<'a>) -> bool {
fn is_unanary_index(unary: &UnaryExpression) -> bool {
match &unary.argument {
match ast_kind {
AstKind::BigIntLiteral(_) => true,
AstKind::UnaryExpression(unary) => match &unary.argument {
Expression::BigIntLiteral(_) => true,
Expression::NumericLiteral(numeric)
if unary.operator == UnaryOperator::UnaryNegation =>
{
numeric.value == 0.0
}
_ => false,
}
}

match ast_kind {
AstKind::UnaryExpression(unary) => is_unanary_index(unary),
AstKind::BigIntLiteral(_) => true,
},
AstKind::NumericLiteral(numeric) => match parent_kind {
AstKind::MemberExpression(expression) => {
if let MemberExpression::ComputedMemberExpression(computed_expression) = expression
{
return computed_expression.expression.is_number_value(numeric.value)
&& numeric.value >= 0.0
&& numeric.value.fract() == 0.0
&& numeric.value < f64::from(u32::MAX);
}

false
}
AstKind::UnaryExpression(unary) => is_unanary_index(unary),
_ => false,
},
_ => false,
Expand Down

0 comments on commit f8464a3

Please sign in to comment.