Skip to content

Commit

Permalink
Fix up frontend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Jan 29, 2025
1 parent 012da72 commit 069b1cb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 82 deletions.
4 changes: 3 additions & 1 deletion src/lang/queryAstNodePathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './wasm'
import { ImportStatement } from 'wasm-lib/kcl/bindings/ImportStatement'
import { Node } from 'wasm-lib/kcl/bindings/Node'
import { ARG_INDEX_FIELD, LABELED_ARG_FIELD } from './queryAst'

function moreNodePathFromSourceRange(
node: Node<
Expand Down Expand Up @@ -76,7 +77,8 @@ function moreNodePathFromSourceRange(
const arg = args[argIndex].arg
if (arg.start <= start && arg.end >= end) {
path.push(['arguments', 'CallExpressionKw'])
path.push([argIndex, 'index'])
path.push([argIndex, ARG_INDEX_FIELD])
path.push(['arg', LABELED_ARG_FIELD])
return moreNodePathFromSourceRange(arg, sourceRange, path)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/lang/std/sketch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,6 @@ describe('testing getConstraintInfo', () => {
offset = 0 + 0
}, %)
|> tangentialArcTo([3.14 + 0, 13.14 + 0], %)`
const ast = assertParse(code)
test.each([
[
'line',
Expand Down
163 changes: 83 additions & 80 deletions src/lang/std/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ const constrainInfo = (
g === 'singleValue'
? { type: 'singleValue' }
: typeof g === 'number'
? { type: 'arrayItem', index: g }
: typeof g === 'string'
? { type: 'objectProperty', key: g }
: undefined,
? { type: 'arrayItem', index: g }
: typeof g === 'string'
? { type: 'objectProperty', key: g }
: undefined,
pathToNode: e,
stdLibFnName: f,
})
Expand Down Expand Up @@ -226,39 +226,39 @@ const commonConstraintInfoHelper = (
const pathToFirstArg: PathToNode = isArr
? [...pathToArrayExpression, [0, 'index']]
: [
...pathToArrayExpression,
[
firstArg.properties.findIndex(
(a) => a.key.name === abbreviatedInputs[0].objInput
),
'index',
],
['value', 'Property'],
]
...pathToArrayExpression,
[
firstArg.properties.findIndex(
(a) => a.key.name === abbreviatedInputs[0].objInput
),
'index',
],
['value', 'Property'],
]

const pathToSecondArg: PathToNode = isArr
? [...pathToArrayExpression, [1, 'index']]
: [
...pathToArrayExpression,
[
firstArg.properties.findIndex(
(a) => a.key.name === abbreviatedInputs[1].objInput
),
'index',
],
['value', 'Property'],
]
...pathToArrayExpression,
[
firstArg.properties.findIndex(
(a) => a.key.name === abbreviatedInputs[1].objInput
),
'index',
],
['value', 'Property'],
]

const input1 = isArr
? firstArg.elements[0]
: firstArg.properties.find(
(a) => a.key.name === abbreviatedInputs[0].objInput
)?.value
(a) => a.key.name === abbreviatedInputs[0].objInput
)?.value
const input2 = isArr
? firstArg.elements[1]
: firstArg.properties.find(
(a) => a.key.name === abbreviatedInputs[1].objInput
)?.value
(a) => a.key.name === abbreviatedInputs[1].objInput
)?.value

const constraints: ConstrainInfo[] = []
if (input1)
Expand Down Expand Up @@ -1807,7 +1807,7 @@ export const angledLineThatIntersects: SketchLineHelper = {
const intersectTag =
firstArg.type === 'ObjectExpression'
? firstArg.properties.find((p) => p.key.name === 'intersectTag')
?.value || createLiteral('')
?.value || createLiteral('')
: createLiteral('')
const intersectTagName =
intersectTag.type === 'Identifier' ? intersectTag.name : ''
Expand Down Expand Up @@ -1998,13 +1998,13 @@ export function changeSketchArguments(
programMemory: ProgramMemory,
sourceRangeOrPath:
| {
type: 'sourceRange'
sourceRange: SourceRange
}
type: 'sourceRange'
sourceRange: SourceRange
}
| {
type: 'path'
pathToNode: PathToNode
},
type: 'path'
pathToNode: PathToNode
},
input: SegmentInputs
): { modifiedAst: Node<Program>; pathToNode: PathToNode } | Error {
const _node = { ...node }
Expand All @@ -2020,7 +2020,8 @@ export function changeSketchArguments(

const { node: callExpression, shallowPath } = nodeMeta

if (callExpression?.callee?.name in sketchLineHelperMap) {
const fnName = callExpression?.callee?.name
if (fnName in sketchLineHelperMap) {
const { updateArgs } = sketchLineHelperMap[callExpression.callee.name]
if (!updateArgs) {
return new Error('not a sketch line helper')
Expand All @@ -2033,8 +2034,10 @@ export function changeSketchArguments(
input,
})
}
if (callExpression?.callee?.name in sketchLineHelperMapKw) {
const { updateArgs } = sketchLineHelperMapKw[callExpression.callee.name]
if (fnName in sketchLineHelperMapKw) {
const isAbsolute = callExpression.type === 'CallExpressionKw' && findKwArg('endAbsolute', callExpression) !== undefined
const correctFnName = fnName === 'line' && isAbsolute ? 'lineTo' : fnName
const { updateArgs } = sketchLineHelperMapKw[correctFnName]
if (!updateArgs) {
return new Error('not a sketch line keyword helper')
}
Expand All @@ -2047,7 +2050,7 @@ export function changeSketchArguments(
})
}

return new Error(`not a sketch line helper: ${callExpression?.callee?.name}`)
return new Error(`not a sketch line helper: ${fnName}`)
}

export function getConstraintInfo(
Expand Down Expand Up @@ -2122,9 +2125,9 @@ export function addNewSketchLn({
spliceBetween = false,
}: CreateLineFnCallArgs):
| {
modifiedAst: Node<Program>
pathToNode: PathToNode
}
modifiedAst: Node<Program>
pathToNode: PathToNode
}
| Error {
const node = structuredClone(_node)
const { add, updateArgs } =
Expand Down Expand Up @@ -2219,10 +2222,10 @@ export function replaceSketchLine({
referencedSegment?: Path
}):
| {
modifiedAst: Node<Program>
valueUsedInTransform?: number
pathToNode: PathToNode
}
modifiedAst: Node<Program>
valueUsedInTransform?: number
pathToNode: PathToNode
}
| Error {
if (![...toolTips, 'intersect', 'circle'].includes(fnName)) {
return new Error(`The following function name is not tooltip: ${fnName}`)
Expand Down Expand Up @@ -2274,9 +2277,9 @@ function addTagToChamfer(
edgeCutMeta: EdgeCutInfo
):
| {
modifiedAst: Node<Program>
tag: string
}
modifiedAst: Node<Program>
tag: string
}
| Error {
const _node = structuredClone(tagInfo.node)
let pipeIndex = 0
Expand Down Expand Up @@ -2401,9 +2404,9 @@ export function addTagForSketchOnFace(
edgeCutMeta: EdgeCutInfo | null
):
| {
modifiedAst: Node<Program>
tag: string
}
modifiedAst: Node<Program>
tag: string
}
| Error {
if (expressionName === 'close') {
return addTagKw()(tagInfo)
Expand Down Expand Up @@ -2442,10 +2445,10 @@ function isAngleLiteral(lineArugement: Expr): boolean {
return lineArugement?.type === 'ArrayExpression'
? isLiteralArrayOrStatic(lineArugement.elements[0])
: lineArugement?.type === 'ObjectExpression'
? isLiteralArrayOrStatic(
? isLiteralArrayOrStatic(
lineArugement.properties.find(({ key }) => key.name === 'angle')?.value
)
: false
: false
}

type addTagFn = (
Expand Down Expand Up @@ -2506,13 +2509,13 @@ function addTagKw(): addTagFn {
callExpr.node.type === 'CallExpressionKw'
? callExpr.node
: {
type: 'CallExpressionKw',
callee: callExpr.node.callee,
unlabeled: callExpr.node.arguments.length
? callExpr.node.arguments[0]
: null,
arguments: [],
}
type: 'CallExpressionKw',
callee: callExpr.node.callee,
unlabeled: callExpr.node.arguments.length
? callExpr.node.arguments[0]
: null,
arguments: [],
}
const tagArg = findKwArg(ARG_TAG, primaryCallExp)
const tagDeclarator =
tagArg || createTagDeclarator(findUniqueName(_node, 'seg', 2))
Expand Down Expand Up @@ -2566,9 +2569,9 @@ export function getXComponent(

function getFirstArgValuesForXYFns(callExpression: CallExpression):
| {
val: [Expr, Expr]
tag?: Expr
}
val: [Expr, Expr]
tag?: Expr
}
| Error {
// used for lineTo, line
const firstArg = callExpression.arguments[0]
Expand All @@ -2577,9 +2580,9 @@ function getFirstArgValuesForXYFns(callExpression: CallExpression):

function getValuesForXYFns(arg: Expr):
| {
val: [Expr, Expr]
tag?: Expr
}
val: [Expr, Expr]
tag?: Expr
}
| Error {
if (arg.type === 'ArrayExpression') {
return { val: [arg.elements[0], arg.elements[1]] }
Expand All @@ -2597,9 +2600,9 @@ function getValuesForXYFns(arg: Expr):

function getFirstArgValuesForAngleFns(callExpression: CallExpression):
| {
val: [Expr, Expr]
tag?: Expr
}
val: [Expr, Expr]
tag?: Expr
}
| Error {
// used for angledLine, angledLineOfXLength, angledLineToX, angledLineOfYLength, angledLineToY
const firstArg = callExpression.arguments[0]
Expand Down Expand Up @@ -2656,9 +2659,9 @@ const getCircle = (
callExp: CallExpression
):
| {
val: [Expr, Expr, Expr]
tag?: Expr
}
val: [Expr, Expr, Expr]
tag?: Expr
}
| Error => {
const firstArg = callExp.arguments[0]
if (firstArg.type === 'ObjectExpression') {
Expand All @@ -2682,9 +2685,9 @@ const getAngledLineThatIntersects = (
callExp: CallExpression
):
| {
val: [Expr, Expr, Expr]
tag?: Expr
}
val: [Expr, Expr, Expr]
tag?: Expr
}
| Error => {
const firstArg = callExp.arguments[0]
if (firstArg.type === 'ObjectExpression') {
Expand Down Expand Up @@ -2728,9 +2731,9 @@ Get the argument corresponding to 'end' or 'endAbsolute' or wherever the line ac
*/
export function getArgForEnd(lineCall: CallExpressionKw):
| {
val: Expr | [Expr, Expr] | [Expr, Expr, Expr]
tag?: Expr
}
val: Expr | [Expr, Expr] | [Expr, Expr, Expr]
tag?: Expr
}
| Error {
const name = lineCall?.callee?.name
let arg
Expand All @@ -2747,9 +2750,9 @@ export function getArgForEnd(lineCall: CallExpressionKw):

export function getFirstArg(callExp: CallExpression):
| {
val: Expr | [Expr, Expr] | [Expr, Expr, Expr]
tag?: Expr
}
val: Expr | [Expr, Expr] | [Expr, Expr, Expr]
tag?: Expr
}
| Error {
const name = callExp?.callee?.name
if (
Expand Down

0 comments on commit 069b1cb

Please sign in to comment.