fix: add circular reference handling for object stringification

This commit is contained in:
MrWindlike 2025-03-11 10:17:46 +08:00
parent 552e8ba2dd
commit c69bbfaf1c
2 changed files with 19 additions and 1 deletions

View File

@ -36,6 +36,7 @@ import tern, { Def } from 'tern';
import { getTypeString } from '../../../utils/type';
import ecmascript from '../../../constants/ecmascript';
import { PREVENT_POPOVER_WIDGET_CLOSE_CLASS } from '../../../constants';
import { stringify } from '../../../utils/object';
injectGlobal`
.CodeMirror-hints {
@ -386,7 +387,7 @@ export const ExpressionEditor = React.forwardRef<
<Box fontWeight="bold" marginBottom="4px">
{error ? 'Error' : getTypeString(evaledValue?.value)}
</Box>
{error || JSON.stringify(evaledValue?.value, null, 2)}
{error || stringify(evaledValue?.value)}
</Box>
);

View File

@ -0,0 +1,17 @@
function stringify(obj: unknown) {
const cache: unknown[] = [];
JSON.stringify(obj, (_, value) => {
if (typeof value === 'object' && value !== null) {
// Duplicate reference found, discard key
if (cache.includes(value)) return;
// Store value in our collection
cache.push(value);
}
return value;
});
}
export { stringify };