From 3016866a4bc67b570e32ddd0fb4bbacac839f435 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 26 Sep 2023 18:30:39 +0100 Subject: [PATCH] Make the right function a real function, then the 'this' param works --- src/settings/rageshake.ts | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/settings/rageshake.ts b/src/settings/rageshake.ts index d3aaf1dc..6ded0c2d 100644 --- a/src/settings/rageshake.ts +++ b/src/settings/rageshake.ts @@ -551,10 +551,10 @@ type StringifyReplacer = ( // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#circular_references // Injects `<$ cycle-trimmed $>` wherever it cuts a cyclical object relationship -const getCircularReplacer = function (): StringifyReplacer { +const getCircularReplacer = (): StringifyReplacer => { const seen = new WeakSet(); const depthMap = new WeakMap(); - return (key: string, value: unknown): unknown => { + return function (this: unknown, key: string, value: unknown): unknown { if (typeof value === "object" && value !== null) { if (seen.has(value)) { return "<$ cycle-trimmed $>"; @@ -562,17 +562,10 @@ const getCircularReplacer = function (): StringifyReplacer { seen.add(value); let depth = 0; - if (key) { - depth = depthMap.get(value) ?? 0; - } - - // 'this' is supposed to be the object the value was found in, according to - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify - // but that doesn't seem to be the case. Instead, we do a pre-pass on the children here to - // remember what depth we saw them at. - for (const v of Object.values(value)) { - if (typeof v === "object") depthMap.set(v, depth + 1); + if (this) { + depth = depthMap.get(this) ?? 0; } + depthMap.set(value, depth + 1); if (depth > DEPTH_LIMIT) return "<$ object-pruned $>"; }