`esprima` AST Tree: How to easily detect and add function parens? - esprima

TL;DR: i want to do same thing as there https://github.com/nolanlawson/optimize-js but with esprima when i traverse through AST tree with estraverse.
ESPrima gives same output nodes for following code:
!function (){}()
and
!(function (){})()
http://esprima.org/demo/parse.html?code=!function%20()%7B%7D()%0A%0A!(function%20()%7B%7D)()
For example - i will traverse through AST tree. On Function Expression ExpressionStatement node i want to check - if that node doesn't have parens around function - i want to add it.
So, how i can detect function parens, how i can add them? I look at tokens, but i have no idea how i can associate flat tokens object with object with specified AST node.

Seems it task not for esprima but for escodegen
https://github.com/estools/escodegen/issues/315

Related

What is the difference between the 'Avl' suffix and the original function in the Win32 API?

I've dumped the export table of ntdll.dll looking for specific APIs, but found the same function but one 'Avl' appended to it, what does it mean?
like:
RtlDeleteElementGenericTable
RtlDeleteElementGenericTableAvl
That is just the simple meaning, For example: RtlDeleteElementGenericTable and RtlDeleteElementGenericTableAvl, The parameter type of RtlDeleteElementGenericTable is RTL_GENERIC_TABLE, But RtlDeleteElementGenericTableAvl is RTL_AVL_TABLE.
The RTL_AVL_TABLE structure contains file system-specific data for an
Adelson-Velsky/Landis (AVL) tree. An AVL tree ensures a more balanced,
shallower tree implementation than a splay tree implementation of a
generic table (RTL_GENERIC_TABLE).
So that's is also the differences between * and *Avl functions.

Trie with custom insert and delete functions

