Skip to content

Commit 3e0f7ee

Browse files
committed
refactor: use inspect and update comment
1 parent 1c528e9 commit 3e0f7ee

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { Duplex } from 'stream';
1010
import { PassThrough } from 'stream';
1111
import type { StubbedInstance } from 'ts-sinon';
1212
import { stubInterface } from 'ts-sinon';
13-
import { promisify } from 'util';
13+
import { inspect, promisify } from 'util';
1414
import {
1515
expect,
1616
fakeTTYProps,
@@ -547,13 +547,11 @@ describe('MongoshNodeRepl', function () {
547547
input.write('history()\n');
548548
await waitEval(bus);
549549
expect(output).includes(
550-
formatOutput(
551-
{
552-
value: getAllHistoryItems()
553-
.slice(1, getAllHistoryItems().length)
554-
.reverse(),
555-
},
556-
{ colors: true, maxArrayLength: Infinity }
550+
inspect(
551+
getAllHistoryItems()
552+
.slice(1, getAllHistoryItems().length)
553+
.reverse(),
554+
{ maxArrayLength: Infinity }
557555
)
558556
);
559557
});
@@ -564,12 +562,9 @@ describe('MongoshNodeRepl', function () {
564562
await waitEval(bus);
565563
const history = getAllHistoryItems().slice(1).reverse();
566564
expect(output).includes(
567-
formatOutput(
568-
{
569-
value: history.slice(history.length - 10).reverse(),
570-
},
571-
{ colors: false, maxArrayLength: Infinity }
572-
)
565+
inspect(history.slice(history.length - 10).reverse(), {
566+
maxArrayLength: Infinity,
567+
})
573568
);
574569
});
575570
});

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,18 @@ class MongoshNodeRepl implements EvaluationListener {
403403
replHistory.slice(1).reverse();
404404

405405
// eslint-disable-next-line @typescript-eslint/no-explicit-any
406-
formattedHistory[util.inspect.custom as any] = (() => {
407-
return formatOutput(
408-
{
409-
// Providing a copy of the history avoids a circular reference.
410-
value: formattedHistory.concat(),
411-
},
412-
{ colors: true, maxArrayLength: Infinity }
413-
);
406+
formattedHistory[util.inspect.custom as any] = ((
407+
depth: number | null,
408+
options: util.InspectOptions,
409+
inspect: typeof util.inspect
410+
) => {
411+
// We pass a copy of the array without the util.inspect.custom set
412+
// to prevent infinite recursion.
413+
return inspect(formattedHistory.concat(), {
414+
...options,
415+
depth,
416+
maxArrayLength: Infinity,
417+
});
414418
// eslint-disable-next-line @typescript-eslint/no-explicit-any
415419
}) as any;
416420
return formattedHistory;

0 commit comments

Comments
 (0)