Skip to content

Commit b363240

Browse files
author
Andy
authored
Simplify registerCodeFIx (#23349)
1 parent a04e747 commit b363240

File tree

1 file changed

+5
-27
lines changed

1 file changed

+5
-27
lines changed

src/services/codeFixProvider.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace ts {
2424
}
2525

2626
export namespace codefix {
27-
const codeFixRegistrations: CodeFixRegistration[][] = [];
27+
const errorCodeToFixes = createMultiMap<CodeFixRegistration>();
2828
const fixIdToRegistration = createMap<CodeFixRegistration>();
2929

3030
type DiagnosticAndArguments = DiagnosticMessage | [DiagnosticMessage, string] | [DiagnosticMessage, string, string];
@@ -48,12 +48,7 @@ namespace ts {
4848

4949
export function registerCodeFix(reg: CodeFixRegistration) {
5050
for (const error of reg.errorCodes) {
51-
let registrations = codeFixRegistrations[error];
52-
if (!registrations) {
53-
registrations = [];
54-
codeFixRegistrations[error] = registrations;
55-
}
56-
registrations.push(reg);
51+
errorCodeToFixes.add(String(error), reg);
5752
}
5853
if (reg.fixIds) {
5954
for (const fixId of reg.fixIds) {
@@ -63,29 +58,12 @@ namespace ts {
6358
}
6459
}
6560

66-
export function getSupportedErrorCodes() {
67-
return Object.keys(codeFixRegistrations);
61+
export function getSupportedErrorCodes(): string[] {
62+
return arrayFrom(errorCodeToFixes.keys());
6863
}
6964

7065
export function getFixes(context: CodeFixContext): CodeFixAction[] {
71-
const fixes = codeFixRegistrations[context.errorCode];
72-
const allActions: CodeFixAction[] = [];
73-
74-
forEach(fixes, f => {
75-
const actions = f.getCodeActions(context);
76-
if (actions && actions.length > 0) {
77-
for (const action of actions) {
78-
if (action === undefined) {
79-
context.host.log(`Action for error code ${context.errorCode} added an invalid action entry; please log a bug`);
80-
}
81-
else {
82-
allActions.push(action);
83-
}
84-
}
85-
}
86-
});
87-
88-
return allActions;
66+
return flatMap(errorCodeToFixes.get(String(context.errorCode)) || emptyArray, f => f.getCodeActions(context));
8967
}
9068

9169
export function getAllFixes(context: CodeFixAllContext): CombinedCodeActions {

0 commit comments

Comments
 (0)