I need to create four custom function for a Trie data structure without changing its O(n) complexity:
makeDir("pathToDir")
make("pathToFile")
delete("pathToDir")
forceDelete("pathToDirOrFile")
makeDir("pathToDir"): adds path to the trie only if it is a valid path
make("pathToFile"): adds path to the trie only if it is a valid path (a file node can't call makeDir() )
delete("pathToDir"): deletes the path from trie only if it has no child dirs
forceDelete("pathToDirOrFile"): deletes path and its child dirs
For example a list of commands would be:
makeDir("\dir");
makeDir("\dir\foo")
makeDir("\dir\foo\lol\ok"); /* incorrect path */
make("\dir\file");
makeDir("\dir\file\abc"); /* file can't have sub dirs */
delete("\dir"); /* has childs, incorrect */
delete("\dir\file");
forceDelete("\dir");
Does anybody have any idea on how to recognize that the node indicates the path of a files? What is the best way to implement these functions?
Validating and splitting the path
It's OS specific, so just pick any library that works with paths for your target system.
The trie
Once you can split a path into pieces, you can build a trie. Keep strings in its edges. For instance, if you have a foo/bar path, there'll be 3 nodes and two edges: the first one (1->2) is marked with foo and the second one (2->3) is marked with bar.
You can store a flag in each node to indicate if it's a regular file or a directory.
To check if a directory is empty, just make sure it's node has no children.
To check if a directory/file can be created, take its base dir (all parts of the path except the last one), check that it exists by traversing your trie from the root and that its node is a directory, not a regular file.
Efficient traversal
You can store edges in hash table that maps a string to a node.

Execute query lazily in Orient-DB

In current project we need to find cheapest paths in almost fully connected graph which can contain lots of edges per vertex pair.
We developed a plugin containing functions
for special traversal this graph to lower reoccurences of similar paths while TRAVERSE execution. We will refer it as search()
for special effective extraction of desired information from results of such traverses. We will refer it as extract()
for extracting best N records according to target parameter without costly ORDER BY. We will refer it as best()
But resulted query still has unsatisfactory performance on full data.
So we decided to modify search() function so it could watch best edges first and prune paths leading to definitely undesired result by using current state of best() function.
Overall solution is effectively a flexible implementation of Branch and Bound method
Resulting query (omitting extract() step) should look like
SELECT best(path, <limit>) FROM (
TRAVERSE search(<params>) FROM #<starting_point>
WHILE <conditions on intermediate vertixes>
) WHERE <conditions on result elements>
This form is very desired so we could adapt conditions under WHILE and WHERE for our current task. The path field is generated by search() containing all information for best() to proceed.
The trouble is that best() function is executed strictly after search() function, so search() can not prune non-optimal branches according to results already evaluated by best().
So the Question is:
Is there a way to pipeline results from TRAVERSE step to SELECT step in the way that older paths were TRAVERSEd with search() after earlier paths handled by SELECT with best()?
the query execution in this case will be streamed. If you add a
System.out.println()
or you put a breakpoint in your functions you'll see that the invocation sequence will be
search
best
search
best
search
...
You can use a ThreadLocal object http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html
to store some context data and share it between the two functions, or you can use the OCommandContext (the last parameter in OSQLFunction.execute() method to store context information.
You can use context.getVariable() and context.setVariable() for this.
The contexts of the two queries (the parent and the inner query) are different, but they should be linked by a parent/child relationship, so you should be able to retrieve them using OCommandContext.getParent()

immediately preceding-sibling must contain attribute

Here is my XML file:
<w type="fruit-hard">apple</w>
<w type="fruit-soft">orange</w>
<w type="vegetable">carrot</w>
I need to find carrot's immediately preceding sibling whose type is fruit-soft. In Chrome (locally loaded XML file), when I try
$x("//w[#type='vegetable']/preceding-sibling::w[1]")
I get "orange" element node like I want, but how do I require that its type be "fruit-soft"? My attempt (below) returns "false."
$x("//w[#type='vegetable']/preceding-sibling::w[1] and preceding-sibling::w[#type='fruit-soft']")
Your original XPath ...
//w[#type='vegetable']/preceding-sibling::w[1]
... is equivalent to
//w[#type='vegetable']/preceding-sibling::w[position()=1]
. You can add additional criteria to the predicate as needed:
//w[#type='vegetable']/preceding-sibling::w[position()=1 and #type='fruit-soft']
Or you can add an add a separate predicate
//w[#type='vegetable']/preceding-sibling::w[1][#type='fruit-soft']
Note that this attempt:
//w[#type='vegetable']/preceding-sibling::w[1] and preceding-sibling::w[#type='fruit-soft']
returns false because the parts on either side of the and are evaluated separately, converted to type boolean, and combined to yield the final result. Supposing that the context node against which that is evaluated is the document root, there will never be a node matching preceding-sibling::w[#type='fruit-soft']. Moreover, even if there were such a node, that expression does not require nodes matching the first part to be the same ones that matches the second part.

Can't get nth node in Selenium

I try to write xpath expressions so that my tests won't be broken by small design changes. So instead of the expressions that Selenium IDE generates, I write my own.
Here's an issue:
//input[#name='question'][7]
This expression doesn't work at all. Input nodes named 'question' are spread across the page. They're not siblings.
I've tried using intermediate expression, but it also fails.
(//input[#name='question'])[2]
error = Error: Element (//input[#name='question'])[2] not found
That's why I suppose Seleniun has a wrong implementation of XPath.
According to XPath docs, the position predicate must filter by the position in the nodeset, so it must find the seventh input with the name 'question'. In Selenium this doesn't work. CSS selectors (:nth-of-kind) neither.
I had to write an expression that filters their common parents:
//*[contains(#class, 'question_section')][7]//input[#name='question']
Is this a Selenium specific issue, or I'm reading the specs wrong way? What can I do to make a shorter expression?
Here's an issue:
//input[#name='question'][7]
This expression doesn't work at all.
This is a FAQ.
[] has a higher priority than //.
The above expression selects every input element with #name = 'question', which is the 7th child of its parent -- and aparently the parents of input elements in the document that is not shown don't have so many input children.
Use (note the brackets):
(//input[#name='question'])[7]
This selects the 7th element input in the document that satisfies the conditions in the predicate.
Edit:
People, who know Selenium (Dave Hunt) suggest that the above expression is written in Selenium as:
xpath=(//input[#name='question'])[7]
If you want the 7th input with name attribute with a value of question in the source then try the following:
/descendant::input[#name='question'][7]

Resources