Skip to content

Commit

Permalink
✨ 🐛 fix, add support for circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Jun 6, 2024
1 parent 696a784 commit 2c324c0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
7 changes: 7 additions & 0 deletions playground/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,10 @@ const array = [1, 2, 3];
consoleInspect(array);
(array as any).innerObj = { a: 1 };
consoleInspect(array);

// circular refs
const circular = { a: 1, b: { a: 3 } };
circular.b = circular;
consoleInspect(circular, {
depth: 5,
})
1 change: 1 addition & 0 deletions src/extras/consoleTable/consoleTableCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function findOptimalExpansion(
wrap: "single-line",
},
{
circular: new Set(),
depth: depth,
keys: new Set(),
wrap: Number.MAX_SAFE_INTEGER,
Expand Down
2 changes: 2 additions & 0 deletions src/inspect/consoleInspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface ConsoleInspectContext {
depth: number;
wrap: number;
keys: Set<string>;
circular: Set<unknown>
}

export default function consoleInspect(
Expand Down Expand Up @@ -66,6 +67,7 @@ function inspect(

const context: ConsoleInspectContext = {
depth: 0,
circular: new Set(),
keys: new Set(options.keys),
wrap:
options.wrap === "auto"
Expand Down
28 changes: 26 additions & 2 deletions src/inspect/inspectors/inspectAny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { inspectIterable } from "./inspectIterable";
import { inspectObject } from "./inspectObject";
import ConsoleInspection from "../utils/ConsoleInspection";
import consoleStyles from "../utils/consoleStyles";

export default function inspectAny(
value: unknown,
Expand All @@ -27,9 +28,15 @@ export default function inspectAny(
],
};
} else if (Array.isArray(value) || isIterable(value)) {
return inspectIterable(value, options, context);
return (
maybeCircular(value, options, context) ??
inspectIterable(value, options, context)
);
} else if (typeof value === "object" && value !== null) {
return inspectObject(value, options, context);
return (
maybeCircular(value, options, context) ??
inspectObject(value, options, context)
);
}

// fallback
Expand All @@ -38,3 +45,20 @@ export default function inspectAny(
spans: [consoleText(String(value))],
};
}

function maybeCircular(
value: unknown,
options: Required<ConsoleInspectOptions>,
context: ConsoleInspectContext,
): ConsoleInspection | undefined {
if (context.circular.has(value)) {
return {
type: "inline",
spans: [
consoleText("[Circular]", consoleStyles[options.theme].dimmed),
],
};
}
context.circular.add(value);
return undefined;
}
12 changes: 9 additions & 3 deletions src/inspect/inspectors/inspectIterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function inspectIterableMultiLine(
iterableType(iterable) === "Map"
? inspectEntry(value, options, context)
: inspectAny(value, options, {
circular: context.circular,
keys: context.keys,
depth: context.depth + 1,
wrap: Math.max(
Expand Down Expand Up @@ -144,11 +145,14 @@ export function inspectIterableMultiLine(
spans.push(consoleText("\n"));
}

spans.push(consoleText(key, consoleStyles[options.theme].highlight));
spans.push(
consoleText(key, consoleStyles[options.theme].highlight),
);
spans.push(consoleText(": "));

const value = array[key as keyof typeof array];
const inspection = inspectAny(value, options, {
circular: context.circular,
keys: context.keys,
depth: context.depth + 1,
wrap: Math.max(
Expand All @@ -162,8 +166,8 @@ export function inspectIterableMultiLine(
} else {
spans.push(...inspection.spans);
}
return spans
})
return spans;
}),
],
};
}
Expand All @@ -176,6 +180,7 @@ function inspectEntry(
const [key, value] = entry as [unknown, unknown];
const keySpan = inspectInline(key, options.theme);
const valueInspection = inspectAny(value, options, {
circular: context.circular,
keys: context.keys,
depth: context.depth + 1,
wrap: Math.max(
Expand All @@ -186,6 +191,7 @@ function inspectEntry(

if (!isPrimitive(key)) {
return inspectObject({ key, value }, options, {
circular: context.circular,
keys: context.keys,
depth: context.depth,
wrap: Math.max(context.wrap - options.indent, 0),
Expand Down
1 change: 1 addition & 0 deletions src/inspect/inspectors/inspectObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export function inspectObjectMultiLine(

const value = object[key as keyof typeof object];
const inspection = inspectAny(value, options, {
circular: context.circular,
keys: context.keys,
depth: context.depth + 1,
wrap: Math.max(
Expand Down

0 comments on commit 2c324c0

Please sign in to comment.