Skip to content

Commit 8b7c5fc

Browse files
authoredNov 19, 2024··
prefer-math-min-max: Ignore BigInt (#2467)
1 parent f862e0c commit 8b7c5fc

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
 

‎rules/prefer-math-min-max.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const {isBigIntLiteral, isCallExpression} = require('./ast/index.js');
23
const {fixSpaceAroundKeyword} = require('./fix/index.js');
34

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

1920
const {operator, left, right} = test;
21+
22+
const hasBigInt = [left, right].some(
23+
node =>
24+
isBigIntLiteral(node)
25+
|| isCallExpression(node, {
26+
name: 'BigInt',
27+
argumentsLength: 1,
28+
optional: false,
29+
}),
30+
);
31+
32+
if (hasBigInt) {
33+
return;
34+
}
35+
2036
const [leftText, rightText, alternateText, consequentText] = [left, right, alternate, consequent].map(node => context.sourceCode.getText(node));
2137

2238
const isGreaterOrEqual = operator === '>' || operator === '>=';

‎test/prefer-math-min-max.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ test.snapshot({
88
'height > 10 ? height : 20',
99
'height > 50 ? Math.min(50, height) : height',
1010
'foo ? foo : bar',
11+
12+
// Ignore bigint
13+
'foo > 10n ? 10n : foo',
14+
'foo > BigInt(10) ? BigInt(10) : foo',
1115
],
1216
invalid: [
1317
// Prefer `Math.min()`

0 commit comments

Comments
 (0)
Please sign in to comment.