Stop throwing in frontend code (#2654)

Return error instead of throw
This commit is contained in:
49fl
2024-06-24 11:45:40 -04:00
committed by GitHub
parent f7196e7eb0
commit f4877cb160
67 changed files with 5127 additions and 4523 deletions

View File

@ -18,6 +18,7 @@ import {
} from './modifyAst'
import { enginelessExecutor } from '../lib/testHelpers'
import { findUsesOfTagInPipe, getNodePathFromSourceRange } from './queryAst'
import { err } from 'lib/trap'
beforeAll(async () => {
await initPromise
@ -140,10 +141,14 @@ function giveSketchFnCallTagTestHelper(
// this wrapper changes the input and output to code
// making it more of an integration test, but easier to read the test intention is the goal
const ast = parse(code)
if (err(ast)) throw ast
const start = code.indexOf(searchStr)
const range: [number, number] = [start, start + searchStr.length]
const { modifiedAst, tag, isTagExisting } = giveSketchFnCallTag(ast, range)
const sketchRes = giveSketchFnCallTag(ast, range)
if (err(sketchRes)) throw sketchRes
const { modifiedAst, tag, isTagExisting } = sketchRes
const newCode = recast(modifiedAst)
if (err(newCode)) throw newCode
return { tag, newCode, isTagExisting }
}
@ -211,6 +216,7 @@ const part001 = startSketchOn('XY')
const yo2 = hmm([identifierGuy + 5])`
it('should move a binary expression into a new variable', async () => {
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const startIndex = code.indexOf('100 + 100') + 1
const { modifiedAst } = moveValueIntoNewVariable(
@ -225,6 +231,7 @@ const yo2 = hmm([identifierGuy + 5])`
})
it('should move a value into a new variable', async () => {
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const startIndex = code.indexOf('2.8') + 1
const { modifiedAst } = moveValueIntoNewVariable(
@ -239,6 +246,7 @@ const yo2 = hmm([identifierGuy + 5])`
})
it('should move a callExpression into a new variable', async () => {
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const startIndex = code.indexOf('def(')
const { modifiedAst } = moveValueIntoNewVariable(
@ -253,6 +261,7 @@ const yo2 = hmm([identifierGuy + 5])`
})
it('should move a binary expression with call expression into a new variable', async () => {
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const startIndex = code.indexOf('jkl(') + 1
const { modifiedAst } = moveValueIntoNewVariable(
@ -267,6 +276,7 @@ const yo2 = hmm([identifierGuy + 5])`
})
it('should move a identifier into a new variable', async () => {
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const startIndex = code.indexOf('identifierGuy +') + 1
const { modifiedAst } = moveValueIntoNewVariable(
@ -290,6 +300,8 @@ describe('testing sketchOnExtrudedFace', () => {
|> close(%)
|> extrude(5 + 7, %)`
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const segmentSnippet = `line([9.7, 9.19], %)`
const segmentRange: [number, number] = [
@ -304,12 +316,15 @@ describe('testing sketchOnExtrudedFace', () => {
]
const extrudePathToNode = getNodePathFromSourceRange(ast, extrudeRange)
const { modifiedAst } = sketchOnExtrudedFace(
const extruded = sketchOnExtrudedFace(
ast,
segmentPathToNode,
extrudePathToNode,
programMemory
)
if (err(extruded)) throw extruded
const { modifiedAst } = extruded
const newCode = recast(modifiedAst)
expect(newCode).toContain(`const part001 = startSketchOn('-XZ')
|> startProfileAt([3.58, 2.06], %)
@ -327,6 +342,7 @@ const sketch001 = startSketchOn(part001, 'seg01')`)
|> close(%)
|> extrude(5 + 7, %)`
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const segmentSnippet = `close(%)`
const segmentRange: [number, number] = [
@ -341,12 +357,15 @@ const sketch001 = startSketchOn(part001, 'seg01')`)
]
const extrudePathToNode = getNodePathFromSourceRange(ast, extrudeRange)
const { modifiedAst } = sketchOnExtrudedFace(
const extruded = sketchOnExtrudedFace(
ast,
segmentPathToNode,
extrudePathToNode,
programMemory
)
if (err(extruded)) throw extruded
const { modifiedAst } = extruded
const newCode = recast(modifiedAst)
expect(newCode).toContain(`const part001 = startSketchOn('-XZ')
|> startProfileAt([3.58, 2.06], %)
@ -364,6 +383,7 @@ const sketch001 = startSketchOn(part001, 'seg01')`)
|> close(%)
|> extrude(5 + 7, %)`
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const sketchSnippet = `startProfileAt([3.58, 2.06], %)`
const sketchRange: [number, number] = [
@ -378,13 +398,16 @@ const sketch001 = startSketchOn(part001, 'seg01')`)
]
const extrudePathToNode = getNodePathFromSourceRange(ast, extrudeRange)
const { modifiedAst } = sketchOnExtrudedFace(
const extruded = sketchOnExtrudedFace(
ast,
sketchPathToNode,
extrudePathToNode,
programMemory,
'end'
)
if (err(extruded)) throw extruded
const { modifiedAst } = extruded
const newCode = recast(modifiedAst)
expect(newCode).toContain(`const part001 = startSketchOn('-XZ')
|> startProfileAt([3.58, 2.06], %)
@ -410,6 +433,7 @@ const sketch001 = startSketchOn(part001, 'END')`)
|> close(%)
const part001 = extrude(5 + 7, sketch001)`
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const segmentSnippet = `line([4.99, -0.46], %)`
const segmentRange: [number, number] = [
@ -424,13 +448,14 @@ const sketch001 = startSketchOn(part001, 'END')`)
]
const extrudePathToNode = getNodePathFromSourceRange(ast, extrudeRange)
const { modifiedAst } = sketchOnExtrudedFace(
const updatedAst = sketchOnExtrudedFace(
ast,
segmentPathToNode,
extrudePathToNode,
programMemory
)
const newCode = recast(modifiedAst)
if (err(updatedAst)) throw updatedAst
const newCode = recast(updatedAst.modifiedAst)
expect(newCode).toContain(`const part001 = extrude(5 + 7, sketch001)
const sketch002 = startSketchOn(part001, 'seg01')`)
})
@ -444,6 +469,7 @@ describe('Testing deleteSegmentFromPipeExpression', () => {
|> line([306.21, 198.85], %, 'a')
|> line([306.21, 198.87], %)`
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const lineOfInterest = "line([306.21, 198.85], %, 'a')"
const range: [number, number] = [
@ -458,6 +484,7 @@ describe('Testing deleteSegmentFromPipeExpression', () => {
code,
pathToNode
)
if (err(modifiedAst)) throw modifiedAst
const newCode = recast(modifiedAst)
expect(newCode).toBe(`const part001 = startSketchOn('-XZ')
|> startProfileAt([54.78, -95.91], %)
@ -520,6 +547,7 @@ ${!replace1 ? ` |> ${line}\n` : ''} |> angledLine([-65, ${
])(`%s`, async (_, line, [replace1, replace2]) => {
const code = makeCode(line)
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const lineOfInterest = line
const range: [number, number] = [
@ -535,6 +563,7 @@ ${!replace1 ? ` |> ${line}\n` : ''} |> angledLine([-65, ${
code,
pathToNode
)
if (err(modifiedAst)) throw modifiedAst
const newCode = recast(modifiedAst)
expect(newCode).toBe(makeCode(line, replace1, replace2))
})
@ -606,6 +635,8 @@ describe('Testing removeSingleConstraintInfo', () => {
['tangentialArcTo([3.14 + 0, 13.14], %)', 'arrayIndex', 1],
])('stdlib fn: %s', async (expectedFinish, key, value) => {
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const lineOfInterest = expectedFinish.split('(')[0] + '('
const range: [number, number] = [
@ -621,7 +652,7 @@ describe('Testing removeSingleConstraintInfo', () => {
ast,
programMemory
)
if (!mod) throw new Error('yo is undefined')
if (!mod) return new Error('mod is undefined')
const recastCode = recast(mod.modifiedAst)
expect(recastCode).toContain(expectedFinish)
})
@ -642,6 +673,8 @@ describe('Testing removeSingleConstraintInfo', () => {
['angledLineToY([30, 10.14 + 0], %)', 'arrayIndex', 0],
])('stdlib fn: %s', async (expectedFinish, key, value) => {
const ast = parse(code)
if (err(ast)) throw ast
const programMemory = await enginelessExecutor(ast)
const lineOfInterest = expectedFinish.split('(')[0] + '('
const range: [number, number] = [
@ -657,7 +690,7 @@ describe('Testing removeSingleConstraintInfo', () => {
ast,
programMemory
)
if (!mod) throw new Error('yo is undefined')
if (!mod) return new Error('mod is undefined')
const recastCode = recast(mod.modifiedAst)
expect(recastCode).toContain(expectedFinish)
})