Skip to content

Commit 3a7f58d

Browse files
authored
Use named constants for relativeTo in Shell codemod (#8893)
* Use named constants for relativeTo in Shell codemod Fixes #8892 * Polish * Fix test
1 parent 076725d commit 3a7f58d

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

e2e/playwright/point-click.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ sketch002 = startSketchOn(plane001)
11531153
sketch001 = startSketchOn(XZ)
11541154
profile001 = ${circleCode}`
11551155
const sweepDeclaration = 'sweep001 = sweep(profile001, path = helix001)'
1156-
const editedSweepDeclaration = `sweep001 = sweep(profile001, path = helix001, relativeTo = "sketchPlane")`
1156+
const editedSweepDeclaration = `sweep001 = sweep(profile001, path = helix001, relativeTo = sweep::SKETCH_PLANE)`
11571157

11581158
await context.addInitScript((initialCode) => {
11591159
localStorage.setItem('persistCode', initialCode)
@@ -1235,11 +1235,11 @@ profile001 = ${circleCode}`
12351235
highlightedHeaderArg: 'relativeTo',
12361236
commandName: 'Sweep',
12371237
})
1238-
await cmdBar.selectOption({ name: 'sketchPlane' }).click()
1238+
await cmdBar.selectOption({ name: 'Sketch Plane' }).click()
12391239
await cmdBar.expectState({
12401240
stage: 'review',
12411241
headerArguments: {
1242-
RelativeTo: 'sketchPlane',
1242+
RelativeTo: 'SKETCH_PLANE',
12431243
},
12441244
commandName: 'Sweep',
12451245
})

