Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix accidental promise returned by reporters. #1702

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions packages/cspell/src/util/reporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ import type {
} from '@cspell/cspell-types';
import { ApplicationError, toError } from './errors';

function mergeEmitters<T extends keyof Omit<CSpellReporter, 'result'>>(
reporters: ReadonlyArray<CSpellReporter>,
emitterName: T
): CSpellReporter[T] {
return async (...args: unknown[]) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The async caused the unexpected Promise.

// eslint-disable-next-line prefer-spread
reporters.forEach((reporter: any) => reporter[emitterName].apply(reporter, args));
type StandardEmitters = Omit<CSpellReporter, 'result'>;

function callAll<P0>(methods: ((p: P0) => void)[]): (p: P0) => void;
function callAll<P0, P1>(methods: ((p0: P0, p1: P1) => void)[]): (p0: P0, p1: P1) => void;
function callAll<P>(methods: ((...p: P[]) => void)[]): (...p: P[]) => void {
return (...p: P[]) => {
for (const method of methods) {
method(...p);
}
return;
};
}

function extractEmitter<K extends keyof StandardEmitters>(
reporters: ReadonlyArray<StandardEmitters>,
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>): CSpellReporter['result'] {
return async (result: RunResult) => {
await Promise.all(reporters.map((reporter) => reporter.result(result)));
Expand All @@ -28,11 +39,11 @@ function mergeResultEmitters(reporters: ReadonlyArray<CSpellReporter>): CSpellRe
*/
export function mergeReporters(...reporters: ReadonlyArray<CSpellReporter>): 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),
};
}
Expand Down