From b363240d05a126b46681f1ea42b01588a3ec2d07 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 12 Apr 2018 09:19:01 -0700 Subject: [PATCH] Simplify registerCodeFIx (#23349) --- src/services/codeFixProvider.ts | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/src/services/codeFixProvider.ts b/src/services/codeFixProvider.ts index 5fc8df3744..39f760ba04 100644 --- a/src/services/codeFixProvider.ts +++ b/src/services/codeFixProvider.ts @@ -24,7 +24,7 @@ namespace ts { } export namespace codefix { - const codeFixRegistrations: CodeFixRegistration[][] = []; + const errorCodeToFixes = createMultiMap(); const fixIdToRegistration = createMap(); type DiagnosticAndArguments = DiagnosticMessage | [DiagnosticMessage, string] | [DiagnosticMessage, string, string]; @@ -48,12 +48,7 @@ namespace ts { export function registerCodeFix(reg: CodeFixRegistration) { for (const error of reg.errorCodes) { - let registrations = codeFixRegistrations[error]; - if (!registrations) { - registrations = []; - codeFixRegistrations[error] = registrations; - } - registrations.push(reg); + errorCodeToFixes.add(String(error), reg); } if (reg.fixIds) { for (const fixId of reg.fixIds) { @@ -63,29 +58,12 @@ namespace ts { } } - export function getSupportedErrorCodes() { - return Object.keys(codeFixRegistrations); + export function getSupportedErrorCodes(): string[] { + return arrayFrom(errorCodeToFixes.keys()); } export function getFixes(context: CodeFixContext): CodeFixAction[] { - const fixes = codeFixRegistrations[context.errorCode]; - const allActions: CodeFixAction[] = []; - - forEach(fixes, f => { - const actions = f.getCodeActions(context); - if (actions && actions.length > 0) { - for (const action of actions) { - if (action === undefined) { - context.host.log(`Action for error code ${context.errorCode} added an invalid action entry; please log a bug`); - } - else { - allActions.push(action); - } - } - } - }); - - return allActions; + return flatMap(errorCodeToFixes.get(String(context.errorCode)) || emptyArray, f => f.getCodeActions(context)); } export function getAllFixes(context: CodeFixAllContext): CombinedCodeActions {