diff --git a/src/lang/modifyAst.ts b/src/lang/modifyAst.ts index c9279faa4..13d4b7825 100644 --- a/src/lang/modifyAst.ts +++ b/src/lang/modifyAst.ts @@ -64,7 +64,7 @@ import { KCL_DEFAULT_CONSTANT_PREFIXES } from '@src/lib/constants' import type { DefaultPlaneStr } from '@src/lib/planes' import { err, trap } from '@src/lib/trap' -import { isOverlap, roundOff } from '@src/lib/utils' +import { isArray, isOverlap, roundOff } from '@src/lib/utils' import type { ExtrudeFacePlane } from '@src/machines/modelingMachine' import { ARG_AT } from '@src/lang/constants' @@ -949,6 +949,27 @@ export function deleteSegmentFromPipeExpression( return _modifiedAst } +/** + * Deletes a standalone top level statement from the AST + * Used for removing both unassigned statements and variable declarations + * + * @param ast The AST to modify + * @param pathToNode The path to the node to delete + */ +export function deleteTopLevelStatement( + ast: Node, + pathToNode: PathToNode +): Error | void { + const pathStep = pathToNode[1] + if (!isArray(pathStep) || typeof pathStep[0] !== 'number') { + return new Error( + 'Invalid pathToNode structure: expected a number at path[1][0]' + ) + } + const statementIndex: number = pathStep[0] + ast.body.splice(statementIndex, 1) +} + export function removeSingleConstraintInfo( pathToCallExp: PathToNode, argDetails: SimplifiedArgDetails,