Basically what you are wanting will hardly be supported natively since the very concept of tree is hierarchical by nature.
What is your goal? Is it just to "look" and see what you want more easily? Or is it to process the generated tree via code?
A list is simpler for you to see, but it is much more complicated to process, since you will have to recreate all the contexts of each block.
If you have difficulties with these blocks, you may just need to get a little more used to recursive algorithms, for example.
Transforming the list
If you really need a list flat can be generated by processing the AST.
Suppose you have the following fictitious tree:
var astTree = {
type: "IfClause",
base:
{
type: "Boolean",
value: false
},
body: [
{ type: "Var" },
{
type: "ForClause",
body: [
{ type: "VarIncrement" }
]
},
{ type: "Return" },
]
};
Then you can rely on a recursive function that runs through all subelements that have a body
and add to an array. Example:
var flatten = function(node) {
var list = [];
function consume(node) {
list.push(node);
if ('body' in node) {
var endNode = { type: node.type + 'End' }
node.type += "Start";
node.body.forEach(consume);
list.push(endNode);
delete node.body;
}
}
consume(node);
return list;
}
And the result is:
[
{
"type": "IfClauseStart",
"base": {
"type": "Boolean",
"value": false
}
},
{
"type": "Var"
},
{
"type": "ForClauseStart"
},
{
"type": "VarIncrement"
},
{
"type": "ForClauseEnd"
},
{
"type": "Return"
},
{
"type": "IfClauseEnd"
}
]
Your question sounds very interesting but it’s unclear, I don’t really understand what you want... take a look here: https://astexplorer.net/ is something similar you are looking for?
– Sergio
@Sergio She is different yet... returns block of commands inside an object of a block of commands. I wanted one that did not have this property
body
in objects, a very wide.– Klaider
Okay, you can give an example of JS and how you want it to appear analyzed?
– Sergio