From 8c6bcdb6246359f169f8494a5c909f8afafa8dbe Mon Sep 17 00:00:00 2001 From: Yang Jun Date: Sun, 14 Jun 2026 17:06:13 +0800 Subject: [PATCH] refactor: restore distinct json and inspect filter shapes Co-authored-by: Cursor --- src/filters/misc.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/filters/misc.ts b/src/filters/misc.ts index f25a1641a..ec6ef374d 100644 --- a/src/filters/misc.ts +++ b/src/filters/misc.ts @@ -29,25 +29,22 @@ function indentWidth (space: number | string): number { function json (this: FilterImpl, value: any, space: number | string = 0) { const memoryLimit = this.context.memoryLimit const width = indentWidth(space) - const ancestors: unknown[] = [] return JSON.stringify(value, function (this: unknown, key: string, value: any) { - while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this) ancestors.pop() - memoryLimit.use(jsonNodeSize(value) + (isArray(this) ? 0 : key.length) + ancestors.length * width) - if (value === null || typeof value !== 'object') return value - ancestors.push(value) + memoryLimit.use(jsonNodeSize(value) + (isArray(this) ? 0 : key.length) + width) return value }, space) } function inspect (this: FilterImpl, value: any, space: number | string = 0) { const memoryLimit = this.context.memoryLimit - const width = indentWidth(space) - const ancestors: unknown[] = [] + const ancestors: object[] = [] return JSON.stringify(value, function (this: unknown, key: string, value: any) { - // `this` is the parent holding `value`; ancestors.length is its nesting depth. + if (typeof value !== 'object' || value === null) { + memoryLimit.use(jsonNodeSize(value) + (isArray(this) ? 0 : key.length)) + return value + } while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this) ancestors.pop() - memoryLimit.use(jsonNodeSize(value) + (isArray(this) ? 0 : key.length) + ancestors.length * width) - if (value === null || typeof value !== 'object') return value + memoryLimit.use(jsonNodeSize(value) + (isArray(this) ? 0 : key.length)) if (ancestors.includes(value)) return '[Circular]' ancestors.push(value) return value