mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-18 06:17:19 -05:00
Merge pull request #33300 from JoshuaKGoldberg/too-large-integer-bigint-codefix
Added codefix for numeric literals >= 2 ** 53
This commit is contained in:
33
src/services/codefixes/useBigintLiteral.ts
Normal file
33
src/services/codefixes/useBigintLiteral.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/* @internal */
|
||||
namespace ts.codefix {
|
||||
const fixId = "useBigintLiteral";
|
||||
const errorCodes = [
|
||||
Diagnostics.Numeric_literals_with_absolute_values_equal_to_2_53_or_greater_are_too_large_to_be_represented_accurately_as_integers.code,
|
||||
];
|
||||
|
||||
registerCodeFix({
|
||||
errorCodes,
|
||||
getCodeActions: context => {
|
||||
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span));
|
||||
if (changes.length > 0) {
|
||||
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_to_a_bigint_numeric_literal, fixId, Diagnostics.Convert_all_to_bigint_numeric_literals)];
|
||||
}
|
||||
},
|
||||
fixIds: [fixId],
|
||||
getAllCodeActions: context => {
|
||||
return codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag));
|
||||
},
|
||||
});
|
||||
|
||||
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, span: TextSpan) {
|
||||
const numericLiteral = tryCast(getTokenAtPosition(sourceFile, span.start), isNumericLiteral);
|
||||
if (!numericLiteral) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We use .getText to overcome parser inaccuracies: https://github.com/microsoft/TypeScript/issues/33298
|
||||
const newText = numericLiteral.getText(sourceFile) + "n";
|
||||
|
||||
changeTracker.replaceNode(sourceFile, numericLiteral, createBigIntLiteral(newText));
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,7 @@
|
||||
"codefixes/fixStrictClassInitialization.ts",
|
||||
"codefixes/requireInTs.ts",
|
||||
"codefixes/useDefaultImport.ts",
|
||||
"codefixes/useBigintLiteral.ts",
|
||||
"codefixes/fixAddModuleReferTypeMissingTypeof.ts",
|
||||
"codefixes/convertToMappedObjectType.ts",
|
||||
"codefixes/removeUnnecessaryAwait.ts",
|
||||
|
||||
Reference in New Issue
Block a user