mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-11-18 20:11:14 +00:00
Merge pull request #33300 from JoshuaKGoldberg/too-large-integer-bigint-codefix
Added codefix for numeric literals >= 2 ** 53
This commit is contained in:
5 files changed
+143
No files matched your search
@@ -33716,9 +33716,33 @@ namespace ts {
|
||||
return grammarErrorOnNode(withMinus ? node.parent : node, diagnosticMessage, literal);
|
||||
}
|
||||
}
|
||||
|
||||
// Realism (size) checking
|
||||
checkNumericLiteralValueSize(node);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkNumericLiteralValueSize(node: NumericLiteral) {
|
||||
// Scientific notation (e.g. 2e54 and 1e00000000010) can't be converted to bigint
|
||||
// Literals with 15 or fewer characters aren't long enough to reach past 2^53 - 1
|
||||
// Fractional numbers (e.g. 9000000000000000.001) are inherently imprecise anyway
|
||||
if (node.numericLiteralFlags & TokenFlags.Scientific || node.text.length <= 15 || node.text.indexOf(".") !== -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We can't rely on the runtime to accurately store and compare extremely large numeric values
|
||||
// Even for internal use, we use getTextOfNode: https://github.com/microsoft/TypeScript/issues/33298
|
||||
// Thus, if the runtime claims a too-large number is lower than Number.MAX_SAFE_INTEGER,
|
||||
// it's likely addition operations on it will fail too
|
||||
const apparentValue = +getTextOfNode(node);
|
||||
if (apparentValue <= 2 ** 53 - 1 && apparentValue + 1 > apparentValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
addErrorOrSuggestion(/*isError*/ false, createDiagnosticForNode(node, Diagnostics.Numeric_literals_with_absolute_values_equal_to_2_53_or_greater_are_too_large_to_be_represented_accurately_as_integers));
|
||||
}
|
||||
|
||||
function checkGrammarBigIntLiteral(node: BigIntLiteral): boolean {
|
||||
const literalType = isLiteralTypeNode(node.parent) ||
|
||||
isPrefixUnaryExpression(node.parent) && isLiteralTypeNode(node.parent.parent);
|
||||
|
||||
@@ -4643,6 +4643,10 @@
|
||||
"category": "Suggestion",
|
||||
"code": 80007
|
||||
},
|
||||
"Numeric literals with absolute values equal to 2^53 or greater are too large to be represented accurately as integers.": {
|
||||
"category": "Suggestion",
|
||||
"code": 80008
|
||||
},
|
||||
|
||||
"Add missing 'super()' call": {
|
||||
"category": "Message",
|
||||
@@ -5124,6 +5128,14 @@
|
||||
"category": "Message",
|
||||
"code": 95090
|
||||
},
|
||||
"Convert to a bigint numeric literal": {
|
||||
"category": "Message",
|
||||
"code": 95091
|
||||
},
|
||||
"Convert all to bigint numeric literals": {
|
||||
"category": "Message",
|
||||
"code": 95092
|
||||
},
|
||||
|
||||
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
|
||||
"category": "Error",
|
||||
|
||||
Reference in new issue
Block a user