From 146ad36b9aeaafcddc1ce6ea2222a3feddb75c4d Mon Sep 17 00:00:00 2001 From: Jason Dent Date: Thu, 16 Sep 2021 13:24:58 +0200 Subject: [PATCH] fix: Fix accidental promise returned by reporters. --- packages/cspell/src/util/reporters.ts | 35 ++++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/cspell/src/util/reporters.ts b/packages/cspell/src/util/reporters.ts index 13405061b45f..13fd4e71afdf 100644 --- a/packages/cspell/src/util/reporters.ts +++ b/packages/cspell/src/util/reporters.ts @@ -7,16 +7,27 @@ import type { } from '@cspell/cspell-types'; import { ApplicationError, toError } from './errors'; -function mergeEmitters>( - reporters: ReadonlyArray, - emitterName: T -): CSpellReporter[T] { - return async (...args: unknown[]) => { - // eslint-disable-next-line prefer-spread - reporters.forEach((reporter: any) => reporter[emitterName].apply(reporter, args)); +type StandardEmitters = Omit; + +function callAll(methods: ((p: P0) => void)[]): (p: P0) => void; +function callAll(methods: ((p0: P0, p1: P1) => void)[]): (p0: P0, p1: P1) => void; +function callAll

(methods: ((...p: P[]) => void)[]): (...p: P[]) => void { + return (...p: P[]) => { + for (const method of methods) { + method(...p); + } + return; }; } +function extractEmitter( + reporters: ReadonlyArray, + emitterName: K +): StandardEmitters[K][] { + // The `bind` is used in case the reporter is a class. + return reporters.map((r) => r[emitterName].bind(r) as StandardEmitters[K]); +} + function mergeResultEmitters(reporters: ReadonlyArray): CSpellReporter['result'] { return async (result: RunResult) => { await Promise.all(reporters.map((reporter) => reporter.result(result))); @@ -28,11 +39,11 @@ function mergeResultEmitters(reporters: ReadonlyArray): CSpellRe */ export function mergeReporters(...reporters: ReadonlyArray): CSpellReporter { return { - issue: mergeEmitters(reporters, 'issue'), - info: mergeEmitters(reporters, 'info'), - debug: mergeEmitters(reporters, 'debug'), - progress: mergeEmitters(reporters, 'progress'), - error: mergeEmitters(reporters, 'error'), + issue: callAll(extractEmitter(reporters, 'issue')), + info: callAll(extractEmitter(reporters, 'info')), + debug: callAll(extractEmitter(reporters, 'debug')), + progress: callAll(extractEmitter(reporters, 'progress')), + error: callAll(extractEmitter(reporters, 'error')), result: mergeResultEmitters(reporters), }; }