Skip to content

Commit fa51b4b

Browse files
authored
Post-kwargs cleanup (KittyCAD#6571)
* Remove dead code * Stop creating CallExpression and instead create CallExpressionKw
1 parent 1fa6e1e commit fa51b4b

File tree

6 files changed

+46
-145
lines changed

6 files changed

+46
-145
lines changed

src/lang/create.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,6 @@ export function createPipeSubstitution(): Node<PipeSubstitution> {
140140
}
141141
}
142142

143-
export function createCallExpressionStdLib(
144-
name: string,
145-
args: CallExpression['arguments']
146-
): Node<CallExpression> {
147-
return {
148-
type: 'CallExpression',
149-
start: 0,
150-
end: 0,
151-
moduleId: 0,
152-
outerAttrs: [],
153-
preComments: [],
154-
commentStart: 0,
155-
callee: createLocalName(name),
156-
arguments: args,
157-
}
158-
}
159-
160143
export const nonCodeMetaEmpty = () => {
161144
return { nonCodeNodes: {}, startNodes: [], start: 0, end: 0 }
162145
}

src/lang/modifyAst.ts

Lines changed: 4 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { NonCodeMeta } from '@rust/kcl-lib/bindings/NonCodeMeta'
55

66
import {
77
createArrayExpression,
8-
createCallExpressionStdLib,
98
createCallExpressionStdLibKw,
109
createExpressionStatement,
1110
createIdentifier,
@@ -14,7 +13,6 @@ import {
1413
createLabeledArg,
1514
createLiteral,
1615
createLocalName,
17-
createObjectExpression,
1816
createPipeExpression,
1917
createVariableDeclaration,
2018
findUniqueName,
@@ -439,9 +437,11 @@ export function loftSketches(
439437
const modifiedAst = structuredClone(node)
440438
const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.LOFT)
441439
const elements = declarators.map((d) => createLocalName(d.id.name))
442-
const loft = createCallExpressionStdLib('loft', [
440+
const loft = createCallExpressionStdLibKw(
441+
'loft',
443442
createArrayExpression(elements),
444-
])
443+
[]
444+
)
445445
const declaration = createVariableDeclaration(name, loft)
446446
modifiedAst.body.push(declaration)
447447
const pathToNode: PathToNode = [
@@ -574,101 +574,6 @@ export function addSweep({
574574
}
575575
}
576576

577-
export function revolveSketch(
578-
node: Node<Program>,
579-
pathToNode: PathToNode,
580-
shouldPipe = false,
581-
angle: Expr = createLiteral(4)
582-
):
583-
| {
584-
modifiedAst: Node<Program>
585-
pathToNode: PathToNode
586-
pathToRevolveArg: PathToNode
587-
}
588-
| Error {
589-
const _node = structuredClone(node)
590-
const _node1 = getNodeFromPath(_node, pathToNode)
591-
if (err(_node1)) return _node1
592-
const { node: sketchExpression } = _node1
593-
594-
// determine if sketchExpression is in a pipeExpression or not
595-
const _node2 = getNodeFromPath<PipeExpression>(
596-
_node,
597-
pathToNode,
598-
'PipeExpression'
599-
)
600-
if (err(_node2)) return _node2
601-
const { node: pipeExpression } = _node2
602-
603-
const isInPipeExpression = pipeExpression.type === 'PipeExpression'
604-
605-
const _node3 = getNodeFromPath<VariableDeclarator>(
606-
_node,
607-
pathToNode,
608-
'VariableDeclarator'
609-
)
610-
if (err(_node3)) return _node3
611-
const { node: variableDeclarator, shallowPath: pathToDecleration } = _node3
612-
613-
const revolveCall = createCallExpressionStdLib('revolve', [
614-
createObjectExpression({
615-
angle: angle,
616-
// TODO: hard coded 'X' axis for revolve MVP, should be changed.
617-
axis: createLocalName('X'),
618-
}),
619-
createLocalName(variableDeclarator.id.name),
620-
])
621-
622-
if (shouldPipe) {
623-
const pipeChain = createPipeExpression(
624-
isInPipeExpression
625-
? [...pipeExpression.body, revolveCall]
626-
: [sketchExpression as any, revolveCall]
627-
)
628-
629-
variableDeclarator.init = pipeChain
630-
const pathToRevolveArg: PathToNode = [
631-
...pathToDecleration,
632-
['init', 'VariableDeclarator'],
633-
['body', ''],
634-
[pipeChain.body.length - 1, 'index'],
635-
['arguments', 'CallExpression'],
636-
[0, 'index'],
637-
]
638-
639-
return {
640-
modifiedAst: _node,
641-
pathToNode,
642-
pathToRevolveArg,
643-
}
644-
}
645-
646-
// We're not creating a pipe expression,
647-
// but rather a separate constant for the extrusion
648-
const name = findUniqueName(node, KCL_DEFAULT_CONSTANT_PREFIXES.REVOLVE)
649-
const VariableDeclaration = createVariableDeclaration(name, revolveCall)
650-
const sketchIndexInPathToNode =
651-
pathToDecleration.findIndex((a) => a[0] === 'body') + 1
652-
const sketchIndexInBody = pathToDecleration[sketchIndexInPathToNode][0]
653-
if (typeof sketchIndexInBody !== 'number')
654-
return new Error('expected sketchIndexInBody to be a number')
655-
_node.body.splice(sketchIndexInBody + 1, 0, VariableDeclaration)
656-
657-
const pathToRevolveArg: PathToNode = [
658-
['body', ''],
659-
[sketchIndexInBody + 1, 'index'],
660-
['declaration', 'VariableDeclaration'],
661-
['init', 'VariableDeclarator'],
662-
['arguments', 'CallExpression'],
663-
[0, 'index'],
664-
]
665-
return {
666-
modifiedAst: _node,
667-
pathToNode: [...pathToNode.slice(0, -1), [-1, 'index']],
668-
pathToRevolveArg,
669-
}
670-
}
671-
672577
export function sketchOnExtrudedFace(
673578
node: Node<Program>,
674579
sketchPathToNode: PathToNode,

src/lang/modifyAst/deleteFromSelection.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,19 @@ export async function deleteFromSelection(
207207
extrudeNameToDelete = dec.id.name
208208
}
209209
if (
210-
// TODO: This is wrong, loft is now a CallExpressionKw.
211-
dec.init.type === 'CallExpression' &&
212-
dec.init.callee.name.name === 'loft' &&
213-
dec.init.arguments?.[0].type === 'ArrayExpression' &&
214-
dec.init.arguments?.[0].elements.some(
215-
(a) => a.type === 'Name' && a.name.name === varDecName
216-
)
210+
(dec.init.type === 'CallExpressionKw' &&
211+
dec.init.callee.name.name === 'loft' &&
212+
dec.init.unlabeled !== null &&
213+
dec.init.unlabeled.type === 'ArrayExpression' &&
214+
dec.init.unlabeled.elements.some(
215+
(a) => a.type === 'Name' && a.name.name === varDecName
216+
)) ||
217+
(dec.init.type === 'CallExpression' &&
218+
dec.init.callee.name.name === 'loft' &&
219+
dec.init.arguments?.[0].type === 'ArrayExpression' &&
220+
dec.init.arguments?.[0].elements.some(
221+
(a) => a.type === 'Name' && a.name.name === varDecName
222+
))
217223
) {
218224
pathToNode = path
219225
extrudeNameToDelete = dec.id.name

src/lang/modifyAst/tagManagement.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import type {
2222
Program,
2323
} from '@src/lang/wasm'
2424
import {
25-
createCallExpressionStdLib,
2625
createArrayExpression,
2726
createLocalName,
2827
createCallExpressionStdLibKw,
@@ -194,12 +193,16 @@ export function createTagExpressions(
194193

195194
// Modify the tag based on selectionType
196195
if (artifact.type === 'sweepEdge' && artifact.subType === 'opposite') {
197-
tagCall = createCallExpressionStdLib('getOppositeEdge', [tagCall])
196+
tagCall = createCallExpressionStdLibKw('getOppositeEdge', tagCall, [])
198197
} else if (
199198
artifact.type === 'sweepEdge' &&
200199
artifact.subType === 'adjacent'
201200
) {
202-
tagCall = createCallExpressionStdLib('getNextAdjacentEdge', [tagCall])
201+
tagCall = createCallExpressionStdLibKw(
202+
'getNextAdjacentEdge',
203+
tagCall,
204+
[]
205+
)
203206
}
204207
return tagCall
205208
}

src/lang/queryAst.test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { Name } from '@rust/kcl-lib/bindings/Name'
33
import {
44
createArrayExpression,
55
createCallExpression,
6-
createCallExpressionStdLib,
6+
createCallExpressionStdLibKw,
7+
createLabeledArg,
78
createLiteral,
89
createPipeSubstitution,
910
} from '@src/lang/create'
@@ -28,6 +29,7 @@ import { assertParse, recast } from '@src/lang/wasm'
2829
import { initPromise } from '@src/lang/wasmUtils'
2930
import { enginelessExecutor } from '@src/lib/testHelpers'
3031
import { err } from '@src/lib/trap'
32+
import { ARG_END_ABSOLUTE } from '@src/lang/constants'
3133

3234
beforeAll(async () => {
3335
await initPromise
@@ -721,20 +723,23 @@ describe('Testing specific sketch getNodeFromPath workflow', () => {
721723
variables: {},
722724
pathToNode: sketchPathToNode,
723725
expressions: [
724-
createCallExpressionStdLib(
725-
'lineTo', // We are forcing lineTo!
726-
[
726+
createCallExpressionStdLibKw('line', null, [
727+
createLabeledArg(
728+
ARG_END_ABSOLUTE,
727729
createArrayExpression([
728-
createCallExpressionStdLib('profileStartX', [
730+
createCallExpressionStdLibKw(
731+
'profileStartX',
729732
createPipeSubstitution(),
730-
]),
731-
createCallExpressionStdLib('profileStartY', [
733+
[]
734+
),
735+
createCallExpressionStdLibKw(
736+
'profileStartY',
732737
createPipeSubstitution(),
733-
]),
734-
]),
735-
createPipeSubstitution(),
736-
]
737-
),
738+
[]
739+
),
740+
])
741+
),
742+
]),
738743
],
739744
})
740745
if (err(modifiedAst)) throw modifiedAst
@@ -748,7 +753,7 @@ describe('Testing specific sketch getNodeFromPath workflow', () => {
748753
|> xLine(length = -0.15)
749754
|> line([-0.02, 0.21], %)
750755
|> line([-0.08, 0.05], %)
751-
|> lineTo([profileStartX(%), profileStartY(%)], %)
756+
|> line(endAbsolute = [profileStartX(%), profileStartY(%)])
752757
`
753758
expect(recasted).toEqual(expectedCode)
754759
})

src/lib/rectangleTool.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import {
1010
createArrayExpression,
1111
createBinaryExpression,
12-
createCallExpressionStdLib,
1312
createCallExpressionStdLibKw,
1413
createLabeledArg,
1514
createLiteral,
@@ -64,16 +63,16 @@ export const getRectangleCallExpressions = (
6463
),
6564
angledLine(
6665
createBinaryExpression([
67-
createCallExpressionStdLib('segAng', [createLocalName(tag)]),
66+
createCallExpressionStdLibKw('segAng', createLocalName(tag), []),
6867
'+',
6968
createLiteral(90),
7069
]), // 90 offset from the previous line
7170
createLiteral(0) // This will be the height of the rectangle
7271
),
7372
angledLine(
74-
createCallExpressionStdLib('segAng', [createLocalName(tag)]), // same angle as the first line
73+
createCallExpressionStdLibKw('segAng', createLocalName(tag), []), // same angle as the first line
7574
createUnaryExpression(
76-
createCallExpressionStdLib('segLen', [createLocalName(tag)]),
75+
createCallExpressionStdLibKw('segLen', createLocalName(tag), []),
7776
'-'
7877
) // negative height
7978
),
@@ -120,7 +119,7 @@ export function updateRectangleSketch(
120119
'angle',
121120
secondEdge,
122121
createBinaryExpression([
123-
createCallExpressionStdLib('segAng', [createLocalName(tag)]),
122+
createCallExpressionStdLibKw('segAng', createLocalName(tag), []),
124123
Math.sign(y) === Math.sign(x) ? '+' : '-',
125124
createLiteral(90),
126125
])
@@ -198,7 +197,7 @@ export function updateCenterRectangleSketch(
198197
}
199198
oldAngleOperator = oldAngle.operator
200199
const newAngle = createBinaryExpression([
201-
createCallExpressionStdLib('segAng', [createLocalName(tag)]),
200+
createCallExpressionStdLibKw('segAng', createLocalName(tag), []),
202201
oldAngleOperator,
203202
createLiteral(90),
204203
])

0 commit comments

Comments
 (0)