Skip to content

Commit 77a77f6

Browse files
committed
fix: parent fixups and testing
1 parent 8577dc5 commit 77a77f6

File tree

3 files changed

+353
-15
lines changed

3 files changed

+353
-15
lines changed

src/index.test.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@ describe("traverse", () => {
66
mockMutation: any,
77
schema: JSONSchema,
88
isCycle = expect.any(Boolean),
9-
nth?: number
9+
nth?: number,
10+
parent = expect.anything(),
1011
) => {
12+
if (parent === false) { parent = undefined; }
1113
if (nth) {
1214
expect(mockMutation).toHaveBeenNthCalledWith(
1315
nth,
1416
schema,
1517
isCycle,
1618
expect.any(String),
17-
expect.anything(),
19+
parent
1820
);
1921
} else {
2022
expect(mockMutation).toHaveBeenCalledWith(
2123
schema,
2224
isCycle,
2325
expect.any(String),
24-
expect.anything(),
26+
parent
2527
);
2628
}
2729
};
@@ -510,7 +512,7 @@ describe("traverse", () => {
510512
};
511513
schema.properties.foo.anyOf[0].items.properties.baz = schema;
512514
schema.properties.bar.allOf[0].properties.baz = schema.properties.foo.anyOf[0];
513-
const mockMutation = jest.fn((s) => s);
515+
const mockMutation = jest.fn((s) => { console.log(s); return s; });
514516
traverse(schema as JSONSchema, mockMutation);
515517
expect(mockMutation).toHaveBeenCalledTimes(6);
516518
});
@@ -655,6 +657,22 @@ describe("traverse", () => {
655657
testCalls(mockMutation2, testSchema2);
656658
expect(mockMutation2).toHaveBeenCalledTimes(1);
657659
expect(testSchema2.items).toBe(testSchema2);
660+
661+
expect(mockMutation1).toHaveBeenCalledTimes(1);
662+
expect(mockMutation1).toHaveBeenCalledWith(
663+
testSchema1.properties.skipFirstCycle,
664+
true,
665+
expect.any(String),
666+
testSchema1
667+
);
668+
669+
expect(mockMutation2).toHaveBeenCalledTimes(1);
670+
expect(mockMutation2).toHaveBeenCalledWith(
671+
testSchema2.items,
672+
true,
673+
expect.any(String),
674+
testSchema2
675+
);
658676
});
659677

660678
});
@@ -755,7 +773,7 @@ describe("traverse", () => {
755773

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

758-
testCalls(mockMutation, testSchema, false, 1);
776+
testCalls(mockMutation, testSchema, false, 1, false);
759777
testCalls(mockMutation, testSchema.properties.foo, false, 2);
760778
testCalls(mockMutation, testSchema.properties.foo.items[0], false, 3);
761779
testCalls(mockMutation, testSchema.properties.foo.items[1], false, 4);
@@ -778,7 +796,7 @@ describe("traverse", () => {
778796

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

781-
testCalls(mockMutation, testSchema, false, 1);
799+
testCalls(mockMutation, testSchema, false, 1, false);
782800
testCalls(mockMutation, testSchema.properties.foo, false, 2);
783801
testCalls(mockMutation, testSchema.properties.foo.items[0], false, 3);
784802
testCalls(mockMutation, testSchema.properties.foo.items[1], false, 4);

src/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ const isCycle = (s: JSONSchema, recursiveStack: JSONSchema[]): JSONSchema | fals
6767
return false;
6868
};
6969

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

@@ -89,6 +88,7 @@ export default function traverse(
8988
traverseOptions = defaultOptions,
9089
depth = 0,
9190
recursiveStack: JSONSchema[] = [],
91+
mutableStack: JSONSchema[] = [],
9292
pathStack: string[] = [],
9393
prePostMap: Array<[JSONSchema, JSONSchema]> = [],
9494
cycleSet: JSONSchema[] = [],
@@ -111,7 +111,7 @@ export default function traverse(
111111
schema,
112112
false,
113113
jsonPathStringify(pathStack),
114-
last(recursiveStack) || schema
114+
last(mutableStack) || schema
115115
);
116116
}
117117
}
@@ -121,13 +121,15 @@ export default function traverse(
121121
mutableSchema = { ...schema };
122122
}
123123

124+
mutableStack.push(mutableSchema);
125+
124126
if (opts.bfs === true) {
125127
if (opts.skipFirstMutation === false || depth !== 0) {
126128
mutableSchema = mutation(
127129
mutableSchema,
128130
false,
129131
jsonPathStringify(pathStack),
130-
last(recursiveStack) || schema
132+
last(mutableStack, 2)
131133
) as JSONSchemaObject;
132134
}
133135
}
@@ -147,7 +149,7 @@ export default function traverse(
147149
s,
148150
true,
149151
jsonPathStringify(path),
150-
last(recursiveStack, true) || schema
152+
last(mutableStack), // should we be popping here?
151153
);
152154
}
153155

@@ -165,6 +167,7 @@ export default function traverse(
165167
traverseOptions,
166168
depth + 1,
167169
recursiveStack,
170+
mutableStack,
168171
path,
169172
prePostMap,
170173
cycleSet,
@@ -203,7 +206,7 @@ export default function traverse(
203206
schema.items,
204207
true,
205208
jsonPathStringify(pathStack),
206-
last(recursiveStack, true) || schema
209+
last(mutableStack)
207210
);
208211
} else {
209212
const [, cycledMutableSchema] = prePostMap.find(
@@ -219,6 +222,7 @@ export default function traverse(
219222
traverseOptions,
220223
depth + 1,
221224
recursiveStack,
225+
mutableStack,
222226
[...pathStack, "items"],
223227
prePostMap,
224228
cycleSet,
@@ -266,14 +270,18 @@ export default function traverse(
266270
}
267271

268272
if (opts.bfs === true) {
273+
mutableStack.pop();
269274
return mutableSchema;
270275
} else {
271276
const isCycle = cycleSet.indexOf(schema) !== -1
272-
return mutation(
277+
//console.log(recursiveStack, depth, mutableSchema);
278+
const mutated = mutation(
273279
mutableSchema,
274280
isCycle,
275281
jsonPathStringify(pathStack),
276-
last(recursiveStack, true) || schema
282+
last(mutableStack, 2) || schema
277283
);
284+
mutableStack.pop();
285+
return mutated;
278286
}
279287
}

0 commit comments

Comments
 (0)