Skip to content

Add function names for code fixes. #46728

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

Merged
merged 4 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ts.codefix {
const errorCodes = [Diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first.code];
registerCodeFix({
errorCodes,
getCodeActions: (context) => {
getCodeActions: function getCodeActionsToAddConvertToUnknownForNonOverlappingTypes(context) {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
return [createCodeFixAction(fixId, changes, Diagnostics.Add_unknown_conversion_for_non_overlapping_types, fixId, Diagnostics.Add_unknown_to_all_conversions_of_non_overlapping_types)];
},
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addEmptyExportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ts.codefix {
Diagnostics.await_expressions_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module.code,
Diagnostics.for_await_loops_are_only_allowed_at_the_top_level_of_a_file_when_that_file_is_a_module_but_this_file_has_no_imports_or_exports_Consider_adding_an_empty_export_to_make_this_file_a_module.code,
],
getCodeActions: context => {
getCodeActions: function getCodeActionsToAddEmptyExportDeclaration(context) {
const { sourceFile } = context;
const changes = textChanges.ChangeTracker.with(context, changes => {
const exportDeclaration = factory.createExportDeclaration(
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addMissingAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ts.codefix {
registerCodeFix({
fixIds: [fixId],
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToAddMissingAsync(context) {
const { sourceFile, errorCode, cancellationToken, program, span } = context;
const diagnostic = find(program.getDiagnosticsProducingTypeChecker().getDiagnostics(sourceFile, cancellationToken), getIsMatchingAsyncError(span, errorCode));
const directSpan = diagnostic && diagnostic.relatedInformation && find(diagnostic.relatedInformation, r => r.code === Diagnostics.Did_you_mean_to_mark_this_function_as_async.code) as TextSpan | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addMissingAwait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace ts.codefix {
registerCodeFix({
fixIds: [fixId],
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToAddMissingAwait(context) {
const { sourceFile, errorCode, span, cancellationToken, program } = context;
const expression = getAwaitErrorSpanExpression(sourceFile, errorCode, span, cancellationToken, program);
if (!expression) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addMissingConst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: (context) => {
getCodeActions: function getCodeActionsToAddMissingConst(context) {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start, context.program));
if (changes.length > 0) {
return [createCodeFixAction(fixId, changes, Diagnostics.Add_const_to_unresolved_variable, fixId, Diagnostics.Add_const_to_all_unresolved_variables)];
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addMissingDeclareProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: (context) => {
getCodeActions: function getCodeActionsToAddMissingDeclareOnProperty(context) {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
if (changes.length > 0) {
return [createCodeFixAction(fixId, changes, Diagnostics.Prefix_with_declare, fixId, Diagnostics.Prefix_all_incorrect_property_declarations_with_declare)];
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addMissingInvocationForDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ts.codefix {
const errorCodes = [Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0.code];
registerCodeFix({
errorCodes,
getCodeActions: (context) => {
getCodeActions: function getCodeActionsToAddMissingInvocationForDecorator(context) {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
return [createCodeFixAction(fixId, changes, Diagnostics.Call_decorator_expression, fixId, Diagnostics.Add_to_all_uncalled_decorators)];
},
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addNameToNamelessParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ts.codefix {
const errorCodes = [Diagnostics.Parameter_has_a_name_but_no_type_Did_you_mean_0_Colon_1.code];
registerCodeFix({
errorCodes,
getCodeActions: (context) => {
getCodeActions: function getCodeActionsToAddNameToNamelessParameter(context) {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
return [createCodeFixAction(fixId, changes, Diagnostics.Add_parameter_name, fixId, Diagnostics.Add_names_to_all_parameters_without_names)];
},
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/convertConstToLet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToConvertConstToLet(context) {
const { sourceFile, span, program } = context;
const range = getConstTokenRange(sourceFile, span.start, program);
if (range === undefined) return;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/convertLiteralTypeToMappedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToConvertLiteralTypeToMappedType(context) {
const { sourceFile, span } = context;
const info = getInfo(sourceFile, span.start);
if (!info) {
Expand Down
5 changes: 2 additions & 3 deletions src/services/codefixes/convertToMappedObjectType.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* @internal */
namespace ts.codefix {
const fixIdAddMissingTypeof = "fixConvertToMappedObjectType";
const fixId = fixIdAddMissingTypeof;
const fixId = "fixConvertToMappedObjectType";
const errorCodes = [Diagnostics.An_index_signature_parameter_type_cannot_be_a_literal_type_or_generic_type_Consider_using_a_mapped_object_type_instead.code];

type FixableDeclaration = InterfaceDeclaration | TypeAliasDeclaration;

registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToConvertToMappedTypeObject(context) {
const { sourceFile, span } = context;
const info = getInfo(sourceFile, span.start);
if (!info) return undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/convertToTypeOnlyExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace ts.codefix {
const fixId = "convertToTypeOnlyExport";
registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToConvertToTypeOnlyExport(context) {
const changes = textChanges.ChangeTracker.with(context, t => fixSingleExportDeclaration(t, getExportSpecifierForDiagnosticSpan(context.span, context.sourceFile), context));
if (changes.length) {
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_to_type_only_export, fixId, Diagnostics.Convert_all_re_exported_types_to_type_only_exports)];
}
},
fixIds: [fixId],
getAllCodeActions: context => {
getAllCodeActions: function getAllCodeActionsToConvertToTypeOnlyExport(context) {
const fixedExportDeclarations = new Map<number, true>();
return codeFixAll(context, errorCodes, (changes, diag) => {
const exportSpecifier = getExportSpecifierForDiagnosticSpan(diag, context.sourceFile);
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/convertToTypeOnlyImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ts.codefix {
const fixId = "convertToTypeOnlyImport";
registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToConvertToTypeOnlyImport(context) {
const changes = textChanges.ChangeTracker.with(context, t => {
const importDeclaration = getImportDeclarationForDiagnosticSpan(context.span, context.sourceFile);
fixSingleImportDeclaration(t, importDeclaration, context);
Expand All @@ -14,7 +14,7 @@ namespace ts.codefix {
}
},
fixIds: [fixId],
getAllCodeActions: context => {
getAllCodeActions: function getAllCodeActionsToConvertToTypeOnlyImport(context) {
return codeFixAll(context, errorCodes, (changes, diag) => {
const importDeclaration = getImportDeclarationForDiagnosticSpan(diag, context.sourceFile);
fixSingleImportDeclaration(changes, importDeclaration, context);
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/disableJsDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions(context) {
getCodeActions: function getCodeActionsToDisableJsDiagnostics(context) {
const { sourceFile, program, span, host, formatContext } = context;

if (!isInJSFile(sourceFile) || !isCheckJsEnabledForFile(sourceFile, program.getCompilerOptions())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToAddMissingTypeof(context) {
const { sourceFile, span } = context;
const importType = getImportTypeNode(sourceFile, span.start);
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, importType));
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixAwaitInSyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace ts.codefix {
return [createCodeFixAction(fixId, changes, Diagnostics.Add_async_modifier_to_containing_function, fixId, Diagnostics.Add_all_missing_async_modifiers)];
},
fixIds: [fixId],
getAllCodeActions: context => {
getAllCodeActions: function getAllCodeActionsToFixAwaitInSyncFunction(context) {
const seen = new Map<number, true>();
return codeFixAll(context, errorCodes, (changes, diag) => {
const nodes = getNodes(diag.file, diag.start);
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixCannotFindModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ts.codefix {
];
registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToFixNotFoundModule(context) {
const { host, sourceFile, span: { start } } = context;
const packageName = tryGetImportedPackageName(sourceFile, start);
if (packageName === undefined) return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace ts.codefix {
const fixId = "fixClassDoesntImplementInheritedAbstractMember";
registerCodeFix({
errorCodes,
getCodeActions(context) {
getCodeActions: function getCodeActionsToFixClassNotImplementingInheritedMembers(context) {
const { sourceFile, span } = context;
const changes = textChanges.ChangeTracker.with(context, t =>
addMissingMembers(getClass(sourceFile, span.start), sourceFile, context, t, context.preferences));
return changes.length === 0 ? undefined : [createCodeFixAction(fixId, changes, Diagnostics.Implement_inherited_abstract_class, fixId, Diagnostics.Implement_all_inherited_abstract_classes)];
},
fixIds: [fixId],
getAllCodeActions: context => {
getAllCodeActions: function getAllCodeActionsToFixClassDoesntImplementInheritedAbstractMember(context) {
const seenClassDeclarations = new Map<number, true>();
return codeFixAll(context, errorCodes, (changes, diag) => {
const classDeclaration = getClass(diag.file, diag.start);
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixEnableExperimentalDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ts.codefix {
];
registerCodeFix({
errorCodes,
getCodeActions: (context) => {
getCodeActions: function getCodeActionsToEnableExperimentalDecorators(context) {
const { configFile } = context.program.getCompilerOptions();
if (configFile === undefined) {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixEnableJsxFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ts.codefix {
const errorCodes = [Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided.code];
registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToFixEnableJsxFlag(context) {
const { configFile } = context.program.getCompilerOptions();
if (configFile === undefined) {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixIncorrectNamedTupleSyntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToFixIncorrectNamedTupleSyntax(context) {
const { sourceFile, span } = context;
const namedTupleMember = getNamedTupleMember(sourceFile, span.start);
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, namedTupleMember));
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixModuleAndTargetOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ts.codefix {
Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher.code,
Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher.code,
],
getCodeActions: context => {
getCodeActions: function getCodeActionsToFixModuleAndTarget(context) {
const compilerOptions = context.program.getCompilerOptions();
const { configFile } = compilerOptions;
if (configFile === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixOverrideModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToFixOverrideModifierIssues(context) {
const { errorCode, span } = context;

const info = errorCodeFixIdMap[errorCode];
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixReturnTypeInAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace ts.codefix {
registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions: context => {
getCodeActions: function getCodeActionsToFixReturnTypeInAsyncFunction(context) {
const { sourceFile, program, span } = context;
const checker = program.getTypeChecker();
const info = getInfo(sourceFile, program.getTypeChecker(), span.start);
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/fixStrictClassInitialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ts.codefix {
const errorCodes = [Diagnostics.Property_0_has_no_initializer_and_is_not_definitely_assigned_in_the_constructor.code];
registerCodeFix({
errorCodes,
getCodeActions: (context) => {
getCodeActions: function getCodeActionsForStrictClassInitializationErrors(context) {
const propertyDeclaration = getPropertyDeclaration(context.sourceFile, context.span.start);
if (!propertyDeclaration) return;

Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/removeUnnecessaryAwait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: (context) => {
getCodeActions: function getCodeActionsToRemoveUnnecessaryAwait(context) {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span));
if (changes.length > 0) {
return [createCodeFixAction(fixId, changes, Diagnostics.Remove_unnecessary_await, fixId, Diagnostics.Remove_all_unnecessary_uses_of_await)];
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/returnValueCorrect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ts.codefix {
registerCodeFix({
errorCodes,
fixIds: [fixIdAddReturnStatement, fixRemoveBracesFromArrowFunctionBody, fixIdWrapTheBlockWithParen],
getCodeActions: context => {
getCodeActions: function getCodeActionsToCorrectReturnValue(context) {
const { program, sourceFile, span: { start }, errorCode } = context;
const info = getInfo(program.getTypeChecker(), sourceFile, start, errorCode);
if (!info) return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/splitTypeOnlyImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ts.codefix {
registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions: context => {
getCodeActions: function getCodeActionsToSplitTypeOnlyImport(context) {
const changes = textChanges.ChangeTracker.with(context, t => {
return splitTypeOnlyImport(t, getImportDeclaration(context.sourceFile, context.span), context);
});
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/useBigintLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ts.codefix {

registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToUseBigintLiteral(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)];
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/wrapJsxInFragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ts.codefix {
const errorCodes = [Diagnostics.JSX_expressions_must_have_one_parent_element.code];
registerCodeFix({
errorCodes,
getCodeActions: context => {
getCodeActions: function getCodeActionsToWrapJsxInFragment(context) {
const { sourceFile, span } = context;
const node = findNodeToFix(sourceFile, span.start);
if (!node) return undefined;
Expand Down