From 9d988432138a8e1da4ec1fc5dcdd0a006f95c09b Mon Sep 17 00:00:00 2001 From: Pascal Delange Date: Thu, 21 Nov 2024 12:46:52 +0100 Subject: [PATCH] fix error on ast state machine better --- .../app-builder/src/services/editor/ast-editor.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/app-builder/src/services/editor/ast-editor.tsx b/packages/app-builder/src/services/editor/ast-editor.tsx index 0066f531..bc9c8e69 100644 --- a/packages/app-builder/src/services/editor/ast-editor.tsx +++ b/packages/app-builder/src/services/editor/ast-editor.tsx @@ -91,10 +91,9 @@ export function useAstNodeEditor({ name, }; if (isUnaryMainAstOperatorFunction(name)) { - const children = newNode.children.slice(0, 1); - newNode.children = children; + newNode.children = newNChildren(1, newNode.children); } else if (isBinaryMainAstOperatorFunction(name)) { - newNode.children.push(NewUndefinedAstNode()); + newNode.children = newNChildren(2, newNode.children); } set({ rootAstNode: setAtPath(rootAstNode, path, newNode), @@ -134,6 +133,13 @@ export function useAstNodeEditor({ return store; } +function newNChildren(n: number, currentChildren: AstNode[]) { + return Array.from( + { length: n }, + (_, i) => currentChildren[i] ?? NewUndefinedAstNode(), + ); +} + export function AstNodeEditorProvider({ children, store,