Skip to content

Commit

Permalink
prefer-math-min-max: ignore BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
chirokas committed Oct 4, 2024
1 parent 2b469be commit 5c21181
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rules/prefer-math-min-max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const {isBigIntLiteral, isCallExpression} = require('./ast/index.js');
const {fixSpaceAroundKeyword} = require('./fix/index.js');

const MESSAGE_ID = 'prefer-math-min-max';
Expand All @@ -17,6 +18,21 @@ const create = context => ({
}

const {operator, left, right} = test;

const hasBigInt = [left, right, alternate, consequent].some(
node =>
isBigIntLiteral(node)
|| isCallExpression(node, {
name: 'BigInt',
argumentsLength: 1,
optional: false,
}),
);

if (hasBigInt) {
return;
}

const [leftText, rightText, alternateText, consequentText] = [left, right, alternate, consequent].map(node => context.sourceCode.getText(node));

const isGreaterOrEqual = operator === '>' || operator === '>=';
Expand Down
6 changes: 6 additions & 0 deletions test/prefer-math-min-max.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ test.snapshot({
'height > 10 ? height : 20',
'height > 50 ? Math.min(50, height) : height',
'foo ? foo : bar',

// BigInt
'10n > 5n ? 10n : 5n;',
'10n > 5 ? 10n : 5;',
'bigN > 50n ? bigN : 50n;',
'BigInt(10) > BigInt(5) ? BigInt(10) : BigInt(5);',
],
invalid: [
// Prefer `Math.min()`
Expand Down

0 comments on commit 5c21181

Please sign in to comment.