Skip to content

Commit

Permalink
Update codemods now that xLine/To is kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Feb 25, 2025
1 parent 6207270 commit 3698be4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 32 deletions.
100 changes: 70 additions & 30 deletions src/lang/std/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
VariableMap,
} from 'lang/wasm'
import {
ARG_INDEX_FIELD,
getNodeFromPath,
getNodeFromPathCurry,
getObjExprProperty,
Expand Down Expand Up @@ -76,6 +77,7 @@ import {

export const ARG_TAG = 'tag'
export const ARG_END = 'end'
export const ARG_LENGTH = 'length'
export const ARG_END_ABSOLUTE = 'endAbsolute'

const STRAIGHT_SEGMENT_ERR = new Error(
Expand Down Expand Up @@ -207,7 +209,9 @@ const commonConstraintInfoHelper = (
case 'CallExpression':
return 0
case 'CallExpressionKw':
return findKwArgAnyIndex([ARG_END, ARG_END_ABSOLUTE], callExp)
const arg = findKwArgAnyIndex([ARG_END, ARG_END_ABSOLUTE], callExp)
if (arg === undefined) return undefined
return arg.argIndex
}
})()
if (argIndex === undefined) {
Expand Down Expand Up @@ -339,6 +343,53 @@ const horzVertConstraintInfoHelper = (
]
}

const horzVertConstraintInfoHelperKw = (
callExp: CallExpressionKw,
inputConstrainTypes: [ConstrainInfo['type'], ConstrainInfo['type']],
stdLibFnName: ConstrainInfo['stdLibFnName'],
abbreviatedInput: AbbreviatedInput,
code: string,
pathToNode: PathToNode,
filterValue?: string
) => {
if (callExp.type !== 'CallExpressionKw') return []
const firstArg = callExp.arguments?.[0]
const callee = callExp.callee
const relevantArg = findKwArgAnyIndex(
[ARG_END_ABSOLUTE, ARG_END, ARG_LENGTH],
callExp
)
if (relevantArg === undefined) return []
const { argIndex, expr: relevantArgExpr } = relevantArg
const pathToRelevantArg: PathToNode = [
...pathToNode,
['arguments', 'CallExpressionKw'],
[argIndex, ARG_INDEX_FIELD],
['arg', LABELED_ARG_FIELD],
]
const pathToCallee: PathToNode = [...pathToNode, ['callee', 'CallExpression']]
return [
constrainInfo(
inputConstrainTypes[0],
true,
callee.name,
stdLibFnName,
undefined,
topLevelRange(callee.start, callee.end),
pathToCallee
),
constrainInfo(
inputConstrainTypes[1],
isNotLiteralArrayOrStatic(relevantArgExpr),
code.slice(relevantArgExpr.start, relevantArgExpr.end),
stdLibFnName,
abbreviatedInput,
topLevelRange(relevantArgExpr.start, relevantArgExpr.end),
pathToRelevantArg
),
]
}

function getTag(index = 2): SketchLineHelper['getTag'] {
return (callExp: CallExpression) => {
if (callExp.type !== 'CallExpression')
Expand Down Expand Up @@ -401,7 +452,6 @@ export const line: SketchLineHelperKw = {
const callExp = createCallExpressionStdLibKw(
'line',
null, // Assumes this is being called in a pipeline, so the first arg is optional and if not given, will become pipeline substitution.
// TODO: ADAM: This should have a tag sometimes.
[createLabeledArg(ARG_END, createArrayExpression([newXVal, newYVal]))]
)
const pathToNodeIndex = pathToNode.findIndex(
Expand Down Expand Up @@ -651,7 +701,7 @@ export const lineTo: SketchLineHelperKw = {
),
}

export const xLineTo: SketchLineHelper = {
export const xLineTo: SketchLineHelperKw = {
add: ({ node, pathToNode, segmentInput, replaceExistingCallback }) => {
if (segmentInput.type !== 'straight-segment') return STRAIGHT_SEGMENT_ERR
const { to } = segmentInput
Expand Down Expand Up @@ -681,9 +731,8 @@ export const xLineTo: SketchLineHelper = {
valueUsedInTransform,
}
}
const callExp = createCallExpression('xLineTo', [
newVal,
createPipeSubstitution(),
const callExp = createCallExpressionStdLibKw('xLine', newVal, [
createLabeledArg(ARG_END_ABSOLUTE, newVal),
])
pipe.body = [...pipe.body, callExp]
return {
Expand All @@ -695,24 +744,20 @@ export const xLineTo: SketchLineHelper = {
if (input.type !== 'straight-segment') return STRAIGHT_SEGMENT_ERR
const { to } = input
const _node = { ...node }
const nodeMeta = getNodeFromPath<CallExpression>(_node, pathToNode)
const nodeMeta = getNodeFromPath<CallExpressionKw>(_node, pathToNode)
if (err(nodeMeta)) return nodeMeta
const { node: callExpression } = nodeMeta
const newX = createLiteral(roundOff(to[0], 2))
if (isLiteralArrayOrStatic(callExpression.arguments?.[0])) {
callExpression.arguments[0] = newX
} else {
mutateObjExpProp(callExpression.arguments?.[0], newX, 'to')
}
mutateKwArg(ARG_END_ABSOLUTE, callExpression, newX)
return {
modifiedAst: _node,
pathToNode,
}
},
getTag: getTag(),
addTag: addTag(),
getTag: getTagKwArg(),
addTag: addTagKw(),
getConstraintInfo: (callExp, ...args) =>
horzVertConstraintInfoHelper(
horzVertConstraintInfoHelperKw(
callExp,
['horizontal', 'xAbsolute'],
'xLineTo',
Expand Down Expand Up @@ -791,7 +836,7 @@ export const yLineTo: SketchLineHelper = {
),
}

export const xLine: SketchLineHelper = {
export const xLine: SketchLineHelperKw = {
add: ({ node, pathToNode, segmentInput, replaceExistingCallback }) => {
if (segmentInput.type !== 'straight-segment') return STRAIGHT_SEGMENT_ERR
const { from, to } = segmentInput
Expand Down Expand Up @@ -826,9 +871,8 @@ export const xLine: SketchLineHelper = {
}
}

const newLine = createCallExpression('xLine', [
newVal,
createPipeSubstitution(),
const newLine = createCallExpressionStdLibKw('xLine', null, [
createLabeledArg(ARG_LENGTH, newVal),
])
if (dec.init.type === 'PipeExpression') {
dec.init.body = [...dec.init.body, newLine]
Expand All @@ -841,24 +885,20 @@ export const xLine: SketchLineHelper = {
if (input.type !== 'straight-segment') return STRAIGHT_SEGMENT_ERR
const { to, from } = input
const _node = { ...node }
const nodeMeta = getNodeFromPath<CallExpression>(_node, pathToNode)
const nodeMeta = getNodeFromPath<CallExpressionKw>(_node, pathToNode)
if (err(nodeMeta)) return nodeMeta
const { node: callExpression } = nodeMeta
const newX = createLiteral(roundOff(to[0] - from[0], 2))
if (isLiteralArrayOrStatic(callExpression.arguments?.[0])) {
callExpression.arguments[0] = newX
} else {
mutateObjExpProp(callExpression.arguments?.[0], newX, 'length')
}
mutateKwArg(ARG_LENGTH, callExpression, newX)
return {
modifiedAst: _node,
pathToNode,
}
},
getTag: getTag(),
addTag: addTag(),
getTag: getTagKwArg(),
addTag: addTagKw(),
getConstraintInfo: (callExp, ...args) =>
horzVertConstraintInfoHelper(
horzVertConstraintInfoHelperKw(
callExp,
['horizontal', 'xRelative'],
'xLine',
Expand Down Expand Up @@ -2284,9 +2324,7 @@ export const updateStartProfileAtArgs: SketchLineHelper['updateArgs'] = ({
}

export const sketchLineHelperMap: { [key: string]: SketchLineHelper } = {
xLine,
yLine,
xLineTo,
yLineTo,
angledLine,
angledLineOfXLength,
Expand All @@ -2302,6 +2340,8 @@ export const sketchLineHelperMapKw: { [key: string]: SketchLineHelperKw } = {
line,
lineTo,
circleThreePoint,
xLine,
xLineTo,
} as const

export function changeSketchArguments(
Expand Down
8 changes: 6 additions & 2 deletions src/lang/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,14 @@ Search the keyword arguments from a call for an argument with one of these label
export function findKwArgAnyIndex(
labels: string[],
call: CallExpressionKw
): number | undefined {
return call.arguments.findIndex((arg) => {
): { expr: Expr; argIndex: number } | undefined {
const argIndex = call.arguments.findIndex((arg) => {
return labels.includes(arg.label.name)
})
if (argIndex < 0) {
return undefined
}
return { expr: call.arguments[argIndex].arg, argIndex }
}

export function isAbsolute(call: CallExpressionKw): boolean {
Expand Down

0 comments on commit 3698be4

Please sign in to comment.