-
-
Notifications
You must be signed in to change notification settings - Fork 419
fix (prefer-bigint-literals): fix add support for numbers/strings with operators
#2784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}, | ||
|
|
@@ -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] === '+') { | ||
| raw = raw.slice(1); | ||
|
||
| } | ||
|
|
||
| return {shouldUseSuggestion: false, text: `${raw.trimEnd()}n`}; | ||
| } | ||
|
|
||
| const {value, raw} = valueNode; | ||
| const {value, raw} = getValueOfNode(valueNode); | ||
|
|
||
| if (!Number.isInteger(value)) { | ||
| return; | ||
|
|
@@ -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}; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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␊ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
||
| ` | ||
|
|
||
| > Error 1/1 | ||
|
|
||
| `␊ | ||
| > 1 | BigInt(+1)␊ | ||
| | ^^^^^^^^^^ Prefer using bigint literal over \`BigInt(…)\`.␊ | ||
| ` | ||
There was a problem hiding this comment.
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 forUnaryExpressionThere was a problem hiding this comment.
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 ")