|
"nth": (A, T, num) => { |
|
num = parseInt(num, 10) |
|
let parent = A.getParentNode(T, "*") |
|
if (parent !== null) { |
|
let pchilds = A.getChildNodes(parent, "*") |
|
if (num < 0) |
|
num = pchilds.length - (num + 1) |
|
for (let i = 0; i < pchilds.length; i++) |
|
if (pchilds[i] === T) |
|
return ((i + 1) === num) |
|
return false |
|
} |
It seems like nth is implemented by getting the parent, then getting the children on * axis, and finding the element in question.
This produces arguably unexpected cases where nth is called from within a filter on an axis with an id, e.g. I would expect
/ ExpressionStatement / CallExpression /:arguments * [
nth(1)
]
running on the input ast of
to find the 1st argument of the CallExpression (1-based indexing, ouch).
Instead, this query returns nothing, because the 1st child of the CallExpression on axis * is actually the function that was called, located on .callee.
What now? Always just +1 the parameter for nth in this case? Not pretty, and doesn't work on ast nodes with multiple variable-length child arrays.
While testing this, I've also found that
/ ExpressionStatement / CallExpression / *
finds 'Identifier', 'Literal', 'ExpressionStatement':
What is happening here? Why is ExpressionStatement, which is the parent of the CallExpression, being matched by this Query?
The readme defines the / Axis as:
Edit: Turns out this last part is likely caused by astq considering the parent property (which I populated on my mozast to get nth to work) as a valid child axis.
astq/src/astq-funcs-std.js
Lines 75 to 86 in ea8d5c8
It seems like
nthis implemented by getting the parent, then getting the children on*axis, and finding the element in question.This produces arguably unexpected cases where
nthis called from within a filter on an axis with an id, e.g. I would expectrunning on the input ast of
to find the 1st argument of the CallExpression (1-based indexing, ouch).
Instead, this query returns nothing, because the 1st child of the CallExpression on axis
*is actually the function that was called, located on.callee.What now? Always just
+1the parameter fornthin this case? Not pretty, and doesn't work on ast nodes with multiple variable-length child arrays.While testing this, I've also found that
finds
'Identifier', 'Literal', 'ExpressionStatement':What is happening here? Why is ExpressionStatement, which is the parent of the CallExpression, being matched by this Query?
The readme defines the
/Axis as:/for direct child nodesEdit: Turns out this last part is likely caused by astq considering the
parentproperty (which I populated on my mozast to getnthto work) as a valid child axis.