Fixed another queryAst test

There were 2 problems:
 - Test was looking for the old style of `line` call to choose an offset
   for pathToNode
 - Test assumed that the `tag` param was always the third one, but in
   a kwarg call, you have to look it up by label
This commit is contained in:
Adam Chalmers
2025-01-14 11:36:15 -06:00
committed by Nick Cameron
parent ff71250d5d
commit ae0860a775
2 changed files with 15 additions and 13 deletions

View File

@ -443,7 +443,7 @@ describe('Testing findUsesOfTagInPipe', () => {
it('finds the current segment', async () => { it('finds the current segment', async () => {
const ast = assertParse(exampleCode) const ast = assertParse(exampleCode)
const lineOfInterest = `198.85], %, $seg01` const lineOfInterest = `198.85], tag = $seg01`
const characterIndex = const characterIndex =
exampleCode.indexOf(lineOfInterest) + lineOfInterest.length exampleCode.indexOf(lineOfInterest) + lineOfInterest.length
const pathToNode = getNodePathFromSourceRange( const pathToNode = getNodePathFromSourceRange(

View File

@ -5,6 +5,7 @@ import {
ArtifactGraph, ArtifactGraph,
BinaryExpression, BinaryExpression,
CallExpression, CallExpression,
CallExpressionKw,
Expr, Expr,
ExpressionStatement, ExpressionStatement,
ObjectExpression, ObjectExpression,
@ -25,7 +26,7 @@ import {
import { createIdentifier, splitPathAtLastIndex } from './modifyAst' import { createIdentifier, splitPathAtLastIndex } from './modifyAst'
import { getSketchSegmentFromSourceRange } from './std/sketchConstraints' import { getSketchSegmentFromSourceRange } from './std/sketchConstraints'
import { getAngle } from '../lib/utils' import { getAngle } from '../lib/utils'
import { getFirstArg } from './std/sketch' import { ARG_TAG, getFirstArg } from './std/sketch'
import { import {
getConstraintLevelFromSourceRange, getConstraintLevelFromSourceRange,
getConstraintType, getConstraintType,
@ -33,7 +34,8 @@ import {
import { err, Reason } from 'lib/trap' import { err, Reason } from 'lib/trap'
import { ImportStatement } from 'wasm-lib/kcl/bindings/ImportStatement' import { ImportStatement } from 'wasm-lib/kcl/bindings/ImportStatement'
import { Node } from 'wasm-lib/kcl/bindings/Node' import { Node } from 'wasm-lib/kcl/bindings/Node'
import { codeRefFromRange } from './std/artifactGraph' import { ArtifactGraph, codeRefFromRange } from './std/artifactGraph'
import { findKwArg } from './util'
/** /**
* Retrieves a node from a given path within a Program node structure, optionally stopping at a specified node type. * Retrieves a node from a given path within a Program node structure, optionally stopping at a specified node type.
@ -885,27 +887,27 @@ export function findUsesOfTagInPipe(
'segEndY', 'segEndY',
'segLen', 'segLen',
] ]
const nodeMeta = getNodeFromPath<CallExpression>( const nodeMeta = getNodeFromPath<CallExpression | CallExpressionKw>(
ast, ast,
pathToNode, pathToNode,
'CallExpression' ['CallExpression', 'CallExpressionKw']
) )
if (err(nodeMeta)) { if (err(nodeMeta)) {
console.error(nodeMeta) console.error(nodeMeta)
return [] return []
} }
const node = nodeMeta.node const node = nodeMeta.node
if (node.type !== 'CallExpression') return [] if (node.type !== 'CallExpressionKw' && node.type !== 'CallExpression')
return []
const tagIndex = node.callee.name === 'close' ? 1 : 2 const tagIndex = node.callee.name === 'close' ? 1 : 2
const thirdParam = node.arguments[tagIndex] const tagParam =
if ( node.type === 'CallExpression'
!(thirdParam?.type === 'TagDeclarator' || thirdParam?.type === 'Identifier') ? node.arguments[tagIndex]
) : findKwArg(ARG_TAG, node)
if (!(tagParam?.type === 'TagDeclarator' || tagParam?.type === 'Identifier'))
return [] return []
const tag = const tag =
thirdParam?.type === 'TagDeclarator' tagParam?.type === 'TagDeclarator' ? String(tagParam.value) : tagParam.name
? String(thirdParam.value)
: thirdParam.name
const varDec = getNodeFromPath<Node<VariableDeclaration>>( const varDec = getNodeFromPath<Node<VariableDeclaration>>(
ast, ast,