|
| 1 | +import { |
| 2 | + isCallExpression, |
| 3 | + isStringLiteral, |
| 4 | +} from './ast/index.js'; |
| 5 | + |
| 6 | +const MESSAGE_ID_ERROR = 'prefer-bigint-literals/error'; |
| 7 | +const MESSAGE_ID_SUGGESTION = 'prefer-bigint-literals/suggestion'; |
| 8 | +const messages = { |
| 9 | + [MESSAGE_ID_ERROR]: 'Prefer using bigint literal over `BigInt(…)`.', |
| 10 | + [MESSAGE_ID_SUGGESTION]: 'Replace with {{replacement}}.', |
| 11 | +}; |
| 12 | + |
| 13 | +const canUseNumericLiteralRaw = numericLiteral => { |
| 14 | + const raw = numericLiteral.raw.replaceAll('_', '').toLowerCase(); |
| 15 | + |
| 16 | + if (raw.includes('.')) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + |
| 20 | + const {value} = numericLiteral; |
| 21 | + |
| 22 | + for (const {prefix, base} of [ |
| 23 | + {prefix: '0b', base: 2}, |
| 24 | + {prefix: '0o', base: 8}, |
| 25 | + {prefix: '0x', base: 16}, |
| 26 | + ]) { |
| 27 | + if (raw.startsWith(prefix)) { |
| 28 | + return raw.slice(2) === value.toString(base); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (raw.includes('e')) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + return raw === String(value); |
| 37 | +}; |
| 38 | + |
| 39 | +function getReplacement(valueNode) { |
| 40 | + if (isStringLiteral(valueNode)) { |
| 41 | + const raw = valueNode.raw.slice(1, -1); |
| 42 | + try { |
| 43 | + BigInt(raw); |
| 44 | + } catch { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + return {shouldUseSuggestion: false, text: `${raw.trimEnd()}n`}; |
| 49 | + } |
| 50 | + |
| 51 | + const {value, raw} = valueNode; |
| 52 | + |
| 53 | + if (!Number.isInteger(value)) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + let bigint; |
| 58 | + try { |
| 59 | + bigint = BigInt(value); |
| 60 | + } catch { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const shouldUseSuggestion = !canUseNumericLiteralRaw(valueNode); |
| 65 | + const text = shouldUseSuggestion ? `${bigint}n` : `${raw}n`; |
| 66 | + return {shouldUseSuggestion, text}; |
| 67 | +} |
| 68 | + |
| 69 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 70 | +const create = context => ({ |
| 71 | + CallExpression(callExpression) { |
| 72 | + if (!isCallExpression(callExpression, { |
| 73 | + name: 'BigInt', |
| 74 | + argumentsLength: 1, |
| 75 | + optional: false, |
| 76 | + })) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const [valueNode] = callExpression.arguments; |
| 81 | + const replacement = getReplacement(valueNode); |
| 82 | + if (!replacement) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const problem = { |
| 87 | + node: callExpression, |
| 88 | + messageId: MESSAGE_ID_ERROR, |
| 89 | + }; |
| 90 | + |
| 91 | + const {shouldUseSuggestion, text} = replacement; |
| 92 | + |
| 93 | + /** @param {import('eslint').Rule.RuleFixer} fixer */ |
| 94 | + const fix = fixer => fixer.replaceText(callExpression, text); |
| 95 | + |
| 96 | + if (shouldUseSuggestion || context.sourceCode.getCommentsInside(callExpression).length > 0) { |
| 97 | + problem.suggest = [ |
| 98 | + { |
| 99 | + messageId: MESSAGE_ID_SUGGESTION, |
| 100 | + data: { |
| 101 | + replacement: text.length < 20 ? `\`${text}\`` : 'a bigint literal', |
| 102 | + }, |
| 103 | + fix, |
| 104 | + }, |
| 105 | + ]; |
| 106 | + } else { |
| 107 | + problem.fix = fix; |
| 108 | + } |
| 109 | + |
| 110 | + return problem; |
| 111 | + }, |
| 112 | +}); |
| 113 | + |
| 114 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 115 | +const config = { |
| 116 | + create, |
| 117 | + meta: { |
| 118 | + type: 'suggestion', |
| 119 | + docs: { |
| 120 | + description: 'Prefer `BigInt` literals over the constructor.', |
| 121 | + recommended: true, |
| 122 | + }, |
| 123 | + fixable: 'code', |
| 124 | + hasSuggestions: true, |
| 125 | + messages, |
| 126 | + }, |
| 127 | +}; |
| 128 | + |
| 129 | +export default config; |
0 commit comments