Skip to content
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

fix: remove circular references when colorizing json #1216

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
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
38 changes: 37 additions & 1 deletion src/ux/colorize-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,42 @@ type Options = {
theme?: Record<string, string> | undefined
}

export function removeCycles(object: unknown) {
// Keep track of seen objects.
const seenObjects = new WeakMap<Record<string, unknown>, undefined>()

const _removeCycles = (obj: unknown) => {
// Use object prototype to get around type and null checks
if (Object.prototype.toString.call(obj) === '[object Object]') {
// We know it is a "Record<string, unknown>" because of the conditional
const dictionary = obj as Record<string, unknown>

// Seen, return undefined to remove.
if (seenObjects.has(dictionary)) return

seenObjects.set(dictionary, undefined)

for (const key in dictionary) {
// Delete the duplicate object if cycle found.
if (_removeCycles(dictionary[key]) === undefined) {
delete dictionary[key]
}
}
} else if (Array.isArray(obj)) {
for (const i in obj) {
if (_removeCycles(obj[i]) === undefined) {
// We don't want to delete the array, but we can replace the element with null.
obj[i] = null
}
}
}

return obj
}

return _removeCycles(object)
}

function formatInput(json?: unknown, options?: Options) {
return options?.pretty
? JSON.stringify(typeof json === 'string' ? JSON.parse(json) : json, null, 2)
Expand All @@ -26,7 +62,7 @@ function formatInput(json?: unknown, options?: Options) {
}

export function tokenize(json?: unknown, options?: Options) {
let input = formatInput(json, options)
let input = formatInput(removeCycles(json), options)

const tokens = []
let foundToken = false
Expand Down
76 changes: 75 additions & 1 deletion test/ux/colorize-json.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from 'chai'

import {tokenize} from '../../src/ux/colorize-json'
import {removeCycles, tokenize} from '../../src/ux/colorize-json'

describe('colorizeJson', () => {
it('tokenizes a basic JSON object', () => {
Expand Down Expand Up @@ -124,4 +124,78 @@ describe('colorizeJson', () => {

expect(result).to.deep.equal([])
})

it('removes circular references from json', () => {
const obj = {
foo: 'bar',
baz: {
qux: 'quux',
},
}
// @ts-expect-error
obj.circular = obj

const result = tokenize(obj)
expect(result).to.deep.equal([
{type: 'brace', value: '{'},
{type: 'key', value: '"foo"'},
{type: 'colon', value: ':'},
{type: 'string', value: '"bar"'},
{type: 'comma', value: ','},
{type: 'key', value: '"baz"'},
{type: 'colon', value: ':'},
{type: 'brace', value: '{'},
{type: 'key', value: '"qux"'},
{type: 'colon', value: ':'},
{type: 'string', value: '"quux"'},
{type: 'brace', value: '}'},
{type: 'brace', value: '}'},
])
})
})

describe('removeCycles', () => {
it('removes circular references from objects', () => {
const obj = {
foo: 'bar',
baz: {
qux: 'quux',
},
}
// @ts-expect-error
obj.circular = obj

const result = removeCycles(obj)
expect(result).to.deep.equal({
foo: 'bar',
baz: {
qux: 'quux',
},
})
})

it('removes circular references from objects in array', () => {
const obj = {
foo: 'bar',
baz: {
qux: 'quux',
},
}
// @ts-expect-error
obj.circular = obj
const arr = [{foo: 'bar'}, obj]

const result = removeCycles(arr)
expect(result).to.deep.equal([
{
foo: 'bar',
},
{
baz: {
qux: 'quux',
},
foo: 'bar',
},
])
})
})
Loading