diff --git a/rules/prefer-math-min-max.js b/rules/prefer-math-min-max.js index fdad464fa4..52b4edb341 100644 --- a/rules/prefer-math-min-max.js +++ b/rules/prefer-math-min-max.js @@ -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'; @@ -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 === '>='; diff --git a/test/prefer-math-min-max.mjs b/test/prefer-math-min-max.mjs index 6da574dfbd..ab3f59df3e 100644 --- a/test/prefer-math-min-max.mjs +++ b/test/prefer-math-min-max.mjs @@ -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()`