Skip to content

Commit 7349abc

Browse files
authored
fix: specify inspect depth for node inspect.custom (#2007)
Discussed in mikro-orm/mikro-orm#5525 (reply in thread)
1 parent 0b40365 commit 7349abc

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

src/adapter/templates/getStringyProps.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const enum DescriptionSymbols {
99
Generic = 'debug.description',
1010
// Node.js-specific symbol that is used for some Node types https://nodejs.org/api/util.html#utilinspectcustom
1111
Node = 'nodejs.util.inspect.custom',
12+
13+
// Depth for `nodejs.util.inspect.custom`
14+
Depth = 2,
1215
}
1316

1417
/**
@@ -58,7 +61,7 @@ export const getStringyProps = templateFunction(function (
5861
continue;
5962
}
6063
try {
61-
str = value[sym]();
64+
str = value[sym](DescriptionSymbols.Depth);
6265
break;
6366
} catch {
6467
// ignored
@@ -101,11 +104,11 @@ export const getToStringIfCustom = templateFunction(function (
101104
Symbol.for(DescriptionSymbols.Generic),
102105
Symbol.for(DescriptionSymbols.Node),
103106
]) {
104-
if (typeof (this as Record<symbol, () => string>)[sym] !== 'function') {
107+
if (typeof (this as Record<symbol, (depth?: number) => string>)[sym] !== 'function') {
105108
continue;
106109
}
107110
try {
108-
str = (this as Record<symbol, () => string>)[sym]();
111+
str = (this as Record<symbol, (depth?: number) => string>)[sym](DescriptionSymbols.Depth);
109112
break;
110113
} catch {
111114
// ignored

src/test/console/console-format-popular-types.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,11 @@ Evaluating: 'console.log([new class { [Symbol.for("debug.description")]() { retu
366366
stdout> (1) [{…}]
367367
stdout> > (1) [{…}]
368368

369-
Evaluating: 'console.log(new class { [Symbol.for("nodejs.util.inspect.custom")]() { return "some node repr" } })'
369+
Evaluating: 'console.log(new class { [Symbol.for("nodejs.util.inspect.custom")](depth) { return "some node repr, depth: " + depth } })'
370370
stdout> {}
371-
stdout> > some node repr
371+
stdout> > some node repr, depth: 2
372372

373-
Evaluating: 'console.log([new class { [Symbol.for("nodejs.util.inspect.custom")]() { return "some node repr" } }])'
373+
Evaluating: 'console.log([new class { [Symbol.for("nodejs.util.inspect.custom")](depth) { return "some node repr, depth: " + depth } }])'
374374
stdout> (1) [{…}]
375375
stdout> > (1) [{…}]
376376

src/test/console/consoleFormatTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('console format', () => {
157157
'new class { toString() { return "custom to string" } }',
158158
'new class { toString() { return "long custom to string".repeat(500) } }',
159159
'new class { [Symbol.for("debug.description")]() { return "some custom repr" } }',
160-
'new class { [Symbol.for("nodejs.util.inspect.custom")]() { return "some node repr" } }',
160+
'new class { [Symbol.for("nodejs.util.inspect.custom")](depth) { return "some node repr, depth: " + depth } }',
161161
];
162162
const expressions = variables.map(v => [`console.log(${v})`, `console.log([${v}])`]);
163163
await p.logger.evaluateAndLog(([] as string[]).concat(...expressions), { depth: 0 });

0 commit comments

Comments
 (0)