Update Insert, Transform, and Clone codemods to match new import behavior (#6577)

* Fix operations to reflect concurrent module import behavior

* Add new generated output

* Fix root module import tracking

* Rename test so that it's easier to filter

* Update output ops

* Fix clippy

* Update output after rebase

* Update multi-axis-robot flowchart output

* Disable e2e tests until future PR

* WIP: Update Insert and Transform codemods to match new import behavior
Fixes #6570

* Fix operations to reflect concurrent module import behavior

* Add new generated output

* Fix root module import tracking

* Rename test so that it's easier to filter

* Update output ops

* Fix clippy

* Update output after rebase

* Disable e2e tests until future PR

* Update one of the tests

* Somewhat working very ugly translate

* Working translate and rotate

* Fix deletion

* Clean up things and disable tests deleting the *first* import due to unclear issue

* Fix Clone

* Clean up ahead of review

* Support cases with translate and rotate in two different pipes (but not for deletion)

* Fix generated output; probably from recent merge

* Find all pipes and look for last in most cases, adding fallbacks to set translate/rotate on the right ones

* More fixups

* Delete unused snap file

* Update src/lang/queryAst.ts

Co-authored-by: Jonathan Tran <jonnytran@gmail.com>

* Change lint ignore to be more specific

* Add test that checks we can still translate, rotate, and delete weird import code

* Clean up

---------

Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
This commit is contained in:
Pierre Jacquier
2025-04-30 13:07:39 -04:00
committed by GitHub
parent 0002295cdf
commit 58a81da039
9 changed files with 497 additions and 148 deletions

View File

@ -1,4 +1,5 @@
import type { Models } from '@kittycad/lib'
import type { ImportStatement } from '@rust/kcl-lib/bindings/ImportStatement'
import type { Node } from '@rust/kcl-lib/bindings/Node'
@ -8,7 +9,11 @@ import {
createObjectExpression,
} from '@src/lang/create'
import { deleteEdgeTreatment } from '@src/lang/modifyAst/addEdgeTreatment'
import { getNodeFromPath, traverse } from '@src/lang/queryAst'
import {
getNodeFromPath,
traverse,
findPipesWithImportAlias,
} from '@src/lang/queryAst'
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
import {
expandCap,
@ -22,7 +27,6 @@ import type {
ArtifactGraph,
CallExpression,
CallExpressionKw,
ExpressionStatement,
KclValue,
PathToNode,
PipeExpression,
@ -120,45 +124,27 @@ export async function deleteFromSelection(
}
// Module import and expression case, need to find and delete both
const statement = getNodeFromPath<ExpressionStatement>(
const statement = getNodeFromPath<ImportStatement>(
astClone,
selection.codeRef.pathToNode,
'ExpressionStatement'
'ImportStatement'
)
if (!err(statement) && statement.node.type === 'ExpressionStatement') {
let expressionIndexToDelete: number | undefined
let importAliasToDelete: string | undefined
if (
statement.node.expression.type === 'Name' &&
statement.node.expression.name.type === 'Identifier'
) {
expressionIndexToDelete = Number(selection.codeRef.pathToNode[1][0])
importAliasToDelete = statement.node.expression.name.name
} else if (
statement.node.expression.type === 'PipeExpression' &&
statement.node.expression.body[0].type === 'Name' &&
statement.node.expression.body[0].name.type === 'Identifier'
) {
expressionIndexToDelete = Number(selection.codeRef.pathToNode[1][0])
importAliasToDelete = statement.node.expression.body[0].name.name
} else {
return new Error('Expected expression to be a Name or PipeExpression')
}
astClone.body.splice(expressionIndexToDelete, 1)
const importIndexToDelete = astClone.body.findIndex(
(n) =>
n.type === 'ImportStatement' &&
n.selector.type === 'None' &&
n.selector.alias?.type === 'Identifier' &&
n.selector.alias.name === importAliasToDelete
)
if (importIndexToDelete >= 0) {
astClone.body.splice(importIndexToDelete, 1)
} else {
return new Error("Couldn't find import to delete")
if (
!err(statement) &&
statement.node.type === 'ImportStatement' &&
selection.codeRef.pathToNode[1] &&
typeof selection.codeRef.pathToNode[1][0] === 'number'
) {
const pipes = findPipesWithImportAlias(ast, selection.codeRef.pathToNode)
for (const { pathToNode: pathToPipeNode } of pipes.reverse()) {
if (typeof pathToPipeNode[1][0] === 'number') {
const pipeWithImportAliasIndex = pathToPipeNode[1][0]
astClone.body.splice(pipeWithImportAliasIndex, 1)
}
}
const importIndex = selection.codeRef.pathToNode[1][0]
astClone.body.splice(importIndex, 1)
return astClone
}