{"_id":"astronaut","_rev":"26-76553d8d7afab2f94ac2fe122c048fce","name":"astronaut","description":"A library for transforming Esprima/SpiderMonkey ASTs.","dist-tags":{"latest":"0.1.4"},"versions":{"0.0.0":{"name":"astronaut","version":"0.0.0","description":"A library for transforming Esprima/SpiderMonkey ASTs.","main":"src/index.js","repository":{"type":"git","url":"https://github.com/giokincade/astronaut"},"directories":{"test":"test"},"scripts":{"test":"./bin/run_tests"},"keywords":["ast","abstract syntax tree","astronaut","esprima","spidermonkey"],"author":{"name":"Giovanni Fernandez-Kincade"},"license":"BSD-2-Clause","dependencies":{"nodeunit":"~0.8.7","esprima":"~1.2.0","escodegen":"~1.3.2","underscore":"~1.6.0"},"bugs":{"url":"https://github.com/giokincade/astronaut/issues"},"_id":"astronaut@0.0.0","dist":{"shasum":"ca1606bb464c2ea3fdc7d9b66958fb8bc111b4e8","tarball":"https://registry.npmjs.org/astronaut/-/astronaut-0.0.0.tgz","integrity":"sha512-KM5nwdJzfhFegWPwvwm3j8fWzp4ds8/8M+3t2kFSlw9HSWlqeyYzEVRa9hNbQFSseu5Xyo2uNqaeZ5oX00YC1w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHzxREBsiMX+OZU0ko1OjCMSYG/OjSf5xMlRl+/fmAMmAiAeisLiX9LrnKOwhpHuKdsc1tEUrv0yIKLSLqMkwy9QUA=="}]},"_from":".","_npmVersion":"1.3.8","_npmUser":{"name":"giokincade","email":"giokincade@gmail.com"},"maintainers":[{"name":"giokincade","email":"giokincade@gmail.com"}]},"0.1.0":{"name":"astronaut","version":"0.1.0","description":"A library for transforming Esprima/SpiderMonkey ASTs.","main":"src/index.js","repository":{"type":"git","url":"https://github.com/giokincade/astronaut"},"directories":{"test":"test"},"scripts":{"test":"./bin/run_tests"},"keywords":["ast","abstract syntax tree","astronaut","esprima","spidermonkey"],"author":{"name":"Giovanni Fernandez-Kincade"},"license":"BSD-2-Clause","dependencies":{"nodeunit":"~0.8.7","esprima":"~1.2.0","escodegen":"~1.3.2","underscore":"~1.6.0"},"bugs":{"url":"https://github.com/giokincade/astronaut/issues"},"_id":"astronaut@0.1.0","dist":{"shasum":"823aeae73beac9bc23f5e57abb5d148524d80882","tarball":"https://registry.npmjs.org/astronaut/-/astronaut-0.1.0.tgz","integrity":"sha512-cPP2+eggptR2aupf3H0hlvmA+5Z+S9aeqsdnl/XGw8OmvDOdrRjTDhEzgtsOyV1HRNygN+NbT8RMeczqpcRnWA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCHsORnYv8yt5BARiHi5Y9DpeFLvfKRXM5UM7EH4J/yQQIgCwh7Nxc3EX9eZBNFvP3mjzCj6rKNuEg5VNZ9jVF0CTE="}]},"_from":".","_npmVersion":"1.3.8","_npmUser":{"name":"giokincade","email":"giokincade@gmail.com"},"maintainers":[{"name":"giokincade","email":"giokincade@gmail.com"}]},"0.1.2":{"name":"astronaut","version":"0.1.2","description":"A library for transforming Esprima/SpiderMonkey ASTs.","main":"src/index.js","repository":{"type":"git","url":"https://github.com/giokincade/astronaut"},"directories":{"test":"test"},"scripts":{"test":"./bin/run_tests"},"keywords":["ast","abstract syntax tree","astronaut","esprima","spidermonkey"],"author":{"name":"Giovanni Fernandez-Kincade"},"license":"BSD-2-Clause","dependencies":{"nodeunit":"~0.8.7","esprima":"~1.2.0","escodegen":"~1.3.2","underscore":"~1.6.0"},"bugs":{"url":"https://github.com/giokincade/astronaut/issues"},"_id":"astronaut@0.1.2","dist":{"shasum":"749e657f2ac81dff1ddb5339ea54375366361e61","tarball":"https://registry.npmjs.org/astronaut/-/astronaut-0.1.2.tgz","integrity":"sha512-EZNCkWQGxfFQNzODNbDjY8O9sfB87/ztJb8dpl3rBrSTbzjfY25v4PLAzex6LyYwniTNsJzRuHsUxg7RdnUI3A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHI8L8uKBC+ME3WudaB7LfD74PdmkjSyPTGxwy9b30gBAiEA6dCHJ59qbmQ8hLoon4LkKJyWsvENibE5EJE2eQvrT4E="}]},"_from":".","_npmVersion":"1.3.8","_npmUser":{"name":"giokincade","email":"giokincade@gmail.com"},"maintainers":[{"name":"giokincade","email":"giokincade@gmail.com"}]},"0.1.3":{"name":"astronaut","version":"0.1.3","description":"A library for transforming Esprima/SpiderMonkey ASTs.","main":"src/index.js","repository":{"type":"git","url":"https://github.com/giokincade/astronaut"},"directories":{"test":"test"},"scripts":{"test":"./bin/run_tests"},"keywords":["ast","abstract syntax tree","astronaut","esprima","spidermonkey"],"author":{"name":"Giovanni Fernandez-Kincade"},"license":"BSD-2-Clause","dependencies":{"nodeunit":"~0.8.7","esprima":"~1.2.0","escodegen":"~1.3.2","underscore":"~1.6.0"},"bugs":{"url":"https://github.com/giokincade/astronaut/issues"},"_id":"astronaut@0.1.3","dist":{"shasum":"7a6352d9d3d89b6c5afc5e862dd86e65b2d686a8","tarball":"https://registry.npmjs.org/astronaut/-/astronaut-0.1.3.tgz","integrity":"sha512-cqF0q7kl2CWcNQwGadqkit91exd1dyLgxUgIBBvJgfKSVzzcsI5DRGvOIrfm1PMibGXAO2YtlOb8AKnMu2i+SA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQClrGRPxAOYf55vCo13QzY2/ZFADQ1+F95Nmqtf5MmEQwIhALAjZsFdDGMK4uH8bm9Zxu7uOm8U1ul/MPTn7h1J4tv0"}]},"_from":".","_npmVersion":"1.3.8","_npmUser":{"name":"giokincade","email":"giokincade@gmail.com"},"maintainers":[{"name":"giokincade","email":"giokincade@gmail.com"}]},"0.1.4":{"name":"astronaut","version":"0.1.4","description":"A library for transforming Esprima/SpiderMonkey ASTs.","main":"src/index.js","repository":{"type":"git","url":"https://github.com/giokincade/astronaut"},"directories":{"test":"test"},"scripts":{"test":"./bin/run_tests"},"keywords":["ast","abstract syntax tree","astronaut","esprima","spidermonkey"],"author":{"name":"Giovanni Fernandez-Kincade"},"license":"BSD-2-Clause","dependencies":{"nodeunit":"~0.8.7","esprima":"~1.2.0","escodegen":"~1.3.2","underscore":"~1.6.0"},"bugs":{"url":"https://github.com/giokincade/astronaut/issues"},"_id":"astronaut@0.1.4","dist":{"shasum":"18efcae97193605c890dce150a579b3877fe00e0","tarball":"https://registry.npmjs.org/astronaut/-/astronaut-0.1.4.tgz","integrity":"sha512-4GwciCl3Tt3s3g/lxXmFN5AMREY4EnVwVyOY4YQa3x0VncweR5fucaHJZH24jaDNKFPIgHJ9C3Z+coAzJmvzKg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDkhAeGFMk3tb3vY+sMrBVyPMe2Eogcv8IKt5DYuhGlCQIhAOUkFc1jKdHwGIJTmWSQ5yjXYMl7KRe8qVj/SEe8FijC"}]},"_from":".","_npmVersion":"1.3.8","_npmUser":{"name":"giokincade","email":"giokincade@gmail.com"},"maintainers":[{"name":"giokincade","email":"giokincade@gmail.com"}]}},"readme":"# Astronaut \nAstronaut is a library for traversing and manipulating the javascript AST.\n[![Build Status](https://travis-ci.org/giokincade/astronaut.svg?branch=master)](https://travis-ci.org/giokincade/astronaut)\n# Examples\nLet's replace all instances of 5 with 4:\n```\nastronaut('1 + 5')\n    .walk(function(node) { \n        if (node.isLiteral() \n                && node.value() === 5) { \n            node.value(4);\n        }\n    })\n    .deparse()\n//'1 + 4'\n```\nLet's replace all instances of 5 with a call to the function \"f\":\n```\nastronaut('1 + 5')\n    .walk(function(node) { \n        if (node.isLiteral() \n                && node.value() === 5) { \n            node.replace('f()');\n        }\n    })\n    .deparse()\n//'1 + f()'\n```\nOr how about we wrap them in a call to the function f:\n```\nastronaut('1 + 5')\n    .walk(function(node) { \n        if (node.isLiteral() \n                && node.value() === 5) { \n            node.wrap('f(<%= node %>)');\n        }\n    })\n    .deparse()\n//'1 + f(5)'\n```\nLet's prefix all calls to f where the second argument is an array expression with a call to g and the same array:\n```\nvar code = \"f(); f(1, [1,2,3]);\";\nastronaut(code).walk(function(node) {\n    if (node.isCallExpression() \n            && node.calleeName() === \"f\"\n            && node.arguments().length > 1\n            && node.arguments()[1].isArrayExpression()) {\n        node.prefix(\"g(\" + node.arguments()[1].deparse() + ')');\n    }\n}).deparse()\n//\"f();g([1,2,3]);f(1,[1,2,3]);\",\n```\nLet's wrap a function body in a try/catch:\n```\nvar code = \"function f(a) { return a; }\";\nastronaut(code).walk(function(node) {\n    if (node.isBlockStatement() && node.parent.isFunctionDeclaration()) {\n        node.wrap(\"try { <%= node %> } catch(e) {} }\");\n    }\n}).deparse()\n//\"function f(a) { try { return a; } catch(e) {} }\";\n```\n\n# Api\n## astronaut\n```\nvar astronaut = require('astronaut')\n```\nAstronaut takes a string to be parsed (using Esprima), or an already-parsed Esprima/SpiderMonkey AST, and returns\nan AstNode.\n\n## AstNode\n### AstNode.data \nThe SpiderMonkey AST data for this node, with sub-nodes wrapped in AstNodes. Updates to this mutate the tree, \nbut for non-primitive values it's recommended to use the replace/wrap functions.\n### AstNode.parent\nA pointer to the parent of this node \n### AstNode.parentKey\nThe key in the parent that points to this node.\n### AstNode.parentArrayIndex\nIf parent[key] is an array, the index of this node in that array. This enables affix functions. \n### AstNode.is &lt;NodeType&gt;()\nReturns a boolean indicating whether or note the node is of the specified type. \n### AstNode.ast()\nReturns the SpiderMonkey AST for this node.\n### AstNode.walk(callback)\nWalk the AST starting at this node, calling the callback along the way.\n### AstNode.map(callback)\nWalk the tree, replacing nodes with new nodes produced by callback(node). \nTo avoid thrashing the tree, if the callback returns null/undefined, no replacement occurs. \n### AstNode.reduce(accumulator, callback)\nReduce the tree down to a single value. \n`callback` should be a function that expects the accumulator as the first argument, the current node as the second, and returns the new accumulator.\n### AstNode.deparse(options)\nA shortcut for escodegen.generate(AstNode.ast(), options) \n### AstNode.wrap(codeOrTemplate)\nWrap the current node in the code specified by an underscore template. The template can be a string to be parsed or a compiled template. The current node should be represented as `node` in the template. For\nexample:\nnode.wrap(\"f(<%= node =>)\")\n\n### AstNode.replace(code)\nReplace the current node with the results of parsing the input `code` \n### AstNode.prefix(code)\nInsert the statement specified in code prior to the statement encoded by the current node. \n### AstNode.suffix(code)\nInsert the statement specified in code after the statement encoded by the current node. \n\n# Installation \n```\nnpm install astronaut\n```\n# Acknowledgements \nI was inspired by [burrito](https://github.com/substack/node-burrito), but was dissapointed by the lack of child pointers and wanted something that supported the Esprima AST. \n[Falafel](https://github.com/substack/node-falafel) seems like it may have solved both problems.\n","maintainers":[{"name":"giokincade","email":"giokincade@gmail.com"}],"time":{"modified":"2022-06-13T03:35:49.663Z","created":"2014-05-03T14:21:25.674Z","0.0.0":"2014-05-03T14:22:42.329Z","0.1.0":"2014-05-04T15:40:34.424Z","0.1.2":"2014-05-05T03:21:23.279Z","0.1.3":"2014-05-06T20:28:21.072Z","0.1.4":"2014-05-09T16:09:46.518Z"},"readmeFilename":"README.md","keywords":["ast","abstract syntax tree","astronaut","esprima","spidermonkey"],"repository":{"type":"git","url":"https://github.com/giokincade/astronaut"},"author":{"name":"Giovanni Fernandez-Kincade"},"bugs":{"url":"https://github.com/giokincade/astronaut/issues"},"license":"BSD-2-Clause"}