Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: avoid unnecessary throwing error in delete cell (#8134)

Myles Scolnickb9a204e352d3
diff --git a/frontend/src/utils/__tests__/id-tree.test.ts b/frontend/src/utils/__tests__/id-tree.test.ts
index 531015d73a..30cf0e8713 100644
--- a/frontend/src/utils/__tests__/id-tree.test.ts
+++ b/frontend/src/utils/__tests__/id-tree.test.ts
@@ -214,16 +214,19 @@ describe("CollapsibleTree", () => {
`);
});
- it("fails to expand", () => {
+ it("fails to expand when node not found", () => {
expect(() => tree.expand("five")).toThrowErrorMatchingInlineSnapshot(
"[Error: Node five not found in tree. Valid ids: one,two,three,four]",
);
- expect(() => {
- tree.expand("one");
- tree.expand("one");
- }).toThrowErrorMatchingInlineSnapshot(
- "[Error: Node one is already expanded]",
- );
+ });
+
+ it("expand on already expanded node is a no-op", () => {
+ // Expanding an already expanded node should return the same tree (no-op)
+ const result = tree.expand("one");
+ expect(result).toBe(tree);
+ // Can call multiple times without error
+ const result2 = tree.expand("one");
+ expect(result2).toBe(tree);
});
it("moves nodes correctly", () => {
@@ -378,6 +381,18 @@ describe("CollapsibleTree", () => {
`);
});
+ it("can delete non-collapsed nodes without throwing", () => {
+ // Deleting a non-collapsed node should not throw
+ // (previously this would throw "Node is already expanded" internally)
+ const result = tree.deleteAtIndex(1);
+ expect(result.toString()).toMatchInlineSnapshot(`
+ "one
+ three
+ four
+ "
+ `);
+ });
+
it("fails to delete nodes", () => {
expect(() => tree.deleteAtIndex(5)).toThrowErrorMatchingInlineSnapshot(
"[Error: Node at index 5 not found in tree]",
diff --git a/frontend/src/utils/id-tree.tsx b/frontend/src/utils/id-tree.tsx
index c8e9671066..d296938d25 100644
--- a/frontend/src/utils/id-tree.tsx
+++ b/frontend/src/utils/id-tree.tsx
@@ -360,7 +360,8 @@ export class CollapsibleTree<T> {
}
/**
- * Expand a node and all of its children
+ * Expand a node and all of its children.
+ * If the node is already expanded, returns the same tree (no-op).
*/
expand(id: T): CollapsibleTree<T> {
const nodeIndex = this.nodes.findIndex((n) => n.value === id);
@@ -373,7 +374,8 @@ export class CollapsibleTree<T> {
let nodes = [...this.nodes];
const node = nodes[nodeIndex];
if (!node.isCollapsed) {
- throw new Error(`Node ${id} is already expanded`);
+ // Already expanded, no-op
+ return this;
}
nodes[nodeIndex] = new TreeNode(node.value, false, []);
@@ -495,13 +497,9 @@ export class CollapsibleTree<T> {
*/
deleteAtIndex(idx: number): CollapsibleTree<T> {
const id = this.atOrThrow(idx);
- let tree = this.withNodes(this.nodes);
- try {
- tree = tree.expand(id);
- } catch {
- // Don't care if its not expanded
- }
- return this.withNodes(arrayDelete(tree.nodes, idx));
+ // Expand the node first (if collapsed) to bring children back to top level
+ const tree = this.expand(id);
+ return tree.withNodes(arrayDelete(tree.nodes, idx));
}
delete(id: T): CollapsibleTree<T> {
@@ -524,16 +522,10 @@ export class CollapsibleTree<T> {
if (found.length === 0) {
return this;
}
- let result = this.withNodes(this.nodes);
- for (const node of found) {
- try {
- result = result.expand(node);
- } catch {
- // Don't care if its the last node and its not expanded
- }
- }
-
- return result;
+ return found.reduce<CollapsibleTree<T>>(
+ (acc, node) => acc.expand(node),
+ this,
+ );
}
/**