unicorn/prefer-unary-minus
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-unary-minus": "error",
},
},
];Prefer the unary minus operator over multiplying or dividing by -1.
const inverted = value * -1;const flipped = -1 * someNumber;const divided = value / -1;const inverted = -value;const flipped = -someNumber;const divided = -value;Examples from the ESLint documentation.
unicorn/prefer-unary-minus
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-unary-minus": "error",
},
},
];Oxlint does not have this rule.
nursery/useUnaryMinus
nursery · since 2.6.0
{
"linter": {
"rules": {
"nursery": {
"useUnaryMinus": "error"
}
}
}
}Prefer Math constants over their approximate numeric values.
Prefer putting the condition in the while statement.
Prefer Math.abs() over manual absolute value expressions and symmetric range checks.
Disallow confusing uses of Array#{splice,toSpliced}().
Prefer flat Math.min() and Math.max() calls over nested calls.
Disallow .map() and .filter() in for…of and for await…of loop headers.