Skip to content

Commit 22857d7

Browse files
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 <[email protected]>
1 parent 1a325d0 commit 22857d7

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

e2e/playwright/regression-tests.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,40 @@ washer = extrude(washerSketch, length = thicknessMax)`
841841
await editor.expectEditor.toContain('@settings(defaultLengthUnit = yd)')
842842
})
843843
})
844+
845+
test('Exiting existing sketch without editing should not delete it', async ({
846+
page,
847+
editor,
848+
homePage,
849+
context,
850+
toolbar,
851+
scene,
852+
cmdBar,
853+
}) => {
854+
await context.folderSetupFn(async (dir) => {
855+
const testDir = path.join(dir, 'test')
856+
await fsp.mkdir(testDir, { recursive: true })
857+
await fsp.writeFile(
858+
path.join(testDir, 'main.kcl'),
859+
`s1 = startSketchOn(XY)
860+
|> startProfile(at = [0, 25])
861+
|> xLine(endAbsolute = -15 + 1.5)
862+
s2 = startSketchOn(XY)
863+
|> startProfile(at = [25, 0])
864+
|> yLine(endAbsolute = -15 + 1.5)`,
865+
'utf-8'
866+
)
867+
})
868+
869+
await homePage.openProject('test')
870+
await scene.settled(cmdBar)
871+
await toolbar.waitForFeatureTreeToBeBuilt()
872+
await toolbar.editSketch(1)
873+
await page.waitForTimeout(1000) // Just hang out for a second
874+
await toolbar.exitSketch()
875+
876+
await editor.expectEditor.toContain('s2 = startSketchOn(XY)')
877+
})
844878
})
845879

846880
async function clickExportButton(page: Page) {

src/components/ModelingMachineProvider.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -751,13 +751,22 @@ export const ModelingMachineProvider = ({
751751
if (err(varDec)) return reject(new Error('No varDec'))
752752
const variableName = varDec.node.declaration.id.name
753753
let isIdentifierUsed = false
754-
traverse(newAst, {
755-
enter: (node) => {
756-
if (node.type === 'Name' && node.name.name === variableName) {
757-
isIdentifierUsed = true
758-
}
759-
},
760-
})
754+
const isInitAPipe =
755+
varDec.node.declaration.init.type === 'PipeExpression'
756+
if (isInitAPipe) {
757+
isIdentifierUsed = true
758+
} else {
759+
traverse(newAst, {
760+
enter: (node) => {
761+
if (
762+
node.type === 'Name' &&
763+
node.name.name === variableName
764+
) {
765+
isIdentifierUsed = true
766+
}
767+
},
768+
})
769+
}
761770
if (isIdentifierUsed) return
762771

763772
// remove body item at varDecIndex

0 commit comments

Comments
 (0)