Skip to content

nth(n) ignores current axis id #20

@leumasme

Description

@leumasme

"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

a(1)

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:

  • / for direct child nodes

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions