| 340 | } |
| 341 | |
| 342 | const hasShallowPredicateCycle = ( |
| 343 | startKey: string, |
| 344 | ctx: ReadContext |
| 345 | ): boolean => { |
| 346 | if ( |
| 347 | ctx.shallowRefPredDepth.size === 0 || |
| 348 | !reachesActiveShallowRef(startKey, ctx) || |
| 349 | !ctx.shallowEdgesByRef[startKey] |
| 350 | ) |
| 351 | return false |
| 352 | const hitsCycle = (target: string, sawPred: boolean): boolean => { |
| 353 | const entryDepth = ctx.shallowRefPredDepth.get(target) |
| 354 | return entryDepth !== undefined && (sawPred || ctx.inPredicate > entryDepth) |
| 355 | } |
| 356 | const seenPlain = new Set<string>([startKey]) |
| 357 | const seenPred = new Set<string>() |
| 358 | const queue: [ref: string, sawPred: boolean][] = [[startKey, false]] |
| 359 | while (queue.length) { |
| 360 | const [u, sawPred] = queue.shift()! |
| 361 | const edges = ctx.shallowEdgesByRef[u] |
| 362 | if (!edges) continue |
| 363 | for (const v of edges.pred) { |
| 364 | if (hitsCycle(v, true)) return true |
| 365 | if (!seenPred.has(v)) { |
| 366 | seenPred.add(v) |
| 367 | queue.push([v, true]) |
| 368 | } |
| 369 | } |
| 370 | for (const v of edges.plain) { |
| 371 | if (hitsCycle(v, sawPred)) return true |
| 372 | if (sawPred) { |
| 373 | if (!seenPred.has(v)) { |
| 374 | seenPred.add(v) |
| 375 | queue.push([v, true]) |
| 376 | } |
| 377 | } else if (!seenPlain.has(v)) { |
| 378 | seenPlain.add(v) |
| 379 | queue.push([v, false]) |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | return false |
| 384 | } |
| 385 | |
| 386 | const validCycleIdentifier = /^[$A-Z_a-z][\w$]*$/ |
| 387 | |