Merge pull request #33300 from JoshuaKGoldberg/too-large-integer-bigint-codefix

Added codefix for numeric literals >= 2 ** 53
This commit is contained in:
Orta
2019-09-11 22:53:19 +02:00
committed by GitHub
5 changed files with 143 additions and 0 deletions

View 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));
}
}

View File

@@ -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",