{"id":4263,"date":"2016-05-31T01:19:31","date_gmt":"2016-05-31T01:19:31","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4263"},"modified":"2016-05-31T01:19:31","modified_gmt":"2016-05-31T01:19:31","slug":"estraverse-accessing-grandparent-nodes","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/estraverse-accessing-grandparent-nodes\/","title":{"rendered":"Estraverse: Accessing grandparent nodes"},"content":{"rendered":"<p>The &#8220;estraverse&#8221; library allows you to parse a JavaScript AST (e.g. generated from ESPrima). <\/p>\n<p>When you use it, you pull in a Javascript file, parse it, and then run a set of callbacks over the tree.<\/p>\n<p>The two callbacks you can define are &#8220;enter&#8221; and &#8220;leave&#8221; which are called when each node is processed, and when it goes back up the stack past the node.<\/p>\n<p>As a convenience, they give you the parent node in &#8220;enter&#8221; but typically this isn&#8217;t enough &#8211; it&#8217;s more helpful to have the entire parent chain.<\/p>\n<p>To get this, you can simply make a Javascript array that you tree as a stack, then modify it as the traversal progresses:<\/p>\n<pre lang=\"javascript\">\nconst fs = require(\"fs\");\nconst esprima = require(\"esprima\");\nconst estraverse = require(\"estraverse\");\nconst filename = ...\n\nlet parentChain = [];\n\nconst ast = esprima.parse(\n  fs.readFileSync(filename)\n);\n\nestraverse.traverse(ast, {\n  enter: (node, parent) => {\n    parentChain.push(node);\n  },\n  leave: (node) => {\n    parentChain.pop();\n  }\n});\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to get the parent chain of Javascript nodes when parsing Javascript using ESPrima + Estraverse<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4263"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4263"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4263\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}