Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nth(n) ignores current axis id #20

Open
leumasme opened this issue Nov 14, 2023 · 1 comment
Open

nth(n) ignores current axis id #20

leumasme opened this issue Nov 14, 2023 · 1 comment

Comments

@leumasme
Copy link

leumasme commented Nov 14, 2023

"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.

@leumasme
Copy link
Author

leumasme commented Dec 14, 2023

Considering that

  • none of the inbuilt adapters support named parent axis
    • such a thing would generally not make sense in a tree AST
  • most of the inbuilt adapters do not support getParentNode at all

it may be a good idea to fully move away from this function. Keeping a parent stack to the current position in the Tree at all times would make the function fully obsolete, and remove any need for a parent property on nodes.
Unfortunately it seems like this library is not updated anymore, aside from basic dependency updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant