Skip to content

Commit

Permalink
perf(linter): use cow_replace instead of replace (#5643)
Browse files Browse the repository at this point in the history
Related to #5586
  • Loading branch information
shulaoda committed Sep 9, 2024
1 parent e52d006 commit bfe9186
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;

use cow_utils::CowUtils;
use oxc_ast::{ast::NumericLiteral, AstKind};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
Expand Down Expand Up @@ -182,16 +183,9 @@ impl<'a> RawNum<'a> {
}

impl NoLossOfPrecision {
fn get_raw<'a>(node: &'a NumericLiteral) -> Cow<'a, str> {
if node.raw.contains('_') {
Cow::Owned(node.raw.replace('_', ""))
} else {
Cow::Borrowed(node.raw)
}
}

fn not_base_ten_loses_precision(node: &'_ NumericLiteral) -> bool {
let raw = Self::get_raw(node).to_uppercase();
let raw = node.raw.cow_replace('_', "");
let raw = raw.cow_to_uppercase();
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
// AST always store number as f64, need a cast to format in bin/oct/hex
let value = node.value as u64;
Expand All @@ -206,7 +200,7 @@ impl NoLossOfPrecision {
}

fn base_ten_loses_precision(node: &'_ NumericLiteral) -> bool {
let raw = Self::get_raw(node);
let raw = node.raw.cow_replace('_', "");
let Some(raw) = Self::normalize(&raw) else {
return true;
};
Expand Down

0 comments on commit bfe9186

Please sign in to comment.