src/lang/modifyAst/sweeps.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ profile002 = startProfile(sketch002, at = [0, 0])
542542
kclManagerInThisFile
543543
)
544544
const sectional = true
545-
const relativeTo = 'sketchPlane'
545+
const relativeTo = 'SKETCH_PLANE'
546546
const result = addSweep({ ast, sketches, path, sectional, relativeTo })
547547
if (err(result)) throw result
548548
await runNewAstAndCheckForSweep(result.modifiedAst, rustContextInThisFile)
@@ -552,7 +552,7 @@ profile002 = startProfile(sketch002, at = [0, 0])
552552
profile001,
553553
path = profile002,
554554
sectional = true,
555-
relativeTo = "sketchPlane",
555+
relativeTo = sweep::SKETCH_PLANE,
556556
)`)
557557
})
558558

@@ -562,15 +562,15 @@ sweep001 = sweep(
562562
profile001,
563563
path = profile002,
564564
sectional = true,
565-
relativeTo = 'sketchPlane',
565+
relativeTo = sweep::SKETCH_PLANE,
566566
)`
567567
const { ast, sketches, path } = await getAstAndSketchesForSweep(
568568
circleAndLineCodeWithSweep,
569569
instanceInThisFile,
570570
kclManagerInThisFile
571571
)
572572
const sectional = false
573-
const relativeTo = 'trajectoryCurve'
573+
const relativeTo = 'TRAJECTORY'
574574
const nodeToEdit = createPathToNodeForLastVariable(ast)
575575
const result = addSweep({
576576
ast,
@@ -585,7 +585,7 @@ sweep001 = sweep(
585585
const newCode = recast(result.modifiedAst, instanceInThisFile)
586586
expect(newCode).toContain(circleAndLineCode)
587587
expect(newCode).toContain(
588-
`sweep001 = sweep(profile001, path = profile002, relativeTo = "trajectoryCurve")`
588+
`sweep001 = sweep(profile001, path = profile002, relativeTo = sweep::TRAJECTORY)`
589589
)
590590
})
591591

src/lang/modifyAst/sweeps.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { OpArg, OpKclValue } from '@rust/kcl-lib/bindings/Operation'
33

44
import {
55
createCallExpressionStdLibKw,
6+
createName,
67
createLabeledArg,
78
createLiteral,
89
createLocalName,
@@ -202,6 +203,14 @@ export function addExtrude({
202203
}
203204
}
204205

206+
// From rust/kcl-lib/std/sweep.kcl
207+
export type SweepRelativeTo = 'SKETCH_PLANE' | 'TRAJECTORY'
208+
export const SWEEP_CONSTANTS: Record<string, SweepRelativeTo> = {
209+
SKETCH_PLANE: 'SKETCH_PLANE',
210+
TRAJECTORY: 'TRAJECTORY',
211+
}
212+
export const SWEEP_MODULE = 'sweep'
213+
205214
export function addSweep({
206215
ast,
207216
sketches,
@@ -216,7 +225,7 @@ export function addSweep({
216225
sketches: Selections
217226
path: Selections
218227
sectional?: boolean
219-
relativeTo?: string
228+
relativeTo?: SweepRelativeTo
220229
tagStart?: string
221230
tagEnd?: string
222231
nodeToEdit?: PathToNode
@@ -254,7 +263,7 @@ export function addSweep({
254263
? [createLabeledArg('sectional', createLiteral(sectional))]
255264
: []
256265
const relativeToExpr = relativeTo
257-
? [createLabeledArg('relativeTo', createLiteral(relativeTo))]
266+
? [createLabeledArg('relativeTo', createName([SWEEP_MODULE], relativeTo))]
258267
: []
259268
const tagStartExpr = tagStart
260269
? [createLabeledArg('tagStart', createTagDeclarator(tagStart))]

src/lib/commandBarConfigs/modelingCommandConfig.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
addLoft,
4949
addRevolve,
5050
addSweep,
51+
type SweepRelativeTo,
5152
} from '@src/lang/modifyAst/sweeps'
5253
import { mockExecAstAndReportErrors } from '@src/lang/modelingWorkflows'
5354
import { addHole, addOffsetPlane, addShell } from '@src/lang/modifyAst/faces'
@@ -148,7 +149,7 @@ export type ModelingCommandSchema = {
148149
path: Selections
149150
sectional?: boolean
150151
// TODO: figure out if we should expose `tolerance` or not
151-
relativeTo?: string
152+
relativeTo?: SweepRelativeTo
152153
tagStart?: string
153154
tagEnd?: string
154155
}
@@ -659,8 +660,8 @@ export const modelingMachineCommandConfig: StateMachineCommandSetConfig<
659660
inputType: 'options',
660661
required: false,
661662
options: [
662-
{ name: 'sketchPlane', value: 'sketchPlane' },
663-
{ name: 'trajectoryCurve', value: 'trajectoryCurve' },
663+
{ name: 'Sketch Plane', value: 'SKETCH_PLANE' },
664+
{ name: 'Trajectory Curve', value: 'TRAJECTORY' },
664665
],
665666
},
666667
tagStart: {

src/lib/operations.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212
import {
1313
retrieveAxisOrEdgeSelectionsFromOpArg,
1414
retrieveTagDeclaratorFromOpArg,
15+
SWEEP_CONSTANTS,
16+
SWEEP_MODULE,
17+
type SweepRelativeTo,
1518
} from '@src/lang/modifyAst/sweeps'
1619
import {
1720
getNodeFromPath,
@@ -899,15 +902,22 @@ const prepareToEditSweep: PrepareToEditCallback = async ({ operation }) => {
899902
) === 'true'
900903
}
901904

902-
let relativeTo: string | undefined
905+
let relativeTo: SweepRelativeTo | undefined
903906
if (
904907
'relativeTo' in operation.labeledArgs &&
905908
operation.labeledArgs.relativeTo
906909
) {
907-
relativeTo = codeManager.code.slice(
910+
const result = codeManager.code.slice(
908911
operation.labeledArgs.relativeTo.sourceRange[0],
909912
operation.labeledArgs.relativeTo.sourceRange[1]
910913
)
914+
if (result === `${SWEEP_MODULE}::${SWEEP_CONSTANTS.SKETCH_PLANE}`) {
915+
relativeTo = SWEEP_CONSTANTS.SKETCH_PLANE
916+
} else if (result === `${SWEEP_MODULE}::${SWEEP_CONSTANTS.TRAJECTORY}`) {
917+
relativeTo = SWEEP_CONSTANTS.TRAJECTORY
918+
} else {
919+
return { reason: "Couldn't retrieve relativeTo argument" }
920+
}
911921
}
912922

913923
// tagStart and tagEng arguments

0 commit comments

Comments
 (0)