From 8265e6d21d83cb06062a2dd6c14a803e4f07dfec Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Tue, 25 Apr 2023 14:22:34 +0200 Subject: [PATCH] ref: Fix comment for normalize --- packages/utils/src/normalize.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/utils/src/normalize.ts b/packages/utils/src/normalize.ts index 4b2dd611f8e2..15095c29775e 100644 --- a/packages/utils/src/normalize.ts +++ b/packages/utils/src/normalize.ts @@ -100,17 +100,16 @@ function visit( return value as ObjOrArray; } - // Do not normalize objects that we know have already been normalized. As a general rule, the - // "__sentry_skip_normalization__" property should only be used sparingly and only should only be set on objects that - // have already been normalized. - let overriddenDepth = depth; - - if (typeof (value as ObjOrArray)['__sentry_override_normalization_depth__'] === 'number') { - overriddenDepth = (value as ObjOrArray)['__sentry_override_normalization_depth__'] as number; - } + // We can set `__sentry_override_normalization_depth__` on an object to ensure that from there + // We keep a certain amount of depth. + // This should be used sparingly, e.g. we use it for the redux integration to ensure we get a certain amount of state. + const remainingDepth = + typeof (value as ObjOrArray)['__sentry_override_normalization_depth__'] === 'number' + ? ((value as ObjOrArray)['__sentry_override_normalization_depth__'] as number) + : depth; // We're also done if we've reached the max depth - if (overriddenDepth === 0) { + if (remainingDepth === 0) { // At this point we know `serialized` is a string of the form `"[object XXXX]"`. Clean it up so it's just `"[XXXX]"`. return stringified.replace('object ', ''); } @@ -126,7 +125,7 @@ function visit( try { const jsonValue = valueWithToJSON.toJSON(); // We need to normalize the return value of `.toJSON()` in case it has circular references - return visit('', jsonValue, overriddenDepth - 1, maxProperties, memo); + return visit('', jsonValue, remainingDepth - 1, maxProperties, memo); } catch (err) { // pass (The built-in `toJSON` failed, but we can still try to do it ourselves) } @@ -155,7 +154,7 @@ function visit( // Recursively visit all the child nodes const visitValue = visitable[visitKey]; - normalized[visitKey] = visit(visitKey, visitValue, overriddenDepth - 1, maxProperties, memo); + normalized[visitKey] = visit(visitKey, visitValue, remainingDepth - 1, maxProperties, memo); numAdded++; }