Fix bug with undo startSketchOn removing existing sketch (#6834)

* Fix bug with `undo startSketchOn` removing existing sketch

Fixes #6822, I believe. in this case the `variableName` was not being
marked as in-use, so I just logged out the AST and made sure this case
was covered. @Irev-Dev this is probably worth a check from you.

* Add a regression test

It's an E2E test because I'm being lazy, but it should probably be an
XState unit test at some point.

* check what's checked

---------

Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
This commit is contained in:
Frank Noirot
2025-05-12 02:55:39 -04:00
committed by GitHub
parent 1a325d0b29
commit 22857d77e9
2 changed files with 50 additions and 7 deletions

View File

@ -841,6 +841,40 @@ washer = extrude(washerSketch, length = thicknessMax)`
await editor.expectEditor.toContain('@settings(defaultLengthUnit = yd)') await editor.expectEditor.toContain('@settings(defaultLengthUnit = yd)')
}) })
}) })
test('Exiting existing sketch without editing should not delete it', async ({
page,
editor,
homePage,
context,
toolbar,
scene,
cmdBar,
}) => {
await context.folderSetupFn(async (dir) => {
const testDir = path.join(dir, 'test')
await fsp.mkdir(testDir, { recursive: true })
await fsp.writeFile(
path.join(testDir, 'main.kcl'),
`s1 = startSketchOn(XY)
|> startProfile(at = [0, 25])
|> xLine(endAbsolute = -15 + 1.5)
s2 = startSketchOn(XY)
|> startProfile(at = [25, 0])
|> yLine(endAbsolute = -15 + 1.5)`,
'utf-8'
)
})
await homePage.openProject('test')
await scene.settled(cmdBar)
await toolbar.waitForFeatureTreeToBeBuilt()
await toolbar.editSketch(1)
await page.waitForTimeout(1000) // Just hang out for a second
await toolbar.exitSketch()
await editor.expectEditor.toContain('s2 = startSketchOn(XY)')
})
}) })
async function clickExportButton(page: Page) { async function clickExportButton(page: Page) {

View File

@ -751,13 +751,22 @@ export const ModelingMachineProvider = ({
if (err(varDec)) return reject(new Error('No varDec')) if (err(varDec)) return reject(new Error('No varDec'))
const variableName = varDec.node.declaration.id.name const variableName = varDec.node.declaration.id.name
let isIdentifierUsed = false let isIdentifierUsed = false
const isInitAPipe =
varDec.node.declaration.init.type === 'PipeExpression'
if (isInitAPipe) {
isIdentifierUsed = true
} else {
traverse(newAst, { traverse(newAst, {
enter: (node) => { enter: (node) => {
if (node.type === 'Name' && node.name.name === variableName) { if (
node.type === 'Name' &&
node.name.name === variableName
) {
isIdentifierUsed = true isIdentifierUsed = true
} }
}, },
}) })
}
if (isIdentifierUsed) return if (isIdentifierUsed) return
// remove body item at varDecIndex // remove body item at varDecIndex