Merge pull request #733 from smartxworks/fix/editor

fix: add circular reference handling for object stringification
This commit is contained in:
tanbowensg 2025-03-11 10:23:04 +08:00 committed by GitHub
commit ee322e6d7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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 };