Skip to content

ref(utils): Fix comment for normalize #7954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions packages/utils/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,16 @@ function visit(
return value as ObjOrArray<unknown>;
}

// 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<unknown>)['__sentry_override_normalization_depth__'] === 'number') {
overriddenDepth = (value as ObjOrArray<unknown>)['__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<unknown>)['__sentry_override_normalization_depth__'] === 'number'
? ((value as ObjOrArray<unknown>)['__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 ', '');
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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++;
}
Expand Down