Skip to content

Commit

Permalink
fix: parent fixups and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Mar 12, 2024
1 parent 8577dc5 commit 77a77f6
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 15 deletions.
30 changes: 24 additions & 6 deletions src/index.test.ts
Expand Up @@ -6,22 +6,24 @@ describe("traverse", () => {
mockMutation: any,
schema: JSONSchema,
isCycle = expect.any(Boolean),
nth?: number
nth?: number,
parent = expect.anything(),
) => {
if (parent === false) { parent = undefined; }
if (nth) {
expect(mockMutation).toHaveBeenNthCalledWith(
nth,
schema,
isCycle,
expect.any(String),
expect.anything(),
parent
);
} else {
expect(mockMutation).toHaveBeenCalledWith(
schema,
isCycle,
expect.any(String),
expect.anything(),
parent
);
}
};
Expand Down Expand Up @@ -510,7 +512,7 @@ describe("traverse", () => {
};
schema.properties.foo.anyOf[0].items.properties.baz = schema;
schema.properties.bar.allOf[0].properties.baz = schema.properties.foo.anyOf[0];
const mockMutation = jest.fn((s) => s);
const mockMutation = jest.fn((s) => { console.log(s); return s; });
traverse(schema as JSONSchema, mockMutation);
expect(mockMutation).toHaveBeenCalledTimes(6);
});
Expand Down Expand Up @@ -655,6 +657,22 @@ describe("traverse", () => {
testCalls(mockMutation2, testSchema2);
expect(mockMutation2).toHaveBeenCalledTimes(1);
expect(testSchema2.items).toBe(testSchema2);

expect(mockMutation1).toHaveBeenCalledTimes(1);
expect(mockMutation1).toHaveBeenCalledWith(
testSchema1.properties.skipFirstCycle,
true,
expect.any(String),
testSchema1
);

expect(mockMutation2).toHaveBeenCalledTimes(1);
expect(mockMutation2).toHaveBeenCalledWith(
testSchema2.items,
true,
expect.any(String),
testSchema2
);
});

});
Expand Down Expand Up @@ -755,7 +773,7 @@ describe("traverse", () => {

traverse(testSchema as JSONSchema, mockMutation, { bfs: true, });

testCalls(mockMutation, testSchema, false, 1);
testCalls(mockMutation, testSchema, false, 1, false);
testCalls(mockMutation, testSchema.properties.foo, false, 2);
testCalls(mockMutation, testSchema.properties.foo.items[0], false, 3);
testCalls(mockMutation, testSchema.properties.foo.items[1], false, 4);
Expand All @@ -778,7 +796,7 @@ describe("traverse", () => {

traverse(testSchema as JSONSchema, mockMutation, { bfs: true, mutable: true });

testCalls(mockMutation, testSchema, false, 1);
testCalls(mockMutation, testSchema, false, 1, false);
testCalls(mockMutation, testSchema.properties.foo, false, 2);
testCalls(mockMutation, testSchema.properties.foo.items[0], false, 3);
testCalls(mockMutation, testSchema.properties.foo.items[1], false, 4);
Expand Down
24 changes: 16 additions & 8 deletions src/index.ts
Expand Up @@ -67,8 +67,7 @@ const isCycle = (s: JSONSchema, recursiveStack: JSONSchema[]): JSONSchema | fals
return false;
};

const last = (i: JSONSchema[], skipTwo = false): JSONSchema | undefined => {
const skip = skipTwo ? -2 : -1;
const last = (i: JSONSchema[], skip: number = 1): JSONSchema => {
return i[i.length - skip];
};

Expand All @@ -89,6 +88,7 @@ export default function traverse(
traverseOptions = defaultOptions,
depth = 0,
recursiveStack: JSONSchema[] = [],
mutableStack: JSONSchema[] = [],
pathStack: string[] = [],
prePostMap: Array<[JSONSchema, JSONSchema]> = [],
cycleSet: JSONSchema[] = [],
Expand All @@ -111,7 +111,7 @@ export default function traverse(
schema,
false,
jsonPathStringify(pathStack),
last(recursiveStack) || schema
last(mutableStack) || schema
);
}
}
Expand All @@ -121,13 +121,15 @@ export default function traverse(
mutableSchema = { ...schema };
}

mutableStack.push(mutableSchema);

if (opts.bfs === true) {
if (opts.skipFirstMutation === false || depth !== 0) {
mutableSchema = mutation(
mutableSchema,
false,
jsonPathStringify(pathStack),
last(recursiveStack) || schema
last(mutableStack, 2)
) as JSONSchemaObject;
}
}
Expand All @@ -147,7 +149,7 @@ export default function traverse(
s,
true,
jsonPathStringify(path),
last(recursiveStack, true) || schema
last(mutableStack), // should we be popping here?
);
}

Expand All @@ -165,6 +167,7 @@ export default function traverse(
traverseOptions,
depth + 1,
recursiveStack,
mutableStack,
path,
prePostMap,
cycleSet,
Expand Down Expand Up @@ -203,7 +206,7 @@ export default function traverse(
schema.items,
true,
jsonPathStringify(pathStack),
last(recursiveStack, true) || schema
last(mutableStack)
);
} else {
const [, cycledMutableSchema] = prePostMap.find(
Expand All @@ -219,6 +222,7 @@ export default function traverse(
traverseOptions,
depth + 1,
recursiveStack,
mutableStack,
[...pathStack, "items"],
prePostMap,
cycleSet,
Expand Down Expand Up @@ -266,14 +270,18 @@ export default function traverse(
}

if (opts.bfs === true) {
mutableStack.pop();
return mutableSchema;
} else {
const isCycle = cycleSet.indexOf(schema) !== -1
return mutation(
//console.log(recursiveStack, depth, mutableSchema);
const mutated = mutation(
mutableSchema,
isCycle,
jsonPathStringify(pathStack),
last(recursiveStack, true) || schema
last(mutableStack, 2) || schema
);
mutableStack.pop();
return mutated;
}
}

0 comments on commit 77a77f6

Please sign in to comment.