tag changes followup (#1747)

* tag changes followup

* fmt
This commit is contained in:
Kurt Hutten
2024-03-17 18:24:03 +11:00
committed by GitHub
parent 47ff4623bd
commit cefa6f85fe

View File

@ -149,7 +149,7 @@ export const lineTo: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('default'), addTag: addTag(),
} }
export const line: SketchLineHelper = { export const line: SketchLineHelper = {
@ -240,7 +240,7 @@ export const line: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('default'), addTag: addTag(),
} }
export const xLineTo: SketchLineHelper = { export const xLineTo: SketchLineHelper = {
@ -288,7 +288,7 @@ export const xLineTo: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('default'), addTag: addTag(),
} }
export const yLineTo: SketchLineHelper = { export const yLineTo: SketchLineHelper = {
@ -336,7 +336,7 @@ export const yLineTo: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('default'), addTag: addTag(),
} }
export const xLine: SketchLineHelper = { export const xLine: SketchLineHelper = {
@ -386,7 +386,7 @@ export const xLine: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('length'), addTag: addTag(),
} }
export const yLine: SketchLineHelper = { export const yLine: SketchLineHelper = {
@ -430,7 +430,7 @@ export const yLine: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('length'), addTag: addTag(),
} }
export const tangentialArcTo: SketchLineHelper = { export const tangentialArcTo: SketchLineHelper = {
@ -510,7 +510,7 @@ export const tangentialArcTo: SketchLineHelper = {
} }
}, },
// TODO copy-paste from angledLine // TODO copy-paste from angledLine
addTag: addTagWithTo('angleLength'), addTag: addTag(),
} }
export const angledLine: SketchLineHelper = { export const angledLine: SketchLineHelper = {
add: ({ add: ({
@ -576,7 +576,7 @@ export const angledLine: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('angleLength'), addTag: addTag(),
} }
export const angledLineOfXLength: SketchLineHelper = { export const angledLineOfXLength: SketchLineHelper = {
@ -649,7 +649,7 @@ export const angledLineOfXLength: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('angleLength'), addTag: addTag(),
} }
export const angledLineOfYLength: SketchLineHelper = { export const angledLineOfYLength: SketchLineHelper = {
@ -723,7 +723,7 @@ export const angledLineOfYLength: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('angleLength'), addTag: addTag(),
} }
export const angledLineToX: SketchLineHelper = { export const angledLineToX: SketchLineHelper = {
@ -792,7 +792,7 @@ export const angledLineToX: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('angleTo'), addTag: addTag(),
} }
export const angledLineToY: SketchLineHelper = { export const angledLineToY: SketchLineHelper = {
@ -862,7 +862,7 @@ export const angledLineToY: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('angleTo'), addTag: addTag(),
} }
export const angledLineThatIntersects: SketchLineHelper = { export const angledLineThatIntersects: SketchLineHelper = {
@ -951,7 +951,7 @@ export const angledLineThatIntersects: SketchLineHelper = {
pathToNode, pathToNode,
} }
}, },
addTag: addTagWithTo('angleTo'), // TODO might be wrong addTag: addTag(), // TODO might be wrong
} }
export const updateStartProfileAtArgs: SketchLineHelper['updateArgs'] = ({ export const updateStartProfileAtArgs: SketchLineHelper['updateArgs'] = ({
@ -1174,29 +1174,31 @@ function isAngleLiteral(lineArugement: Value): boolean {
type addTagFn = (a: ModifyAstBase) => { modifiedAst: Program; tag: string } type addTagFn = (a: ModifyAstBase) => { modifiedAst: Program; tag: string }
function addTagWithTo( function addTag(): addTagFn {
argType: 'angleLength' | 'angleTo' | 'default' | 'length'
): addTagFn {
return ({ node, pathToNode }) => { return ({ node, pathToNode }) => {
let tagName = findUniqueName(node, 'seg', 2)
const _node = { ...node } const _node = { ...node }
const { node: callExpression } = getNodeFromPath<CallExpression>( const { node: primaryCallExp } = getNodeFromPath<CallExpression>(
_node, _node,
pathToNode pathToNode,
'CallExpression'
) )
const tagArg = callExpression.arguments?.[2] // Tag is always 3rd expression now, using arg index feels brittle
if (tagArg) { // 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 { return {
modifiedAst: _node, modifiedAst: _node,
tag: String(tagArg), tag: String(tagLiteral.value),
} }
} else { } else {
callExpression.arguments[2] = createLiteral(tagName) throw new Error('Unable to assign tag without value')
return {
modifiedAst: _node,
tag: tagName,
}
} }
} }
} }