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 () => {
const ast = assertParse(exampleCode)
const lineOfInterest = `198.85], %, $seg01`
const lineOfInterest = `198.85], tag = $seg01`
const characterIndex =
exampleCode.indexOf(lineOfInterest) + lineOfInterest.length
const pathToNode = getNodePathFromSourceRange(

View File

@ -5,6 +5,7 @@ import {
ArtifactGraph,
BinaryExpression,
CallExpression,
CallExpressionKw,
Expr,
ExpressionStatement,
ObjectExpression,
@ -25,7 +26,7 @@ import {
import { createIdentifier, splitPathAtLastIndex } from './modifyAst'
import { getSketchSegmentFromSourceRange } from './std/sketchConstraints'
import { getAngle } from '../lib/utils'
import { getFirstArg } from './std/sketch'
import { ARG_TAG, getFirstArg } from './std/sketch'
import {
getConstraintLevelFromSourceRange,
getConstraintType,
@ -33,7 +34,8 @@ import {
import { err, Reason } from 'lib/trap'
import { ImportStatement } from 'wasm-lib/kcl/bindings/ImportStatement'
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.
@ -885,27 +887,27 @@ export function findUsesOfTagInPipe(
'segEndY',
'segLen',
]
const nodeMeta = getNodeFromPath<CallExpression>(
const nodeMeta = getNodeFromPath<CallExpression | CallExpressionKw>(
ast,
pathToNode,
'CallExpression'
['CallExpression', 'CallExpressionKw']
)
if (err(nodeMeta)) {
console.error(nodeMeta)
return []
}
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 thirdParam = node.arguments[tagIndex]
if (
!(thirdParam?.type === 'TagDeclarator' || thirdParam?.type === 'Identifier')
)
const tagParam =
node.type === 'CallExpression'
? node.arguments[tagIndex]
: findKwArg(ARG_TAG, node)
if (!(tagParam?.type === 'TagDeclarator' || tagParam?.type === 'Identifier'))
return []
const tag =
thirdParam?.type === 'TagDeclarator'
? String(thirdParam.value)
: thirdParam.name
tagParam?.type === 'TagDeclarator' ? String(tagParam.value) : tagParam.name
const varDec = getNodeFromPath<Node<VariableDeclaration>>(
ast,