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

refactor(typescript): packages/rspack/src/util/StatsFactory #7381

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 2 additions & 8 deletions packages/rspack/src/stats/StatsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ export class StatsFactory {
h.call(comparators, context)
);
if (comparators.length > 0) {
items.sort(
// @ts-expect-error number of arguments is correct
concatComparators(...comparators)
);
items.sort(concatComparators(...comparators));
}

// run filter on sorted items
Expand Down Expand Up @@ -344,10 +341,7 @@ export class StatsFactory {
h => h.call(comparators2, context)
);
if (comparators2.length > 0) {
resultItems.sort(
// @ts-expect-error number of arguments is correct
concatComparators(...comparators2)
);
resultItems.sort(concatComparators(...comparators2));
}

// group result items
Expand Down
16 changes: 7 additions & 9 deletions packages/rspack/src/util/comparators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,18 @@ const concatComparatorsCache: TwoKeyWeakMap<
Comparator
> = new TwoKeyWeakMap();

export const concatComparators = (
c1: Comparator,
c2: Comparator,
...cRest: Comparator[]
): Comparator => {
if (cRest.length > 0) {
const [c3, ...cRest2] = cRest;
return concatComparators(c1, concatComparators(c2, c3, ...cRest2));
}
export const concatComparators = (...comps: Array<Comparator>): Comparator => {
const [c1, c2, ...cRest] = comps;

if (c2 === undefined) {
return c1;
}

if (cRest.length > 0) {
const [c3, ...cRest2] = cRest;
return concatComparators(c1, concatComparators(c2, c3, ...cRest2));
}

const cacheEntry = concatComparatorsCache.get(c1, c2);
if (cacheEntry !== undefined) return cacheEntry;

Expand Down
Loading