Refactor source ranges into a generic node type (#4350)

* WIP

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Fix formatting

* Fix yarn build:wasm

* Fix ts_rs bindings

* Fix tsc errors

* Fix wasm TS types

* Add minimal failing test

* Rename field to avoid name collisions

* Remove node wrapper around NonCodeMeta

Trying to fix TS unit test errors deserializing JSON AST in Rust.

* Rename Node to BoxNode

* Fix lints

* Fix lint by boxing literals

* Rename UnboxedNode to Node

* Look at this (photo)Graph *in the voice of Nickelback*

* Update docs

* Update snapshots

* initial trait

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* update docs

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* gross hack for TagNode

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* extend gross hack

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fix EnvRef bullshit

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* Fix to fail parsing when a tag declarator matches a stdlib function name

* Fix test errors after merging main

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest)

* Confirm

* Change to use simpler map_err

* Add comment

---------

Signed-off-by: Nick Cameron <nrc@ncameron.org>
Signed-off-by: Jess Frazelle <github@jessfraz.com>
Co-authored-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jonathan Tran
2024-10-30 16:52:17 -04:00
committed by GitHub
parent 0c6c646fe7
commit 43bec115c0
148 changed files with 12935 additions and 13924 deletions

View File

@ -55,6 +55,7 @@ import { err } from 'lib/trap'
import { perpendicularDistance } from 'sketch-helpers'
import { TagDeclarator } from 'wasm-lib/kcl/bindings/TagDeclarator'
import { EdgeCutInfo } from 'machines/modelingMachine'
import { Node } from 'wasm-lib/kcl/bindings/Node'
const STRAIGHT_SEGMENT_ERR = new Error(
'Invalid input, expected "straight-segment"'
@ -1785,7 +1786,7 @@ export const angledLineThatIntersects: SketchLineHelper = {
)
}
if (intersectTag !== -1) {
const tag = firstArg.properties[intersectTag]?.value as Identifier
const tag = firstArg.properties[intersectTag]?.value as Node<Identifier>
const pathToTagProp: PathToNode = [
...pathToObjectExp,
[intersectTag, 'index'],
@ -1825,7 +1826,9 @@ export const updateStartProfileAtArgs: SketchLineHelper['updateArgs'] = ({
body: [],
nonCodeMeta: {
start: [],
start: 0,
end: 0,
startNodes: [],
nonCodeNodes: [],
},
},
@ -1865,7 +1868,7 @@ export const sketchLineHelperMap: { [key: string]: SketchLineHelper } = {
} as const
export function changeSketchArguments(
node: Program,
node: Node<Program>,
programMemory: ProgramMemory,
sourceRangeOrPath:
| {
@ -1877,7 +1880,7 @@ export function changeSketchArguments(
pathToNode: PathToNode
},
input: SegmentInputs
): { modifiedAst: Program; pathToNode: PathToNode } | Error {
): { modifiedAst: Node<Program>; pathToNode: PathToNode } | Error {
const _node = { ...node }
const thePath =
sourceRangeOrPath.type === 'sourceRange'
@ -1906,7 +1909,7 @@ export function changeSketchArguments(
}
export function getConstraintInfo(
callExpression: CallExpression,
callExpression: Node<CallExpression>,
code: string,
pathToNode: PathToNode
): ConstrainInfo[] {
@ -1944,7 +1947,7 @@ export function compareVec2Epsilon2(
}
interface CreateLineFnCallArgs {
node: Program
node: Node<Program>
programMemory: ProgramMemory
input: SegmentInputs
fnName: ToolTip
@ -1961,7 +1964,7 @@ export function addNewSketchLn({
spliceBetween = false,
}: CreateLineFnCallArgs):
| {
modifiedAst: Program
modifiedAst: Node<Program>
pathToNode: PathToNode
}
| Error {
@ -1971,8 +1974,12 @@ export function addNewSketchLn({
return new Error('not a sketch line helper')
}
getNodeFromPath<VariableDeclarator>(node, pathToNode, 'VariableDeclarator')
getNodeFromPath<PipeExpression | CallExpression>(
getNodeFromPath<Node<VariableDeclarator>>(
node,
pathToNode,
'VariableDeclarator'
)
getNodeFromPath<Node<PipeExpression | CallExpression>>(
node,
pathToNode,
'PipeExpression'
@ -1991,13 +1998,13 @@ export function addCallExpressionsToPipe({
pathToNode,
expressions,
}: {
node: Program
node: Node<Program>
programMemory: ProgramMemory
pathToNode: PathToNode
expressions: CallExpression[]
expressions: Node<CallExpression>[]
}) {
const _node = { ...node }
const pipeExpression = getNodeFromPath<PipeExpression>(
const pipeExpression = getNodeFromPath<Node<PipeExpression>>(
_node,
pathToNode,
'PipeExpression'
@ -2046,7 +2053,7 @@ export function replaceSketchLine({
replaceExistingCallback,
referencedSegment,
}: {
node: Program
node: Node<Program>
programMemory: ProgramMemory
pathToNode: PathToNode
fnName: ToolTip
@ -2055,7 +2062,7 @@ export function replaceSketchLine({
referencedSegment?: Path
}):
| {
modifiedAst: Program
modifiedAst: Node<Program>
valueUsedInTransform?: number
pathToNode: PathToNode
}
@ -2107,7 +2114,7 @@ function addTagToChamfer(
edgeCutMeta: EdgeCutInfo | null
):
| {
modifiedAst: Program
modifiedAst: Node<Program>
tag: string
}
| Error {
@ -2234,7 +2241,7 @@ export function addTagForSketchOnFace(
edgeCutMeta: EdgeCutInfo | null
):
| {
modifiedAst: Program
modifiedAst: Node<Program>
tag: string
}
| Error {
@ -2272,12 +2279,14 @@ function isAngleLiteral(lineArugement: Expr): boolean {
: false
}
type addTagFn = (a: AddTagInfo) => { modifiedAst: Program; tag: string } | Error
type addTagFn = (
a: AddTagInfo
) => { modifiedAst: Node<Program>; tag: string } | Error
function addTag(tagIndex = 2): addTagFn {
return ({ node, pathToNode }) => {
const _node = { ...node }
const callExpr = getNodeFromPath<CallExpression>(
const callExpr = getNodeFromPath<Node<CallExpression>>(
_node,
pathToNode,
'CallExpression'