Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions rules/prefer-bigint-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ const messages = {
[MESSAGE_ID_SUGGESTION]: 'Replace with {{replacement}}.',
};

const canUseNumericLiteralRaw = numericLiteral => {
const raw = numericLiteral.raw.replaceAll('_', '').toLowerCase();
const canUseNumericLiteralRaw = (value, nodeRaw) => {
const raw = nodeRaw.replaceAll('_', '').toLowerCase();

if (raw.includes('.')) {
return false;
}

const {value} = numericLiteral;

for (const {prefix, base} of [
{prefix: '0b', base: 2},
{prefix: '0o', base: 8},
Expand All @@ -36,19 +34,32 @@ const canUseNumericLiteralRaw = numericLiteral => {
return raw === String(value);
};

function getValueOfNode(valueNode) {
if (valueNode.type === 'UnaryExpression' && (valueNode.operator === '+' || valueNode.operator === '-')) {
return valueNode.operator === '+' ? {value: valueNode.argument.value, raw: valueNode.argument.raw} : {value: -valueNode.argument.value, raw: `-${valueNode.argument.raw}`};
}

return {value: valueNode.value, raw: valueNode.raw};
}

function getReplacement(valueNode) {
if (isStringLiteral(valueNode)) {
const raw = valueNode.raw.slice(1, -1);
let raw = valueNode.raw.slice(1, -1);
try {
BigInt(raw);
} catch {
return;
}

// BigInt("+1") -> 1n
if (raw[0] === '+') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike "-1", "+1" probably a mistake, let's use suggestion instead of autofix. Same for UnaryExpression

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced with suggestion, also fixed logic for cases with spaces BigInt(" +1 ")

raw = raw.slice(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add test for BigInt("++1")?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And BigInt("+ 1")

Copy link
Collaborator

@fisker fisker Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BigInt(" -1 ")
BigInt(" +1 ")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests:
BigInt("++1") - valid
BigInt("+ 1") - valid

BigInt(" -1 ") - invalid
BigInt(" +1 ") - invalid with suggestion

}

return {shouldUseSuggestion: false, text: `${raw.trimEnd()}n`};
}

const {value, raw} = valueNode;
const {value, raw} = getValueOfNode(valueNode);

if (!Number.isInteger(value)) {
return;
Expand All @@ -61,7 +72,7 @@ function getReplacement(valueNode) {
return;
}

const shouldUseSuggestion = !canUseNumericLiteralRaw(valueNode);
const shouldUseSuggestion = !canUseNumericLiteralRaw(value, raw);
const text = shouldUseSuggestion ? `${bigint}n` : `${raw}n`;
return {shouldUseSuggestion, text};
}
Expand Down
6 changes: 6 additions & 0 deletions test/prefer-bigint-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ test.snapshot({
'BigInt("1_2")',
'BigInt("1\\\n2")',
String.raw`BigInt("\u{31}")`,
'BigInt(!1)',
'BigInt(~1)',
],
invalid: [
'BigInt("0")',
Expand Down Expand Up @@ -52,5 +54,9 @@ test.snapshot({
'BigInt(1e2)',
'BigInt(/* comment */1)',
`BigInt(${'9'.repeat(100)})`,
'BigInt("-1")',
'BigInt("+1")',
'BigInt(-1)',
'BigInt(+1)',
],
});
84 changes: 84 additions & 0 deletions test/snapshots/prefer-bigint-literals.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,87 @@ Generated by [AVA](https://avajs.dev).
Suggestion 1/1: Replace with a bigint literal.␊
1 | 10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104n␊
`

## invalid(22): BigInt("-1")

> Input

`␊
1 | BigInt("-1")␊
`

> Output

`␊
1 | -1n␊
`

> Error 1/1

`␊
> 1 | BigInt("-1")␊
| ^^^^^^^^^^^^ Prefer using bigint literal over \`BigInt(…)\`.␊
`

## invalid(23): BigInt("+1")

> Input

`␊
1 | BigInt("+1")␊
`

> Output

`␊
1 | 1n␊
`

> Error 1/1

`␊
> 1 | BigInt("+1")␊
| ^^^^^^^^^^^^ Prefer using bigint literal over \`BigInt(…)\`.␊
`

## invalid(24): BigInt(-1)

> Input

`␊
1 | BigInt(-1)␊
`

> Output

`␊
1 | -1n␊
`

> Error 1/1

`␊
> 1 | BigInt(-1)␊
| ^^^^^^^^^^ Prefer using bigint literal over \`BigInt(…)\`.␊
`

## invalid(25): BigInt(+1)

> Input

`␊
1 | BigInt(+1)␊
`

> Output

`␊
1 | 1n␊
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of 1n, better fix to -(-1n)

`

> Error 1/1

`␊
> 1 | BigInt(+1)␊
| ^^^^^^^^^^ Prefer using bigint literal over \`BigInt(…)\`.␊
`
Binary file modified test/snapshots/prefer-bigint-literals.js.snap
Binary file not shown.
Loading