Skip to content

Commit

Permalink
chore(schema): clean up and describe execution
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth committed Mar 12, 2024
1 parent 7d2d714 commit 4f13425
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
18 changes: 11 additions & 7 deletions packages/@sanity/schema/src/sanity/extractSchema.ts
Expand Up @@ -497,25 +497,29 @@ function sortByDependencies(compiledSchema: SchemaDef): string[] {

// Sorts the types by their dependencies
const typeNames: string[] = []
const tempMark = new Set<SanitySchemaType>()
const permMark = new Set<SanitySchemaType>()
// holds a temporary mark for types that are currently being visited, to detect cyclic dependencies
const currentlyVisiting = new Set<SanitySchemaType>()

// holds a permanent mark for types that have been already visited
const visited = new Set<SanitySchemaType>()

// 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)
Expand Down
Expand Up @@ -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
})
})
})
14 changes: 0 additions & 14 deletions packages/@sanity/schema/test/extractSchema/fixtures/block.ts
Expand Up @@ -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',
Expand Down Expand Up @@ -164,15 +159,6 @@ export default {
},
],
},
// {
// type: 'block',
// of: [
// {
// type: 'color',
// title: 'Color',
// },
// ],
// },
],
},
{
Expand Down

0 comments on commit 4f13425

Please sign in to comment.