Skip to content

Commit

Permalink
tag changes followup (#1747)
Browse files Browse the repository at this point in the history
* tag changes followup

* fmt
  • Loading branch information
Irev-Dev authored Mar 17, 2024
1 parent 47ff462 commit cefa6f8
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/lang/std/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const lineTo: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('default'),
addTag: addTag(),
}

export const line: SketchLineHelper = {
Expand Down Expand Up @@ -240,7 +240,7 @@ export const line: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('default'),
addTag: addTag(),
}

export const xLineTo: SketchLineHelper = {
Expand Down Expand Up @@ -288,7 +288,7 @@ export const xLineTo: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('default'),
addTag: addTag(),
}

export const yLineTo: SketchLineHelper = {
Expand Down Expand Up @@ -336,7 +336,7 @@ export const yLineTo: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('default'),
addTag: addTag(),
}

export const xLine: SketchLineHelper = {
Expand Down Expand Up @@ -386,7 +386,7 @@ export const xLine: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('length'),
addTag: addTag(),
}

export const yLine: SketchLineHelper = {
Expand Down Expand Up @@ -430,7 +430,7 @@ export const yLine: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('length'),
addTag: addTag(),
}

export const tangentialArcTo: SketchLineHelper = {
Expand Down Expand Up @@ -510,7 +510,7 @@ export const tangentialArcTo: SketchLineHelper = {
}
},
// TODO copy-paste from angledLine
addTag: addTagWithTo('angleLength'),
addTag: addTag(),
}
export const angledLine: SketchLineHelper = {
add: ({
Expand Down Expand Up @@ -576,7 +576,7 @@ export const angledLine: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('angleLength'),
addTag: addTag(),
}

export const angledLineOfXLength: SketchLineHelper = {
Expand Down Expand Up @@ -649,7 +649,7 @@ export const angledLineOfXLength: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('angleLength'),
addTag: addTag(),
}

export const angledLineOfYLength: SketchLineHelper = {
Expand Down Expand Up @@ -723,7 +723,7 @@ export const angledLineOfYLength: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('angleLength'),
addTag: addTag(),
}

export const angledLineToX: SketchLineHelper = {
Expand Down Expand Up @@ -792,7 +792,7 @@ export const angledLineToX: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('angleTo'),
addTag: addTag(),
}

export const angledLineToY: SketchLineHelper = {
Expand Down Expand Up @@ -862,7 +862,7 @@ export const angledLineToY: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('angleTo'),
addTag: addTag(),
}

export const angledLineThatIntersects: SketchLineHelper = {
Expand Down Expand Up @@ -951,7 +951,7 @@ export const angledLineThatIntersects: SketchLineHelper = {
pathToNode,
}
},
addTag: addTagWithTo('angleTo'), // TODO might be wrong
addTag: addTag(), // TODO might be wrong
}

export const updateStartProfileAtArgs: SketchLineHelper['updateArgs'] = ({
Expand Down Expand Up @@ -1174,29 +1174,31 @@ function isAngleLiteral(lineArugement: Value): boolean {

type addTagFn = (a: ModifyAstBase) => { modifiedAst: Program; tag: string }

function addTagWithTo(
argType: 'angleLength' | 'angleTo' | 'default' | 'length'
): addTagFn {
function addTag(): addTagFn {
return ({ node, pathToNode }) => {
let tagName = findUniqueName(node, 'seg', 2)
const _node = { ...node }
const { node: callExpression } = getNodeFromPath<CallExpression>(
const { node: primaryCallExp } = getNodeFromPath<CallExpression>(
_node,
pathToNode
pathToNode,
'CallExpression'
)
const tagArg = callExpression.arguments?.[2]
if (tagArg) {
// Tag is always 3rd expression now, using arg index feels brittle
// but we can come up with a better way to identify tag later.
const thirdArg = primaryCallExp.arguments?.[2]
const tagLiteral =
thirdArg || (createLiteral(findUniqueName(_node, 'seg', 2)) as Literal)
const isTagExisting = !!thirdArg
if (!isTagExisting) {
primaryCallExp.arguments[2] = tagLiteral
}
if ('value' in tagLiteral) {
// Now TypeScript knows tagLiteral has a value property
return {
modifiedAst: _node,
tag: String(tagArg),
tag: String(tagLiteral.value),
}
} else {
callExpression.arguments[2] = createLiteral(tagName)

return {
modifiedAst: _node,
tag: tagName,
}
throw new Error('Unable to assign tag without value')
}
}
}
Expand Down

0 comments on commit cefa6f8

Please sign in to comment.