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
38 changes: 28 additions & 10 deletions rules/prefer-bigint-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ const canUseNumericLiteralRaw = (value, nodeRaw) => {

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 valueNode.operator === '+'
? {value: valueNode.argument.value, raw: valueNode.argument.raw, isPlusSignUnary: true}
: {value: -valueNode.argument.value, raw: `-${valueNode.argument.raw}`, isPlusSignUnary: false};
}

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

function getReplacement(valueNode) {
Expand All @@ -52,14 +54,17 @@ function getReplacement(valueNode) {
}

// BigInt("+1") -> 1n
if (raw[0] === '+') {
raw = raw.slice(1);
const plusSignIndex = raw.indexOf('+');

if (plusSignIndex !== -1) {
raw = raw.slice(0, plusSignIndex) + raw.slice(plusSignIndex + 1);
return {shouldUseSuggestion: true, text: `${raw.trimEnd()}n`};
}

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

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

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

const shouldUseSuggestion = !canUseNumericLiteralRaw(value, raw);
const shouldUseSuggestion = isPlusSignUnary ? true : !canUseNumericLiteralRaw(value, raw);
const text = shouldUseSuggestion ? `${bigint}n` : `${raw}n`;
return {shouldUseSuggestion, text};
}
Expand All @@ -94,15 +99,28 @@ const create = context => ({
return;
}

let {shouldUseSuggestion, text} = replacement;
let nodeToReplace = callExpression;

const {parent} = callExpression;

// -BigInt(-1) -> 1n
if (
parent.type === 'UnaryExpression'
&& parent.operator === '-'
&& text.startsWith('-')
) {
nodeToReplace = parent;
text = text.slice(1);
}

const problem = {
node: callExpression,
node: nodeToReplace,
messageId: MESSAGE_ID_ERROR,
};

const {shouldUseSuggestion, text} = replacement;

/** @param {import('eslint').Rule.RuleFixer} fixer */
const fix = fixer => fixer.replaceText(callExpression, text);
const fix = fixer => fixer.replaceText(nodeToReplace, text);

if (shouldUseSuggestion || context.sourceCode.getCommentsInside(callExpression).length > 0) {
problem.suggest = [
Expand Down
8 changes: 8 additions & 0 deletions test/prefer-bigint-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ test.snapshot({
String.raw`BigInt("\u{31}")`,
'BigInt(!1)',
'BigInt(~1)',
'BigInt("++1")',
'BigInt("+ 1")',
],
invalid: [
'BigInt("0")',
Expand Down Expand Up @@ -58,5 +60,11 @@ test.snapshot({
'BigInt("+1")',
'BigInt(-1)',
'BigInt(+1)',
'-BigInt(-1)',
'-BigInt("-1")',
'-BigInt(1)',
'-BigInt("1")',
'BigInt(" +1 ")',
'BigInt(" -1 ")',
],
});
136 changes: 128 additions & 8 deletions test/snapshots/prefer-bigint-literals.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,15 @@ Generated by [AVA](https://avajs.dev).
1 | BigInt("+1")␊
`

> Output

`␊
1 | 1n␊
`

> Error 1/1

`␊
> 1 | BigInt("+1")␊
| ^^^^^^^^^^^^ Prefer using bigint literal over \`BigInt(…)\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace with \`1n\`.␊
1 | 1n␊
`

## invalid(24): BigInt(-1)
Expand Down Expand Up @@ -496,6 +494,25 @@ Generated by [AVA](https://avajs.dev).
1 | BigInt(+1)␊
`

> Error 1/1

`␊
> 1 | BigInt(+1)␊
| ^^^^^^^^^^ Prefer using bigint literal over \`BigInt(…)\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace with \`1n\`.␊
1 | 1n␊
`

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

> Input

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

> Output

`␊
Expand All @@ -505,6 +522,109 @@ Generated by [AVA](https://avajs.dev).
> Error 1/1

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

## invalid(27): -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(…)\`.␊
`

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

> Input

`␊
1 | -BigInt(1)␊
`

> Output

`␊
1 | -1n␊
`

> Error 1/1

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

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

> Input

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

> Output

`␊
1 | -1n␊
`

> Error 1/1

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

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

> Input

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

> Error 1/1

`␊
> 1 | BigInt(" +1 ")␊
| ^^^^^^^^^^^^^^ Prefer using bigint literal over \`BigInt(…)\`.␊
--------------------------------------------------------------------------------␊
Suggestion 1/1: Replace with \` 1n\`.␊
1 | 1n␊
`

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

> Input

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

> Output

`␊
1 | -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.