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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 8 additions & 13 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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;

0 commit comments

Comments
 (0)