diff --git a/src/lang/modifyAst/addEdgeTreatment.test.ts b/src/lang/modifyAst/addEdgeTreatment.test.ts index 87267d1a6..6ec7f1657 100644 --- a/src/lang/modifyAst/addEdgeTreatment.test.ts +++ b/src/lang/modifyAst/addEdgeTreatment.test.ts @@ -10,6 +10,7 @@ import { VariableDeclarator, SourceRange, topLevelRange, + CallExpressionKw, } from '../wasm' import { EdgeTreatmentType, @@ -78,14 +79,14 @@ const runGetPathToExtrudeForSegmentSelectionTest = async ( ast: Program, code: string, expectedExtrudeSnippet: string - ): CallExpression | PipeExpression | Error { + ): CallExpression | CallExpressionKw | PipeExpression | Error { const extrudeRange = topLevelRange( code.indexOf(expectedExtrudeSnippet), code.indexOf(expectedExtrudeSnippet) + expectedExtrudeSnippet.length ) const expectedExtrudePath = getNodePathFromSourceRange(ast, extrudeRange) const expectedExtrudeNodeResult = getNodeFromPath< - VariableDeclarator | CallExpression + VariableDeclarator | CallExpression | CallExpressionKw >(ast, expectedExtrudePath) if (err(expectedExtrudeNodeResult)) { return expectedExtrudeNodeResult @@ -93,7 +94,9 @@ const runGetPathToExtrudeForSegmentSelectionTest = async ( const expectedExtrudeNode = expectedExtrudeNodeResult.node // check whether extrude is in the sketch pipe - const extrudeInSketchPipe = expectedExtrudeNode.type === 'CallExpression' + const extrudeInSketchPipe = + expectedExtrudeNode.type === 'CallExpression' || + expectedExtrudeNode.type === 'CallExpressionKw' if (extrudeInSketchPipe) { return expectedExtrudeNode } @@ -506,7 +509,7 @@ extrude001 = extrude(sketch001, length = -15) |> line(endAbsolute = [profileStartX(%), profileStartY(%)]) |> close() extrude001 = extrude(sketch001, length = -15)` - const segmentSnippets = ['line(end = [20, 0], %)', 'line([-20, 0])'] + const segmentSnippets = ['line(end = [20, 0])', 'line(end = [-20, 0])'] const expectedCode = `sketch001 = startSketchOn('XY') |> startProfileAt([-10, 10], %) |> line(end = [20, 0], tag = $seg01) @@ -632,9 +635,11 @@ extrude001 = extrude(sketch001, length = -5) it('should correctly identify no edges', () => { const ast = assertParse(code) const lineOfInterest = `line(end = [-3.29, -13.85])` - const range = topLevelRange( - code.indexOf(lineOfInterest), - code.indexOf(lineOfInterest) + lineOfInterest.length + const start = code.indexOf(lineOfInterest) + expect(start).toBeGreaterThan(-1) + const range = topLevelRange( + start, + start + lineOfInterest.length ) const pathToNode = getNodePathFromSourceRange(ast, range) if (err(pathToNode)) return @@ -657,10 +662,12 @@ describe('Testing button states', () => { ) => { const ast = assertParse(code) + const start = code.indexOf(segmentSnippet) + expect(start).toBeGreaterThan(-1) const range = segmentSnippet ? topLevelRange( - code.indexOf(segmentSnippet), - code.indexOf(segmentSnippet) + segmentSnippet.length + start, + start + segmentSnippet.length ) : topLevelRange(ast.end, ast.end) // empty line in the end of the code diff --git a/src/lang/modifyAst/addEdgeTreatment.ts b/src/lang/modifyAst/addEdgeTreatment.ts index 53ca8bc01..1bedc18e4 100644 --- a/src/lang/modifyAst/addEdgeTreatment.ts +++ b/src/lang/modifyAst/addEdgeTreatment.ts @@ -1,6 +1,7 @@ import { ArtifactGraph, CallExpression, + CallExpressionKw, Expr, Identifier, ObjectExpression, @@ -526,7 +527,7 @@ export const hasValidEdgeTreatmentSelection = ({ traverse(ast, { enter(node) { if ( - node.type === 'CallExpression' && + (node.type === 'CallExpression' || node.type == 'CallExpressionKw') && (node.callee.name === 'extrude' || node.callee.name === 'revolve') ) { extrudeExists = true @@ -548,13 +549,16 @@ export const hasValidEdgeTreatmentSelection = ({ // selection exists: for (const selection of selectionRanges.graphSelections) { // check if all selections are in sketchLineHelperMap - const segmentNode = getNodeFromPath>( - ast, - selection.codeRef.pathToNode, - 'CallExpression' - ) + const segmentNode = getNodeFromPath< + Node + >(ast, selection.codeRef.pathToNode, ['CallExpression', 'CallExpressionKw']) if (err(segmentNode)) return false - if (segmentNode.node.type !== 'CallExpression') { + if ( + !( + segmentNode.node.type === 'CallExpression' || + segmentNode.node.type === 'CallExpressionKw' + ) + ) { return false } if (!(segmentNode.node.callee.name in sketchLineHelperMap)) { @@ -597,7 +601,8 @@ export const hasValidEdgeTreatmentSelection = ({ traverse(ast, { enter(node) { if ( - node.type === 'CallExpression' && + (node.type === 'CallExpression' || + node.type === 'CallExpressionKw') && isEdgeTreatmentType(node.callee.name) ) { inEdgeTreatment = true @@ -610,7 +615,8 @@ export const hasValidEdgeTreatmentSelection = ({ }, leave(node) { if ( - node.type === 'CallExpression' && + (node.type === 'CallExpression' || + node.type === 'CallExpressionKw') && isEdgeTreatmentType(node.callee.name) ) { inEdgeTreatment = false @@ -650,7 +656,7 @@ export const isTagUsedInEdgeTreatment = ({ enter: (node) => { // Check if we are entering an edge treatment call if ( - node.type === 'CallExpression' && + (node.type === 'CallExpression' || node.type === 'CallExpressionKw') && isEdgeTreatmentType(node.callee.name) ) { inEdgeTreatment = true @@ -668,7 +674,7 @@ export const isTagUsedInEdgeTreatment = ({ if ( inObj && inEdgeTreatment && - node.type === 'CallExpression' && + (node.type === 'CallExpression' || node.type === 'CallExpressionKw') && isEdgeType(node.callee.name) ) { inTagHelper = node.callee.name @@ -694,7 +700,7 @@ export const isTagUsedInEdgeTreatment = ({ }, leave: (node) => { if ( - node.type === 'CallExpression' && + (node.type === 'CallExpression' || node.type === 'CallExpressionKw') && isEdgeTreatmentType(node.callee.name) ) { inEdgeTreatment = false @@ -712,7 +718,7 @@ export const isTagUsedInEdgeTreatment = ({ if ( inObj && inEdgeTreatment && - node.type === 'CallExpression' && + (node.type === 'CallExpression' || node.type === 'CallExpressionKw') && isEdgeType(node.callee.name) ) { inTagHelper = '' diff --git a/src/lang/queryAst.ts b/src/lang/queryAst.ts index e3d1d15da..b856be7b3 100644 --- a/src/lang/queryAst.ts +++ b/src/lang/queryAst.ts @@ -429,6 +429,7 @@ export function getNodePathFromSourceRange( previousPath: PathToNode = [['body', '']] ): PathToNode { const [start, end] = sourceRange || [] + expect(start).toBeGreaterThan(-1) let path: PathToNode = [...previousPath] const _node = { ...node }