Start cleaning up unneeded positional arg code
This commit is contained in:
@ -3,17 +3,14 @@ import type { Node } from '@rust/kcl-lib/bindings/Node'
|
||||
import { getNodeFromPath } from '@src/lang/queryAst'
|
||||
import { getNodePathFromSourceRange } from '@src/lang/queryAstNodePathUtils'
|
||||
import {
|
||||
addCloseToPipe,
|
||||
addNewSketchLn,
|
||||
addTagForSketchOnFace,
|
||||
changeSketchArguments,
|
||||
getConstraintInfo,
|
||||
getConstraintInfoKw,
|
||||
getXComponent,
|
||||
getYComponent,
|
||||
} from '@src/lang/std/sketch'
|
||||
import { topLevelRange } from '@src/lang/util'
|
||||
import type { CallExpression, CallExpressionKw } from '@src/lang/wasm'
|
||||
import type { CallExpressionKw } from '@src/lang/wasm'
|
||||
import { assertParse, recast } from '@src/lang/wasm'
|
||||
import { initPromise } from '@src/lang/wasmUtils'
|
||||
import { enginelessExecutor } from '@src/lib/testHelpers'
|
||||
@ -140,74 +137,6 @@ describe('testing changeSketchArguments', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('testing addNewSketchLn', () => {
|
||||
const lineToChange = 'line(endAbsolute = [-1.59, -1.54])'
|
||||
test('addNewSketchLn', async () => {
|
||||
// Enable rotations #152
|
||||
const code = `
|
||||
mySketch001 = startSketchOn(XY)
|
||||
|> startProfile(at = [0, 0])
|
||||
// |> rx(45, %)
|
||||
|> line(endAbsolute = [-1.59, -1.54])
|
||||
|> line(endAbsolute = [0.46, -5.82])`
|
||||
const ast = assertParse(code)
|
||||
|
||||
const execState = await enginelessExecutor(ast)
|
||||
const sourceStart = code.indexOf(lineToChange)
|
||||
expect(sourceStart).toBe(87)
|
||||
const newSketchLnRetVal = addNewSketchLn({
|
||||
node: ast,
|
||||
variables: execState.variables,
|
||||
input: {
|
||||
type: 'straight-segment',
|
||||
from: [0, 0],
|
||||
to: [2, 3],
|
||||
},
|
||||
fnName: 'lineTo',
|
||||
pathToNode: [
|
||||
['body', ''],
|
||||
[0, 'index'],
|
||||
['declaration', 'VariableDeclaration'],
|
||||
['init', 'VariableDeclarator'],
|
||||
],
|
||||
})
|
||||
if (err(newSketchLnRetVal)) return newSketchLnRetVal
|
||||
|
||||
// Enable rotations #152
|
||||
let expectedCode = `mySketch001 = startSketchOn(XY)
|
||||
|> startProfile(at = [0, 0])
|
||||
// |> rx(45, %)
|
||||
|> line(endAbsolute = [-1.59, -1.54])
|
||||
|> line(endAbsolute = [0.46, -5.82])
|
||||
|> line(endAbsolute = [2, 3])
|
||||
`
|
||||
|
||||
const { modifiedAst } = newSketchLnRetVal
|
||||
expect(recast(modifiedAst)).toBe(expectedCode)
|
||||
|
||||
const modifiedAst2 = addCloseToPipe({
|
||||
node: ast,
|
||||
variables: execState.variables,
|
||||
pathToNode: [
|
||||
['body', ''],
|
||||
[0, 'index'],
|
||||
['declaration', 'VariableDeclaration'],
|
||||
['init', 'VariableDeclarator'],
|
||||
],
|
||||
})
|
||||
if (err(modifiedAst2)) return modifiedAst2
|
||||
|
||||
expectedCode = `mySketch001 = startSketchOn(XY)
|
||||
|> startProfile(at = [0, 0])
|
||||
// |> rx(45, %)
|
||||
|> line(endAbsolute = [-1.59, -1.54])
|
||||
|> line(endAbsolute = [0.46, -5.82])
|
||||
|> close()
|
||||
`
|
||||
expect(recast(modifiedAst2)).toBe(expectedCode)
|
||||
})
|
||||
})
|
||||
|
||||
describe('testing addTagForSketchOnFace', () => {
|
||||
it('needs to be in it', async () => {
|
||||
const originalLine = 'line(endAbsolute = [-1.59, -1.54])'
|
||||
@ -669,16 +598,11 @@ describe('testing getConstraintInfo', () => {
|
||||
const sourceRange = topLevelRange(start, start + functionName.length)
|
||||
if (err(ast)) return ast
|
||||
const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
|
||||
const callExp = getNodeFromPath<Node<CallExpression | CallExpressionKw>>(
|
||||
ast,
|
||||
pathToNode,
|
||||
['CallExpression', 'CallExpressionKw']
|
||||
)
|
||||
const callExp = getNodeFromPath<Node<CallExpressionKw>>(ast, pathToNode, [
|
||||
'CallExpressionKw',
|
||||
])
|
||||
if (err(callExp)) return callExp
|
||||
const result =
|
||||
callExp.node.type === 'CallExpression'
|
||||
? getConstraintInfo(callExp.node, code, pathToNode)
|
||||
: getConstraintInfoKw(callExp.node, code, pathToNode)
|
||||
const result = getConstraintInfoKw(callExp.node, code, pathToNode)
|
||||
expect(result).toEqual(expected)
|
||||
})
|
||||
})
|
||||
@ -830,16 +754,11 @@ describe('testing getConstraintInfo', () => {
|
||||
const sourceRange = topLevelRange(start, start + functionName.length)
|
||||
if (err(ast)) return ast
|
||||
const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
|
||||
const callExp = getNodeFromPath<Node<CallExpression | CallExpressionKw>>(
|
||||
ast,
|
||||
pathToNode,
|
||||
['CallExpression', 'CallExpressionKw']
|
||||
)
|
||||
const callExp = getNodeFromPath<Node<CallExpressionKw>>(ast, pathToNode, [
|
||||
'CallExpressionKw',
|
||||
])
|
||||
if (err(callExp)) return callExp
|
||||
const result =
|
||||
callExp.node.type === 'CallExpression'
|
||||
? getConstraintInfo(callExp.node, code, pathToNode)
|
||||
: getConstraintInfoKw(callExp.node, code, pathToNode)
|
||||
const result = getConstraintInfoKw(callExp.node, code, pathToNode)
|
||||
expect(result).toEqual(expected)
|
||||
})
|
||||
})
|
||||
@ -1193,17 +1112,12 @@ describe('testing getConstraintInfo', () => {
|
||||
const sourceRange = topLevelRange(start, start + functionName.length)
|
||||
if (err(ast)) return ast
|
||||
const pathToNode = getNodePathFromSourceRange(ast, sourceRange)
|
||||
const callExp = getNodeFromPath<Node<CallExpression | CallExpressionKw>>(
|
||||
ast,
|
||||
pathToNode,
|
||||
['CallExpression', 'CallExpressionKw']
|
||||
)
|
||||
const callExp = getNodeFromPath<Node<CallExpressionKw>>(ast, pathToNode, [
|
||||
'CallExpressionKw',
|
||||
])
|
||||
if (err(callExp)) return callExp
|
||||
|
||||
const result =
|
||||
callExp.node.type === 'CallExpression'
|
||||
? getConstraintInfo(callExp.node, code, pathToNode)
|
||||
: getConstraintInfoKw(callExp.node, code, pathToNode)
|
||||
const result = getConstraintInfoKw(callExp.node, code, pathToNode)
|
||||
expect(result).toEqual(expected)
|
||||
})
|
||||
})
|
||||
|
||||
@ -60,7 +60,6 @@ import type {
|
||||
SegmentInputs,
|
||||
SimplifiedArgDetails,
|
||||
SingleValueInput,
|
||||
SketchLineHelper,
|
||||
SketchLineHelperKw,
|
||||
addCall,
|
||||
} from '@src/lang/std/stdTypes'
|
||||
@ -430,18 +429,6 @@ const horzVertConstraintInfoHelper = (
|
||||
]
|
||||
}
|
||||
|
||||
function getTag(index = 2): SketchLineHelper['getTag'] {
|
||||
return (callExp: CallExpression) => {
|
||||
if (callExp.type !== 'CallExpression')
|
||||
return new Error('Not a CallExpression')
|
||||
const arg = callExp.arguments?.[index]
|
||||
if (!arg) return new Error('No argument')
|
||||
if (arg.type !== 'TagDeclarator')
|
||||
return new Error('Tag not a TagDeclarator')
|
||||
return arg.value
|
||||
}
|
||||
}
|
||||
|
||||
function getTagKwArg(): SketchLineHelperKw['getTag'] {
|
||||
return (callExp: CallExpressionKw) => {
|
||||
if (callExp.type !== 'CallExpressionKw')
|
||||
@ -2947,7 +2934,7 @@ export const angledLineThatIntersects: SketchLineHelperKw = {
|
||||
},
|
||||
}
|
||||
|
||||
export const updateStartProfileAtArgs: SketchLineHelper['updateArgs'] = ({
|
||||
export const updateStartProfileAtArgs: SketchLineHelperKw['updateArgs'] = ({
|
||||
node,
|
||||
pathToNode,
|
||||
input,
|
||||
@ -2995,10 +2982,6 @@ export const updateStartProfileAtArgs: SketchLineHelper['updateArgs'] = ({
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Just remove this.
|
||||
export const sketchLineHelperMap: { [key: string]: SketchLineHelper } =
|
||||
{} as const
|
||||
|
||||
export const sketchLineHelperMapKw: { [key: string]: SketchLineHelperKw } = {
|
||||
arc,
|
||||
arcTo,
|
||||
@ -3045,19 +3028,6 @@ export function changeSketchArguments(
|
||||
const { node: callExpression, shallowPath } = nodeMeta
|
||||
|
||||
const fnName = callExpression?.callee?.name.name
|
||||
if (fnName in sketchLineHelperMap) {
|
||||
const { updateArgs } = sketchLineHelperMap[callExpression.callee.name.name]
|
||||
if (!updateArgs) {
|
||||
return new Error('not a sketch line helper')
|
||||
}
|
||||
|
||||
return updateArgs({
|
||||
node: _node,
|
||||
variables,
|
||||
pathToNode: shallowPath,
|
||||
input,
|
||||
})
|
||||
}
|
||||
if (fnName in sketchLineHelperMapKw) {
|
||||
const correctFnName =
|
||||
callExpression.type === 'CallExpressionKw'
|
||||
@ -3170,22 +3140,6 @@ export function tooltipToFnName(tooltip: ToolTip): string | Error {
|
||||
}
|
||||
}
|
||||
|
||||
export function getConstraintInfo(
|
||||
callExpression: Node<CallExpression>,
|
||||
code: string,
|
||||
pathToNode: PathToNode,
|
||||
filterValue?: string
|
||||
): ConstrainInfo[] {
|
||||
const fnName = callExpression?.callee?.name.name || ''
|
||||
if (!(fnName in sketchLineHelperMap)) return []
|
||||
return sketchLineHelperMap[fnName].getConstraintInfo(
|
||||
callExpression,
|
||||
code,
|
||||
pathToNode,
|
||||
filterValue
|
||||
)
|
||||
}
|
||||
|
||||
export function getConstraintInfoKw(
|
||||
callExpression: Node<CallExpressionKw>,
|
||||
code: string,
|
||||
@ -3249,47 +3203,6 @@ interface CreateLineFnCallArgs {
|
||||
snaps?: addCall['snaps']
|
||||
}
|
||||
|
||||
export function addNewSketchLn({
|
||||
node: _node,
|
||||
variables,
|
||||
fnName,
|
||||
pathToNode,
|
||||
input: segmentInput,
|
||||
spliceBetween = false,
|
||||
snaps,
|
||||
}: CreateLineFnCallArgs):
|
||||
| {
|
||||
modifiedAst: Node<Program>
|
||||
pathToNode: PathToNode
|
||||
}
|
||||
| Error {
|
||||
const node = structuredClone(_node)
|
||||
const { add, updateArgs } =
|
||||
sketchLineHelperMap?.[fnName] || sketchLineHelperMapKw?.[fnName] || {}
|
||||
if (!add || !updateArgs) {
|
||||
return new Error(`${fnName} is not a sketch line helper`)
|
||||
}
|
||||
|
||||
getNodeFromPath<Node<VariableDeclarator>>(
|
||||
node,
|
||||
pathToNode,
|
||||
'VariableDeclarator'
|
||||
)
|
||||
getNodeFromPath<Node<PipeExpression | CallExpression | CallExpressionKw>>(
|
||||
node,
|
||||
pathToNode,
|
||||
'PipeExpression'
|
||||
)
|
||||
return add({
|
||||
node,
|
||||
variables,
|
||||
pathToNode,
|
||||
segmentInput,
|
||||
spliceBetween,
|
||||
snaps,
|
||||
})
|
||||
}
|
||||
|
||||
export function addCallExpressionsToPipe({
|
||||
node,
|
||||
pathToNode,
|
||||
@ -3367,10 +3280,7 @@ export function replaceSketchLine({
|
||||
}
|
||||
const _node = { ...node }
|
||||
|
||||
const { add } =
|
||||
sketchLineHelperMap[fnName] === undefined
|
||||
? sketchLineHelperMapKw[fnName]
|
||||
: sketchLineHelperMap[fnName]
|
||||
const { add } = sketchLineHelperMapKw[fnName]
|
||||
const addRetVal = add({
|
||||
node: _node,
|
||||
variables,
|
||||
@ -3561,24 +3471,9 @@ export function addTagForSketchOnFace(
|
||||
const { addTag } = sketchLineHelperMapKw[expressionName]
|
||||
return addTag(tagInfo)
|
||||
}
|
||||
if (expressionName in sketchLineHelperMap) {
|
||||
const { addTag } = sketchLineHelperMap[expressionName]
|
||||
return addTag(tagInfo)
|
||||
}
|
||||
return new Error(`"${expressionName}" is not a sketch line helper`)
|
||||
}
|
||||
|
||||
export function getTagFromCallExpression(
|
||||
callExp: CallExpression
|
||||
): string | Error {
|
||||
if (callExp.callee.name.name === 'close') return getTag(1)(callExp)
|
||||
if (callExp.callee.name.name in sketchLineHelperMap) {
|
||||
const { getTag } = sketchLineHelperMap[callExp.callee.name.name]
|
||||
return getTag(callExp)
|
||||
}
|
||||
return new Error(`"${callExp.callee.name.name}" is not a sketch line helper`)
|
||||
}
|
||||
|
||||
function isAngleLiteral(lineArgument: Expr): boolean {
|
||||
return isLiteralArrayOrStatic(lineArgument)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user