Skip to content

Commit ff7ef4a

Browse files
author
Andy
authored
Add fixName property to CodeFixAction (#23350)
1 parent d4a166d commit ff7ef4a

30 files changed

+65
-51
lines changed

src/server/client.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ namespace ts.server {
558558
const request = this.processRequest<protocol.CodeFixRequest>(CommandNames.GetCodeFixes, args);
559559
const response = this.processResponse<protocol.CodeFixResponse>(request);
560560

561-
return response.body.map(({ description, changes, fixId, fixAllDescription }) => ({ description, changes: this.convertChanges(changes, file), fixId, fixAllDescription }));
561+
return response.body.map<CodeFixAction>(({ fixName, description, changes, commands, fixId, fixAllDescription }) =>
562+
({ fixName, description, changes: this.convertChanges(changes, file), commands: commands as CodeActionCommand[], fixId, fixAllDescription }));
562563
}
563564

564565
getCombinedCodeFix = notImplemented;

src/server/protocol.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,8 @@ namespace ts.server.protocol {
16981698
}
16991699

17001700
export interface CodeFixAction extends CodeAction {
1701+
/** Short name to identify the fix, for use by telemetry. */
1702+
fixName: string;
17011703
/**
17021704
* If present, one may call 'getCombinedCodeFix' with this fixId.
17031705
* This may be omitted to indicate that the code fix can't be applied in a group.

src/server/session.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ namespace ts.server {
16681668
}
16691669
}
16701670

1671-
private getCodeFixes(args: protocol.CodeFixRequestArgs, simplifiedResult: boolean): ReadonlyArray<protocol.CodeAction> | ReadonlyArray<CodeAction> {
1671+
private getCodeFixes(args: protocol.CodeFixRequestArgs, simplifiedResult: boolean): ReadonlyArray<protocol.CodeFixAction> | ReadonlyArray<CodeFixAction> {
16721672
if (args.errorCodes.length === 0) {
16731673
return undefined;
16741674
}
@@ -1678,15 +1678,7 @@ namespace ts.server {
16781678
const { startPosition, endPosition } = this.getStartAndEndPosition(args, scriptInfo);
16791679

16801680
const codeActions = project.getLanguageService().getCodeFixesAtPosition(file, startPosition, endPosition, args.errorCodes, this.getFormatOptions(file), this.getPreferences(file));
1681-
if (!codeActions) {
1682-
return undefined;
1683-
}
1684-
if (simplifiedResult) {
1685-
return codeActions.map(codeAction => this.mapCodeAction(project, codeAction));
1686-
}
1687-
else {
1688-
return codeActions;
1689-
}
1681+
return simplifiedResult ? codeActions.map(codeAction => this.mapCodeFixAction(project, codeAction)) : codeActions;
16901682
}
16911683

16921684
private getCombinedCodeFix({ scope, fixId }: protocol.GetCombinedCodeFixRequestArgs, simplifiedResult: boolean): protocol.CombinedCodeActions | CombinedCodeActions {
@@ -1734,9 +1726,12 @@ namespace ts.server {
17341726
return { startPosition, endPosition };
17351727
}
17361728

1737-
private mapCodeAction(project: Project, { description, changes: unmappedChanges, commands, fixId, fixAllDescription }: CodeFixAction): protocol.CodeFixAction {
1738-
const changes = unmappedChanges.map(change => this.mapTextChangesToCodeEditsUsingScriptinfo(change, project.getScriptInfoForNormalizedPath(toNormalizedPath(change.fileName))));
1739-
return { description, changes, commands, fixId, fixAllDescription };
1729+
private mapCodeAction(project: Project, { description, changes, commands }: CodeAction): protocol.CodeAction {
1730+
return { description, changes: this.mapTextChangesToCodeEdits(project, changes), commands };
1731+
}
1732+
1733+
private mapCodeFixAction(project: Project, { fixName, description, changes, commands, fixId, fixAllDescription }: CodeFixAction): protocol.CodeFixAction {
1734+
return { fixName, description, changes: this.mapTextChangesToCodeEdits(project, changes), commands, fixId, fixAllDescription };
17401735
}
17411736

17421737
private mapTextChangesToCodeEdits(project: Project, textChanges: ReadonlyArray<FileTextChanges>): protocol.FileCodeEdits[] {

src/services/codeFixProvider.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ namespace ts {
3434
: getLocaleSpecificMessage(diag);
3535
}
3636

37-
export function createCodeFixActionNoFixId(changes: FileTextChanges[], description: DiagnosticAndArguments) {
38-
return createCodeFixActionWorker(diagnosticToString(description), changes, /*fixId*/ undefined, /*fixAllDescription*/ undefined);
37+
export function createCodeFixActionNoFixId(fixName: string, changes: FileTextChanges[], description: DiagnosticAndArguments) {
38+
return createCodeFixActionWorker(fixName, diagnosticToString(description), changes, /*fixId*/ undefined, /*fixAllDescription*/ undefined);
3939
}
4040

41-
export function createCodeFixAction(changes: FileTextChanges[], description: DiagnosticAndArguments, fixId: {}, fixAllDescription: DiagnosticAndArguments, command?: CodeActionCommand): CodeFixAction {
42-
return createCodeFixActionWorker(diagnosticToString(description), changes, fixId, diagnosticToString(fixAllDescription), command);
41+
export function createCodeFixAction(fixName: string, changes: FileTextChanges[], description: DiagnosticAndArguments, fixId: {}, fixAllDescription: DiagnosticAndArguments, command?: CodeActionCommand): CodeFixAction {
42+
return createCodeFixActionWorker(fixName, diagnosticToString(description), changes, fixId, diagnosticToString(fixAllDescription), command);
4343
}
4444

45-
function createCodeFixActionWorker(description: string, changes: FileTextChanges[], fixId?: {}, fixAllDescription?: string, command?: CodeActionCommand): CodeFixAction {
46-
return { description, changes, fixId, fixAllDescription, commands: command ? [command] : undefined };
45+
function createCodeFixActionWorker(fixName: string, description: string, changes: FileTextChanges[], fixId?: {}, fixAllDescription?: string, command?: CodeActionCommand): CodeFixAction {
46+
return { fixName, description, changes, fixId, fixAllDescription, commands: command ? [command] : undefined };
4747
}
4848

4949
export function registerCodeFix(reg: CodeFixRegistration) {

src/services/codefixes/addMissingInvocationForDecorator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ts.codefix {
66
errorCodes,
77
getCodeActions: (context) => {
88
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
9-
return [createCodeFixAction(changes, Diagnostics.Call_decorator_expression, fixId, Diagnostics.Add_to_all_uncalled_decorators)];
9+
return [createCodeFixAction(fixId, changes, Diagnostics.Call_decorator_expression, fixId, Diagnostics.Add_to_all_uncalled_decorators)];
1010
},
1111
fixIds: [fixId],
1212
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file!, diag.start!)),

src/services/codefixes/annotateWithTypeFromJSDoc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ts.codefix {
88
const decl = getDeclaration(context.sourceFile, context.span.start);
99
if (!decl) return;
1010
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, decl));
11-
return [createCodeFixAction(changes, Diagnostics.Annotate_with_type_from_JSDoc, fixId, Diagnostics.Annotate_everything_with_types_from_JSDoc)];
11+
return [createCodeFixAction(fixId, changes, Diagnostics.Annotate_with_type_from_JSDoc, fixId, Diagnostics.Annotate_everything_with_types_from_JSDoc)];
1212
},
1313
fixIds: [fixId],
1414
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {

src/services/codefixes/convertFunctionToEs6Class.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace ts.codefix {
66
errorCodes,
77
getCodeActions(context: CodeFixContext) {
88
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, context.span.start, context.program.getTypeChecker()));
9-
return [createCodeFixAction(changes, Diagnostics.Convert_function_to_an_ES2015_class, fixId, Diagnostics.Convert_all_constructor_functions_to_classes)];
9+
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_function_to_an_ES2015_class, fixId, Diagnostics.Convert_all_constructor_functions_to_classes)];
1010
},
1111
fixIds: [fixId],
1212
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, err) => doChange(changes, err.file!, err.start, context.program.getTypeChecker())),

