From 4f13425fb841961aa73d1928e914bd2b4bac5ef2 Mon Sep 17 00:00:00 2001 From: Sindre Gulseth Date: Tue, 12 Mar 2024 11:16:06 +0100 Subject: [PATCH] chore(schema): clean up and describe execution --- .../@sanity/schema/src/sanity/extractSchema.ts | 18 +++++++++++------- .../test/extractSchema/extractSchema.test.ts | 8 ++++---- .../test/extractSchema/fixtures/block.ts | 14 -------------- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/packages/@sanity/schema/src/sanity/extractSchema.ts b/packages/@sanity/schema/src/sanity/extractSchema.ts index a1d1750318c..1b7681c734d 100644 --- a/packages/@sanity/schema/src/sanity/extractSchema.ts +++ b/packages/@sanity/schema/src/sanity/extractSchema.ts @@ -497,25 +497,29 @@ function sortByDependencies(compiledSchema: SchemaDef): string[] { // Sorts the types by their dependencies const typeNames: string[] = [] - const tempMark = new Set() - const permMark = new Set() + // holds a temporary mark for types that are currently being visited, to detect cyclic dependencies + const currentlyVisiting = new Set() + + // holds a permanent mark for types that have been already visited + const visited = new Set() + // visit implements a depth-first search function visit(type: SanitySchemaType) { - if (permMark.has(type)) { + if (visited.has(type)) { return } // If we find a type that is already in the temporary mark, we have a cyclic dependency. - if (tempMark.has(type)) { + if (currentlyVisiting.has(type)) { return } // mark this as a temporary mark, meaning it's being visited - tempMark.add(type) + currentlyVisiting.add(type) const deps = dependencyMap.get(type) if (deps !== undefined) { deps.forEach((dep) => visit(dep)) } - tempMark.delete(type) - permMark.add(type) + currentlyVisiting.delete(type) + visited.add(type) if (!typeNames.includes(type.name)) { typeNames.unshift(type.name) diff --git a/packages/@sanity/schema/test/extractSchema/extractSchema.test.ts b/packages/@sanity/schema/test/extractSchema/extractSchema.test.ts index 212d4cbb6f0..748461c6056 100644 --- a/packages/@sanity/schema/test/extractSchema/extractSchema.test.ts +++ b/packages/@sanity/schema/test/extractSchema/extractSchema.test.ts @@ -371,9 +371,9 @@ describe('Extract schema test', () => { expect(extracted.length).toBeGreaterThan(0) // we don't really care about the exact number, just that it passes :+1: }) - // const skips = cases.filter((v): v is {schemaName: string; schema: null} => v.schema === null) - // test.skip.each(skips)('extracts schema $schemaName', () => { - // // Add a test for the skipped cases so we can track them in the test report - // }) + const skips = cases.filter((v): v is {schemaName: string; schema: null} => v.schema === null) + test.skip.each(skips)('extracts schema $schemaName', () => { + // Add a test for the skipped cases so we can track them in the test report + }) }) }) diff --git a/packages/@sanity/schema/test/extractSchema/fixtures/block.ts b/packages/@sanity/schema/test/extractSchema/fixtures/block.ts index 26c1a777f98..12390f533c6 100644 --- a/packages/@sanity/schema/test/extractSchema/fixtures/block.ts +++ b/packages/@sanity/schema/test/extractSchema/fixtures/block.ts @@ -132,11 +132,6 @@ export default { }, {type: 'author', title: 'Embedded author'}, {type: 'code', title: 'Code'}, - // { - // type: 'color', - // name: 'colorBlock', - // title: 'Color (block)', - // }, { type: 'object', title: 'Test object', @@ -164,15 +159,6 @@ export default { }, ], }, - // { - // type: 'block', - // of: [ - // { - // type: 'color', - // title: 'Color', - // }, - // ], - // }, ], }, {