Skip to content

Commit

Permalink
fix(json-schema/reactive): fix circular reference check logic
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Sep 15, 2021
1 parent 1afc3f3 commit b356dad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
24 changes: 24 additions & 0 deletions packages/json-schema/src/__tests__/traverse.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { traverse } from '../traverse'
import { FormPath } from '@formily/shared'

test('traverse circular reference', () => {
// eslint-disable-next-line
Expand All @@ -15,3 +16,26 @@ test('traverse circular reference', () => {
a.dd.mm = a
traverse(a, () => {})
})

test('traverse none circular reference', () => {
// eslint-disable-next-line
var dd = {
mm: null,
}
let a = {
dd,
bb: {
dd,
},
}
const paths = []
traverse(a, (value, path) => {
paths.push(path)
})
expect(
paths.some((path) => FormPath.parse(path).includes('dd.mm'))
).toBeTruthy()
expect(
paths.some((path) => FormPath.parse(path).includes('bb.dd.mm'))
).toBeTruthy()
})
8 changes: 4 additions & 4 deletions packages/json-schema/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ export const compile = <Source = any, Scope = any>(
source: Source,
scope?: Scope
): any => {
const seenObjects = new WeakMap()
const seenObjects = new WeakSet()
const compile = (source: any) => {
if (isStr(source)) {
return shallowCompile(source, scope)
} else if (isArr(source)) {
return source.map((value: any) => compile(value))
} else if (isPlainObj(source)) {
if (isNoNeedCompileObject(source)) return source
if (seenObjects.get(source)) {
if (seenObjects.has(source)) {
return source
}
seenObjects.set(source, true)
seenObjects.add(source)
const results = reduce(
source,
(buf, value, key) => {
Expand All @@ -82,7 +82,7 @@ export const compile = <Source = any, Scope = any>(
},
{}
)
seenObjects.set(source, false)
seenObjects.delete(source)
return results
}
return source
Expand Down
8 changes: 4 additions & 4 deletions packages/json-schema/src/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ export const traverse = (
visitor: (value: any, path: Array<string | number>, address: string) => void,
filter?: (value: any, path: Array<string | number>) => boolean
) => {
const seenObjects = new WeakMap()
const seenObjects = new WeakSet()
const root = target
const traverse = (target: any, path = [], address = '') => {
if (filter?.(target, path) === false) return

if (isPlainObj(target)) {
if (seenObjects.get(target)) {
if (seenObjects.has(target)) {
return
}
seenObjects.set(target, true)
seenObjects.add(target)
if (isNoNeedCompileObject(target) && root !== target) {
visitor(target, path, address)
return
}
each(target, (value, key) => {
traverse(value, path.concat(key), address + '.' + key)
})
seenObjects.set(target, false)
seenObjects.delete(target)
} else {
visitor(target, path, address)
}
Expand Down

0 comments on commit b356dad

Please sign in to comment.