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/logging-truncateArgs #7383

Merged
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
15 changes: 5 additions & 10 deletions packages/rspack/src/logging/truncateArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@
* https://github.com/webpack/webpack/blob/main/LICENSE
*/

const arraySum = (array: any) => {
const arraySum = (array: number[]) => {
let sum = 0;
for (const item of array) sum += item;
return sum;
};

/**
* @param {any[]} args items to be truncated
* @param {number} maxLength maximum length of args including spaces between
* @returns {string[]} truncated args
* @param args items to be truncated
* @param maxLength maximum length of args including spaces between
* @returns truncated args
*/
// @ts-expect-error
const truncateArgs = (args, maxLength) => {
// @ts-expect-error
const truncateArgs = (args: any[], maxLength: number): string[] => {
const lengths = args.map(a => `${a}`.length);
const availableLength = maxLength - lengths.length + 1;

Expand All @@ -36,7 +34,6 @@ const truncateArgs = (args, maxLength) => {
}

// Check if there is space for at least 4 chars per arg
// @ts-expect-error
if (availableLength < arraySum(lengths.map(i => Math.min(i, 6)))) {
// remove args
if (args.length > 1)
Expand All @@ -52,7 +49,6 @@ const truncateArgs = (args, maxLength) => {
// Try to remove chars from the longest items until it fits
while (currentLength > availableLength) {
const maxLength = Math.max(...lengths);
// @ts-expect-error
const shorterItems = lengths.filter(l => l !== maxLength);
const nextToMaxLength =
shorterItems.length > 0 ? Math.max(...shorterItems) : 0;
Expand All @@ -71,7 +67,6 @@ const truncateArgs = (args, maxLength) => {
}

// Return args reduced to length in lengths
// @ts-expect-error
return args.map((a, i) => {
const str = `${a}`;
const length = lengths[i];
Expand Down
Loading