src/services/codefixes/convertToEs6Module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace ts.codefix {
1313
}
1414
});
1515
// No support for fix-all since this applies to the whole file at once anyway.
16-
return [createCodeFixActionNoFixId(changes, Diagnostics.Convert_to_ES6_module)];
16+
return [createCodeFixActionNoFixId("convertToEs6Module", changes, Diagnostics.Convert_to_ES6_module)];
1717
},
1818
});
1919

src/services/codefixes/correctQualifiedNameToIndexedAccessType.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ts.codefix {
99
if (!qualifiedName) return undefined;
1010
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, qualifiedName));
1111
const newText = `${qualifiedName.left.text}["${qualifiedName.right.text}"]`;
12-
return [createCodeFixAction(changes, [Diagnostics.Rewrite_as_the_indexed_access_type_0, newText], fixId, Diagnostics.Rewrite_all_as_indexed_access_types)];
12+
return [createCodeFixAction(fixId, changes, [Diagnostics.Rewrite_as_the_indexed_access_type_0, newText], fixId, Diagnostics.Rewrite_all_as_indexed_access_types)];
1313
},
1414
fixIds: [fixId],
1515
getAllCodeActions: (context) => codeFixAll(context, errorCodes, (changes, diag) => {

src/services/codefixes/disableJsDiagnostics.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @internal */
22
namespace ts.codefix {
3+
const fixName = "disableJsDiagnostics";
34
const fixId = "disableJsDiagnostics";
45
const errorCodes = mapDefined(Object.keys(Diagnostics) as ReadonlyArray<keyof typeof Diagnostics>, key => {
56
const diag = Diagnostics[key];
@@ -18,6 +19,7 @@ namespace ts.codefix {
1819
const fixes: CodeFixAction[] = [
1920
// fixId unnecessary because adding `// @ts-nocheck` even once will ignore every error in the file.
2021
createCodeFixActionNoFixId(
22+
fixName,
2123
[createFileTextChanges(sourceFile.fileName, [
2224
createTextChange(sourceFile.checkJsDirective
2325
? createTextSpanFromBounds(sourceFile.checkJsDirective.pos, sourceFile.checkJsDirective.end)
@@ -27,7 +29,7 @@ namespace ts.codefix {
2729
];
2830

2931
if (textChanges.isValidLocationToAddComment(sourceFile, span.start)) {
30-
fixes.unshift(createCodeFixAction(textChanges.ChangeTracker.with(context, t => makeChange(t, sourceFile, span.start)), Diagnostics.Ignore_this_error_message, fixId, Diagnostics.Add_ts_ignore_to_all_error_messages));
32+
fixes.unshift(createCodeFixAction(fixName, textChanges.ChangeTracker.with(context, t => makeChange(t, sourceFile, span.start)), Diagnostics.Ignore_this_error_message, fixId, Diagnostics.Add_ts_ignore_to_all_error_messages));
3133
}
3234

3335
return fixes;

src/services/codefixes/fixAddMissingMember.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @internal */
22
namespace ts.codefix {
3+
const fixName = "addMissingMember";
34
const errorCodes = [
45
Diagnostics.Property_0_does_not_exist_on_type_1.code,
56
Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2.code,
@@ -75,7 +76,7 @@ namespace ts.codefix {
7576
function getActionsForAddMissingMemberInJavaScriptFile(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean): CodeFixAction | undefined {
7677
const changes = textChanges.ChangeTracker.with(context, t => addMissingMemberInJs(t, classDeclarationSourceFile, classDeclaration, tokenName, makeStatic));
7778
return changes.length === 0 ? undefined
78-
: createCodeFixAction(changes, [makeStatic ? Diagnostics.Initialize_static_property_0 : Diagnostics.Initialize_property_0_in_the_constructor, tokenName], fixId, Diagnostics.Add_all_missing_members);
79+
: createCodeFixAction(fixName, changes, [makeStatic ? Diagnostics.Initialize_static_property_0 : Diagnostics.Initialize_property_0_in_the_constructor, tokenName], fixId, Diagnostics.Add_all_missing_members);
7980
}
8081

8182
function addMissingMemberInJs(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean): void {
@@ -120,7 +121,7 @@ namespace ts.codefix {
120121

121122
function createAddPropertyDeclarationAction(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, makeStatic: boolean, tokenName: string, typeNode: TypeNode): CodeFixAction {
122123
const changes = textChanges.ChangeTracker.with(context, t => addPropertyDeclaration(t, classDeclarationSourceFile, classDeclaration, tokenName, typeNode, makeStatic));
123-
return createCodeFixAction(changes, [makeStatic ? Diagnostics.Declare_static_property_0 : Diagnostics.Declare_property_0, tokenName], fixId, Diagnostics.Add_all_missing_members);
124+
return createCodeFixAction(fixName, changes, [makeStatic ? Diagnostics.Declare_static_property_0 : Diagnostics.Declare_property_0, tokenName], fixId, Diagnostics.Add_all_missing_members);
124125
}
125126

126127
function addPropertyDeclaration(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, typeNode: TypeNode, makeStatic: boolean): void {
@@ -153,7 +154,7 @@ namespace ts.codefix {
153154

154155
const changes = textChanges.ChangeTracker.with(context, t => t.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, indexSignature));
155156
// No fixId here because code-fix-all currently only works on adding individual named properties.
156-
return createCodeFixActionNoFixId(changes, [Diagnostics.Add_index_signature_for_property_0, tokenName]);
157+
return createCodeFixActionNoFixId(fixName, changes, [Diagnostics.Add_index_signature_for_property_0, tokenName]);
157158
}
158159

159160
function getActionForMethodDeclaration(
@@ -167,7 +168,7 @@ namespace ts.codefix {
167168
preferences: UserPreferences,
168169
): CodeFixAction | undefined {
169170
const changes = textChanges.ChangeTracker.with(context, t => addMethodDeclaration(t, classDeclarationSourceFile, classDeclaration, token, callExpression, makeStatic, inJs, preferences));
170-
return createCodeFixAction(changes, [makeStatic ? Diagnostics.Declare_static_method_0 : Diagnostics.Declare_method_0, token.text], fixId, Diagnostics.Add_all_missing_members);
171+
return createCodeFixAction(fixName, changes, [makeStatic ? Diagnostics.Declare_static_method_0 : Diagnostics.Declare_method_0, token.text], fixId, Diagnostics.Add_all_missing_members);
171172
}
172173

173174
function addMethodDeclaration(

src/services/codefixes/fixAwaitInSyncFunction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ts.codefix {
1212
const nodes = getNodes(sourceFile, span.start);
1313
if (!nodes) return undefined;
1414
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, nodes));
15-
return [createCodeFixAction(changes, Diagnostics.Add_async_modifier_to_containing_function, fixId, Diagnostics.Add_all_missing_async_modifiers)];
15+
return [createCodeFixAction(fixId, changes, Diagnostics.Add_async_modifier_to_containing_function, fixId, Diagnostics.Add_all_missing_async_modifiers)];
1616
},
1717
fixIds: [fixId],
1818
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {

src/services/codefixes/fixCannotFindModule.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ts.codefix {
88
const { host, sourceFile, span: { start } } = context;
99
const packageName = getTypesPackageNameToInstall(host, sourceFile, start);
1010
return packageName === undefined ? []
11-
: [createCodeFixAction(/*changes*/ [], [Diagnostics.Install_0, packageName], fixId, Diagnostics.Install_all_missing_types_packages, getCommand(sourceFile.fileName, packageName))];
11+
: [createCodeFixAction(fixId, /*changes*/ [], [Diagnostics.Install_0, packageName], fixId, Diagnostics.Install_all_missing_types_packages, getCommand(sourceFile.fileName, packageName))];
1212
},
1313
fixIds: [fixId],
1414
getAllCodeActions: context => codeFixAll(context, errorCodes, (_, diag, commands) => {

src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace ts.codefix {
1111
const { program, sourceFile, span } = context;
1212
const changes = textChanges.ChangeTracker.with(context, t =>
1313
addMissingMembers(getClass(sourceFile, span.start), sourceFile, program.getTypeChecker(), t, context.preferences));
14-
return changes.length === 0 ? undefined : [createCodeFixAction(changes, Diagnostics.Implement_inherited_abstract_class, fixId, Diagnostics.Implement_all_inherited_abstract_classes)];
14+
return changes.length === 0 ? undefined : [createCodeFixAction(fixId, changes, Diagnostics.Implement_inherited_abstract_class, fixId, Diagnostics.Implement_all_inherited_abstract_classes)];
1515
},
1616
fixIds: [fixId],
1717
getAllCodeActions: context => {

src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace ts.codefix {
1111
const checker = program.getTypeChecker();
1212
return mapDefined<ExpressionWithTypeArguments, CodeFixAction>(getClassImplementsHeritageClauseElements(classDeclaration), implementedTypeNode => {
1313
const changes = textChanges.ChangeTracker.with(context, t => addMissingDeclarations(checker, implementedTypeNode, sourceFile, classDeclaration, t, context.preferences));
14-
return changes.length === 0 ? undefined : createCodeFixAction(changes, [Diagnostics.Implement_interface_0, implementedTypeNode.getText(sourceFile)], fixId, Diagnostics.Implement_all_unimplemented_interfaces);
14+
return changes.length === 0 ? undefined : createCodeFixAction(fixId, changes, [Diagnostics.Implement_interface_0, implementedTypeNode.getText(sourceFile)], fixId, Diagnostics.Implement_all_unimplemented_interfaces);
1515
});
1616
},
1717
fixIds: [fixId],

src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ts.codefix {
1010
if (!nodes) return undefined;
1111
const { constructor, superCall } = nodes;
1212
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, constructor, superCall));
13-
return [createCodeFixAction(changes, Diagnostics.Make_super_call_the_first_statement_in_the_constructor, fixId, Diagnostics.Make_all_super_calls_the_first_statement_in_their_constructor)];
13+
return [createCodeFixAction(fixId, changes, Diagnostics.Make_super_call_the_first_statement_in_the_constructor, fixId, Diagnostics.Make_all_super_calls_the_first_statement_in_their_constructor)];
1414
},
1515
fixIds: [fixId],
1616
getAllCodeActions(context) {

src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace ts.codefix {
88
const { sourceFile, span } = context;
99
const ctr = getNode(sourceFile, span.start);
1010
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, ctr));
11-
return [createCodeFixAction(changes, Diagnostics.Add_missing_super_call, fixId, Diagnostics.Add_all_missing_super_calls)];
11+
return [createCodeFixAction(fixId, changes, Diagnostics.Add_missing_super_call, fixId, Diagnostics.Add_all_missing_super_calls)];
1212
},
1313
fixIds: [fixId],
1414
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>

src/services/codefixes/fixExtendsInterfaceBecomesImplements.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ts.codefix {
1010
if (!nodes) return undefined;
1111
const { extendsToken, heritageClauses } = nodes;
1212
const changes = textChanges.ChangeTracker.with(context, t => doChanges(t, sourceFile, extendsToken, heritageClauses));
13-
return [createCodeFixAction(changes, Diagnostics.Change_extends_to_implements, fixId, Diagnostics.Change_all_extended_interfaces_to_implements)];
13+
return [createCodeFixAction(fixId, changes, Diagnostics.Change_extends_to_implements, fixId, Diagnostics.Change_all_extended_interfaces_to_implements)];
1414
},
1515
fixIds: [fixId],
1616
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {

0 commit comments

Comments
 (0)