Skip to content

Commit

Permalink
prefer-math-min-max: Ignore BigInt (#2467)
Browse files Browse the repository at this point in the history
  • Loading branch information
chirokas authored Nov 19, 2024
1 parent f862e0c commit 8b7c5fc
Show file tree
Hide file tree
Showing 2 changed files with 20 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].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
4 changes: 4 additions & 0 deletions test/prefer-math-min-max.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ test.snapshot({
'height > 10 ? height : 20',
'height > 50 ? Math.min(50, height) : height',
'foo ? foo : bar',

// Ignore bigint
'foo > 10n ? 10n : foo',
'foo > BigInt(10) ? BigInt(10) : foo',
],
invalid: [
// Prefer `Math.min()`
Expand Down

0 comments on commit 8b7c5fc

Please sign in to comment.