{"record":{"id":"09b1d0c357ea673e","repo":"oxc-project/oxc","slug":"prefer-math-abs-x-over-alternatives","errorCode":null,"errorMessage":"Prefer `Math.abs(x)` over alternatives","messagePattern":"Prefer `Math\\.abs\\(x\\)` over alternatives","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs","lineNumber":15,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, BinaryExpression, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse oxc_syntax::operator::BinaryOperator;\n\nuse crate::{\n    AstNode, ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_expression,\n};\n\nfn prefer_math_abs(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `Math.abs(x)` over alternatives\").with_label(span)\n}\n\nfn prefer_math_hypot(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `Math.hypot(…)` over alternatives\").with_label(span)\n}\n\nfn prefer_math_log_n(span: Span, good_method: &str, bad_method: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Prefer `Math.{good_method}(x)` over `{bad_method}`\"))\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferModernMathApis;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Checks for usage of legacy patterns for mathematical operations.","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs#L1-L33","documentation":"Diagnostic from the unicorn/prefer-modern-math-apis lint rule. It fires when an absolute value is computed manually, e.g. `x < 0 ? -x : x` or `Math.sqrt(x * x)`; `Math.abs(x)` is the direct, correct, and faster equivalent. The flagged input is the alternative expression on the given span. It is a lint suggestion, not a runtime error.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs:15 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Replace the manual negation/conditional with `Math.abs(x)`","Apply the rule's auto-fix","Verify edge cases (`-0`, `Infinity`, `NaN`) still behave as expected"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}