Post-kwargs cleanup (#6571)
* Remove dead code * Stop creating CallExpression and instead create CallExpressionKw
This commit is contained in:
		@ -140,23 +140,6 @@ export function createPipeSubstitution(): Node<PipeSubstitution> {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function createCallExpressionStdLib(
 | 
			
		||||
  name: string,
 | 
			
		||||
  args: CallExpression['arguments']
 | 
			
		||||
): Node<CallExpression> {
 | 
			
		||||
  return {
 | 
			
		||||
    type: 'CallExpression',
 | 
			
		||||
    start: 0,
 | 
			
		||||
    end: 0,
 | 
			
		||||
    moduleId: 0,
 | 
			
		||||
    outerAttrs: [],
 | 
			
		||||
    preComments: [],
 | 
			
		||||
    commentStart: 0,
 | 
			
		||||
    callee: createLocalName(name),
 | 
			
		||||
    arguments: args,
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const nonCodeMetaEmpty = () => {
 | 
			
		||||
  return { nonCodeNodes: {}, startNodes: [], start: 0, end: 0 }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,6 @@ import type { NonCodeMeta } from '@rust/kcl-lib/bindings/NonCodeMeta'
 | 
			
		||||
 | 
			
		||||
import {
 | 
			
		||||
  createArrayExpression,
 | 
			
		||||
  createCallExpressionStdLib,
 | 
			
		||||
  createCallExpressionStdLibKw,
 | 
			
		||||
  createExpressionStatement,
 | 
			
		||||
  createIdentifier,
 | 
			
		||||
@ -14,7 +13,6 @@ import {
 | 
			
		||||
  createLabeledArg,
 | 
			
		||||
  createLiteral,
 | 
			
		||||
  createLocalName,
 | 
			
		||||
  createObjectExpression,
 | 
			
		||||
  createPipeExpression,
 | 
			
		||||
  createVariableDeclaration,
 | 
			
		||||
  findUniqueName,
 | 
			
		||||
@ -439,9 +437,11 @@ export function loftSketches(
 | 
			
		||||
  const modifiedAst = structuredClone(node)
 | 
			
		||||
  const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.LOFT)
 | 
			
		||||
  const elements = declarators.map((d) => createLocalName(d.id.name))
 | 
			
		||||
  const loft = createCallExpressionStdLib('loft', [
 | 
			
		||||
  const loft = createCallExpressionStdLibKw(
 | 
			
		||||
    'loft',
 | 
			
		||||
    createArrayExpression(elements),
 | 
			
		||||
  ])
 | 
			
		||||
    []
 | 
			
		||||
  )
 | 
			
		||||
  const declaration = createVariableDeclaration(name, loft)
 | 
			
		||||
  modifiedAst.body.push(declaration)
 | 
			
		||||
  const pathToNode: PathToNode = [
 | 
			
		||||
@ -574,101 +574,6 @@ export function addSweep({
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function revolveSketch(
 | 
			
		||||
  node: Node<Program>,
 | 
			
		||||
  pathToNode: PathToNode,
 | 
			
		||||
  shouldPipe = false,
 | 
			
		||||
  angle: Expr = createLiteral(4)
 | 
			
		||||
):
 | 
			
		||||
  | {
 | 
			
		||||
      modifiedAst: Node<Program>
 | 
			
		||||
      pathToNode: PathToNode
 | 
			
		||||
      pathToRevolveArg: PathToNode
 | 
			
		||||
    }
 | 
			
		||||
  | Error {
 | 
			
		||||
  const _node = structuredClone(node)
 | 
			
		||||
  const _node1 = getNodeFromPath(_node, pathToNode)
 | 
			
		||||
  if (err(_node1)) return _node1
 | 
			
		||||
  const { node: sketchExpression } = _node1
 | 
			
		||||
 | 
			
		||||
  // determine if sketchExpression is in a pipeExpression or not
 | 
			
		||||
  const _node2 = getNodeFromPath<PipeExpression>(
 | 
			
		||||
    _node,
 | 
			
		||||
    pathToNode,
 | 
			
		||||
    'PipeExpression'
 | 
			
		||||
  )
 | 
			
		||||
  if (err(_node2)) return _node2
 | 
			
		||||
  const { node: pipeExpression } = _node2
 | 
			
		||||
 | 
			
		||||
  const isInPipeExpression = pipeExpression.type === 'PipeExpression'
 | 
			
		||||
 | 
			
		||||
  const _node3 = getNodeFromPath<VariableDeclarator>(
 | 
			
		||||
    _node,
 | 
			
		||||
    pathToNode,
 | 
			
		||||
    'VariableDeclarator'
 | 
			
		||||
  )
 | 
			
		||||
  if (err(_node3)) return _node3
 | 
			
		||||
  const { node: variableDeclarator, shallowPath: pathToDecleration } = _node3
 | 
			
		||||
 | 
			
		||||
  const revolveCall = createCallExpressionStdLib('revolve', [
 | 
			
		||||
    createObjectExpression({
 | 
			
		||||
      angle: angle,
 | 
			
		||||
      // TODO: hard coded 'X' axis for revolve MVP, should be changed.
 | 
			
		||||
      axis: createLocalName('X'),
 | 
			
		||||
    }),
 | 
			
		||||
    createLocalName(variableDeclarator.id.name),
 | 
			
		||||
  ])
 | 
			
		||||
 | 
			
		||||
  if (shouldPipe) {
 | 
			
		||||
    const pipeChain = createPipeExpression(
 | 
			
		||||
      isInPipeExpression
 | 
			
		||||
        ? [...pipeExpression.body, revolveCall]
 | 
			
		||||
        : [sketchExpression as any, revolveCall]
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    variableDeclarator.init = pipeChain
 | 
			
		||||
    const pathToRevolveArg: PathToNode = [
 | 
			
		||||
      ...pathToDecleration,
 | 
			
		||||
      ['init', 'VariableDeclarator'],
 | 
			
		||||
      ['body', ''],
 | 
			
		||||
      [pipeChain.body.length - 1, 'index'],
 | 
			
		||||
      ['arguments', 'CallExpression'],
 | 
			
		||||
      [0, 'index'],
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    return {
 | 
			
		||||
      modifiedAst: _node,
 | 
			
		||||
      pathToNode,
 | 
			
		||||
      pathToRevolveArg,
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // We're not creating a pipe expression,
 | 
			
		||||
  // but rather a separate constant for the extrusion
 | 
			
		||||
  const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.REVOLVE)
 | 
			
		||||
  const VariableDeclaration = createVariableDeclaration(name, revolveCall)
 | 
			
		||||
  const sketchIndexInPathToNode =
 | 
			
		||||
    pathToDecleration.findIndex((a) => a[0] === 'body') + 1
 | 
			
		||||
  const sketchIndexInBody = pathToDecleration[sketchIndexInPathToNode][0]
 | 
			
		||||
  if (typeof sketchIndexInBody !== 'number')
 | 
			
		||||
    return new Error('expected sketchIndexInBody to be a number')
 | 
			
		||||
  _node.body.splice(sketchIndexInBody + 1, 0, VariableDeclaration)
 | 
			
		||||
 | 
			
		||||
  const pathToRevolveArg: PathToNode = [
 | 
			
		||||
    ['body', ''],
 | 
			
		||||
    [sketchIndexInBody + 1, 'index'],
 | 
			
		||||
    ['declaration', 'VariableDeclaration'],
 | 
			
		||||
    ['init', 'VariableDeclarator'],
 | 
			
		||||
    ['arguments', 'CallExpression'],
 | 
			
		||||
    [0, 'index'],
 | 
			
		||||
  ]
 | 
			
		||||
  return {
 | 
			
		||||
    modifiedAst: _node,
 | 
			
		||||
    pathToNode: [...pathToNode.slice(0, -1), [-1, 'index']],
 | 
			
		||||
    pathToRevolveArg,
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function sketchOnExtrudedFace(
 | 
			
		||||
  node: Node<Program>,
 | 
			
		||||
  sketchPathToNode: PathToNode,
 | 
			
		||||
 | 
			
		||||
@ -207,13 +207,19 @@ export async function deleteFromSelection(
 | 
			
		||||
              extrudeNameToDelete = dec.id.name
 | 
			
		||||
            }
 | 
			
		||||
            if (
 | 
			
		||||
              // TODO: This is wrong, loft is now a CallExpressionKw.
 | 
			
		||||
              dec.init.type === 'CallExpression' &&
 | 
			
		||||
              dec.init.callee.name.name === 'loft' &&
 | 
			
		||||
              dec.init.arguments?.[0].type === 'ArrayExpression' &&
 | 
			
		||||
              dec.init.arguments?.[0].elements.some(
 | 
			
		||||
                (a) => a.type === 'Name' && a.name.name === varDecName
 | 
			
		||||
              )
 | 
			
		||||
              (dec.init.type === 'CallExpressionKw' &&
 | 
			
		||||
                dec.init.callee.name.name === 'loft' &&
 | 
			
		||||
                dec.init.unlabeled !== null &&
 | 
			
		||||
                dec.init.unlabeled.type === 'ArrayExpression' &&
 | 
			
		||||
                dec.init.unlabeled.elements.some(
 | 
			
		||||
                  (a) => a.type === 'Name' && a.name.name === varDecName
 | 
			
		||||
                )) ||
 | 
			
		||||
              (dec.init.type === 'CallExpression' &&
 | 
			
		||||
                dec.init.callee.name.name === 'loft' &&
 | 
			
		||||
                dec.init.arguments?.[0].type === 'ArrayExpression' &&
 | 
			
		||||
                dec.init.arguments?.[0].elements.some(
 | 
			
		||||
                  (a) => a.type === 'Name' && a.name.name === varDecName
 | 
			
		||||
                ))
 | 
			
		||||
            ) {
 | 
			
		||||
              pathToNode = path
 | 
			
		||||
              extrudeNameToDelete = dec.id.name
 | 
			
		||||
 | 
			
		||||
@ -22,7 +22,6 @@ import type {
 | 
			
		||||
  Program,
 | 
			
		||||
} from '@src/lang/wasm'
 | 
			
		||||
import {
 | 
			
		||||
  createCallExpressionStdLib,
 | 
			
		||||
  createArrayExpression,
 | 
			
		||||
  createLocalName,
 | 
			
		||||
  createCallExpressionStdLibKw,
 | 
			
		||||
@ -194,12 +193,16 @@ export function createTagExpressions(
 | 
			
		||||
 | 
			
		||||
        // Modify the tag based on selectionType
 | 
			
		||||
        if (artifact.type === 'sweepEdge' && artifact.subType === 'opposite') {
 | 
			
		||||
          tagCall = createCallExpressionStdLib('getOppositeEdge', [tagCall])
 | 
			
		||||
          tagCall = createCallExpressionStdLibKw('getOppositeEdge', tagCall, [])
 | 
			
		||||
        } else if (
 | 
			
		||||
          artifact.type === 'sweepEdge' &&
 | 
			
		||||
          artifact.subType === 'adjacent'
 | 
			
		||||
        ) {
 | 
			
		||||
          tagCall = createCallExpressionStdLib('getNextAdjacentEdge', [tagCall])
 | 
			
		||||
          tagCall = createCallExpressionStdLibKw(
 | 
			
		||||
            'getNextAdjacentEdge',
 | 
			
		||||
            tagCall,
 | 
			
		||||
            []
 | 
			
		||||
          )
 | 
			
		||||
        }
 | 
			
		||||
        return tagCall
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@ -3,7 +3,8 @@ import type { Name } from '@rust/kcl-lib/bindings/Name'
 | 
			
		||||
import {
 | 
			
		||||
  createArrayExpression,
 | 
			
		||||
  createCallExpression,
 | 
			
		||||
  createCallExpressionStdLib,
 | 
			
		||||
  createCallExpressionStdLibKw,
 | 
			
		||||
  createLabeledArg,
 | 
			
		||||
  createLiteral,
 | 
			
		||||
  createPipeSubstitution,
 | 
			
		||||
} from '@src/lang/create'
 | 
			
		||||
@ -28,6 +29,7 @@ import { assertParse, recast } from '@src/lang/wasm'
 | 
			
		||||
import { initPromise } from '@src/lang/wasmUtils'
 | 
			
		||||
import { enginelessExecutor } from '@src/lib/testHelpers'
 | 
			
		||||
import { err } from '@src/lib/trap'
 | 
			
		||||
import { ARG_END_ABSOLUTE } from '@src/lang/constants'
 | 
			
		||||
 | 
			
		||||
beforeAll(async () => {
 | 
			
		||||
  await initPromise
 | 
			
		||||
@ -721,20 +723,23 @@ describe('Testing specific sketch getNodeFromPath workflow', () => {
 | 
			
		||||
      variables: {},
 | 
			
		||||
      pathToNode: sketchPathToNode,
 | 
			
		||||
      expressions: [
 | 
			
		||||
        createCallExpressionStdLib(
 | 
			
		||||
          'lineTo', // We are forcing lineTo!
 | 
			
		||||
          [
 | 
			
		||||
        createCallExpressionStdLibKw('line', null, [
 | 
			
		||||
          createLabeledArg(
 | 
			
		||||
            ARG_END_ABSOLUTE,
 | 
			
		||||
            createArrayExpression([
 | 
			
		||||
              createCallExpressionStdLib('profileStartX', [
 | 
			
		||||
              createCallExpressionStdLibKw(
 | 
			
		||||
                'profileStartX',
 | 
			
		||||
                createPipeSubstitution(),
 | 
			
		||||
              ]),
 | 
			
		||||
              createCallExpressionStdLib('profileStartY', [
 | 
			
		||||
                []
 | 
			
		||||
              ),
 | 
			
		||||
              createCallExpressionStdLibKw(
 | 
			
		||||
                'profileStartY',
 | 
			
		||||
                createPipeSubstitution(),
 | 
			
		||||
              ]),
 | 
			
		||||
            ]),
 | 
			
		||||
            createPipeSubstitution(),
 | 
			
		||||
          ]
 | 
			
		||||
        ),
 | 
			
		||||
                []
 | 
			
		||||
              ),
 | 
			
		||||
            ])
 | 
			
		||||
          ),
 | 
			
		||||
        ]),
 | 
			
		||||
      ],
 | 
			
		||||
    })
 | 
			
		||||
    if (err(modifiedAst)) throw modifiedAst
 | 
			
		||||
@ -748,7 +753,7 @@ describe('Testing specific sketch getNodeFromPath workflow', () => {
 | 
			
		||||
  |> xLine(length = -0.15)
 | 
			
		||||
  |> line([-0.02, 0.21], %)
 | 
			
		||||
  |> line([-0.08, 0.05], %)
 | 
			
		||||
  |> lineTo([profileStartX(%), profileStartY(%)], %)
 | 
			
		||||
  |> line(endAbsolute = [profileStartX(%), profileStartY(%)])
 | 
			
		||||
`
 | 
			
		||||
    expect(recasted).toEqual(expectedCode)
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,6 @@ import {
 | 
			
		||||
import {
 | 
			
		||||
  createArrayExpression,
 | 
			
		||||
  createBinaryExpression,
 | 
			
		||||
  createCallExpressionStdLib,
 | 
			
		||||
  createCallExpressionStdLibKw,
 | 
			
		||||
  createLabeledArg,
 | 
			
		||||
  createLiteral,
 | 
			
		||||
@ -64,16 +63,16 @@ export const getRectangleCallExpressions = (
 | 
			
		||||
    ),
 | 
			
		||||
    angledLine(
 | 
			
		||||
      createBinaryExpression([
 | 
			
		||||
        createCallExpressionStdLib('segAng', [createLocalName(tag)]),
 | 
			
		||||
        createCallExpressionStdLibKw('segAng', createLocalName(tag), []),
 | 
			
		||||
        '+',
 | 
			
		||||
        createLiteral(90),
 | 
			
		||||
      ]), // 90 offset from the previous line
 | 
			
		||||
      createLiteral(0) // This will be the height of the rectangle
 | 
			
		||||
    ),
 | 
			
		||||
    angledLine(
 | 
			
		||||
      createCallExpressionStdLib('segAng', [createLocalName(tag)]), // same angle as the first line
 | 
			
		||||
      createCallExpressionStdLibKw('segAng', createLocalName(tag), []), // same angle as the first line
 | 
			
		||||
      createUnaryExpression(
 | 
			
		||||
        createCallExpressionStdLib('segLen', [createLocalName(tag)]),
 | 
			
		||||
        createCallExpressionStdLibKw('segLen', createLocalName(tag), []),
 | 
			
		||||
        '-'
 | 
			
		||||
      ) // negative height
 | 
			
		||||
    ),
 | 
			
		||||
@ -120,7 +119,7 @@ export function updateRectangleSketch(
 | 
			
		||||
    'angle',
 | 
			
		||||
    secondEdge,
 | 
			
		||||
    createBinaryExpression([
 | 
			
		||||
      createCallExpressionStdLib('segAng', [createLocalName(tag)]),
 | 
			
		||||
      createCallExpressionStdLibKw('segAng', createLocalName(tag), []),
 | 
			
		||||
      Math.sign(y) === Math.sign(x) ? '+' : '-',
 | 
			
		||||
      createLiteral(90),
 | 
			
		||||
    ])
 | 
			
		||||
@ -198,7 +197,7 @@ export function updateCenterRectangleSketch(
 | 
			
		||||
    }
 | 
			
		||||
    oldAngleOperator = oldAngle.operator
 | 
			
		||||
    const newAngle = createBinaryExpression([
 | 
			
		||||
      createCallExpressionStdLib('segAng', [createLocalName(tag)]),
 | 
			
		||||
      createCallExpressionStdLibKw('segAng', createLocalName(tag), []),
 | 
			
		||||
      oldAngleOperator,
 | 
			
		||||
      createLiteral(90),
 | 
			
		||||
    ])
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user