{"_id":"sl-esprima-ast-utils","_rev":"2-e6f5af0e08affe69ca1a50e73b282b28","name":"sl-esprima-ast-utils","description":"collection for AST query/manipulation/transformation","dist-tags":{"latest":"0.0.7"},"versions":{"0.0.7":{"name":"sl-esprima-ast-utils","version":"0.0.7","description":"collection for AST query/manipulation/transformation","main":"index.js","directories":{"lib":".","test":"test"},"scripts":{"test":"tap test/test-*.js","docr":"docr ./lib/io.js ./lib/walk.js ./lib/debug.js ./lib/query.js ./lib/manipulations.js ./lib/transformations.js ./lib/tokens.js ./lib/arrays.js --prepend intro.markdown --append LICENSE > Readme.md"},"repository":{"type":"git","url":"git+https://github.com/llafuente/esprima-ast-utils.git"},"keywords":["arguments","falafel","browserify","jsdoc"],"author":{"name":"Luis Lafuente","email":"llafuente@noboxout.com","url":"http://www.noboxout.com"},"license":"MIT","bugs":{"url":"https://github.com/llafuente/esprima-ast-utils/issues"},"homepage":"https://github.com/llafuente/esprima-ast-utils","dependencies":{"esprima":"^3.1.3","cssauron-falafel":"^1.2.1","archy":"^1.0.0"},"devDependencies":{"tap":"^0.4.11","object-enhancements":"^0.1.1","docr":"0.0.4","doctrine":"^0.5.2"},"gitHead":"37be4492c4c6531414fbde0d75ed8b2eaa1223ca","_id":"sl-esprima-ast-utils@0.0.7","_shasum":"37be8ffce12bcb7c7fd330411bce79af3df2d268","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.10.3","_npmUser":{"name":"sealights","email":"alon@sealights.io"},"dist":{"shasum":"37be8ffce12bcb7c7fd330411bce79af3df2d268","tarball":"https://registry.npmjs.org/sl-esprima-ast-utils/-/sl-esprima-ast-utils-0.0.7.tgz","integrity":"sha512-QpAwxPvUuaIZRrb10y0ss2RWFGabGxHYQongFps5pN/p/vpLa1U2/nxT2WwGK+5O1WGxut53kGgJ0pM/oM8WKg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDnjCRXk5bnJRymh2jmKKez4oXvo1muV90SzkzwtO4VBAIgdWV4Fh86t66W3HB4Q4Mj5bqSAaKqaBKfIQJYBWCPqMg="}]},"maintainers":[{"name":"sealights","email":"alon@sealights.io"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/sl-esprima-ast-utils-0.0.7.tgz_1513071047064_0.42429662006907165"}}},"readme":"# esprima-ast-utils [![Build Status](https://secure.travis-ci.org/llafuente/esprima-ast-utils.png?branch=master)](http://travis-ci.org/llafuente/esprima-ast-utils)\n\nNode module to manipulate, transform, query and debug [esprima](https://github.com/ariya/esprima) ASTs.\n\n## Objective\n\nWhen you edit esprima AST and go back to code with escodegen you lose too much information because primary you don't keep track of ranges, tokens, comments etc.\nesprima-ast-utils do this for you, so no escodegen is needed, you can edit the AST directly and code everything is in sync.\n\n## API\n\n#### io\n\n\n##### `parse` (String:str [, Boolean:debug]) -> Object\n\nParse given str\n\n*Parameters:*\n\n* `str`\n\n* `debug`: display $id, $parent and $code in console.log (enumerable=true)\n\n\n*Returns:*\n\n* `Object`\n\n*Note*: location it's not supported, and won't sync with changes, range/rokens do.\n\n\n<br /><br />\n\n##### `parseWrap` (String:str [, Boolean:debug]) -> Object\n\nWrap your code into a function and parse given str.\nNeeded if your code contains a `ReturnStatement` at Program level.\n\n*Parameters:*\n\n* `str`\n\n* `debug`: display $id, $parent and $code in console.log (enumerable=true)\n\n\n*Returns:*\n\n* `Object`\n\n*Note*: location it's not supported, and won't sync with changes, range/rokens do.\n\n\n<br /><br />\n\n##### `parseFile` (String:file [, Boolean:debug]) -> Object\n\nParse given file\n\n*Parameters:*\n\n* `file`: Path\n\n* `debug`: display $id, $parent and $code in console.log (enumerable=true)\n\n\n*Returns:*\n\n* `Object`\n\n*Note*: : NodeJS only\n\n\n<br /><br />\n\n##### `encode` (Object:tree) -> String\n\nReturn tree.$code, just for API completeness.\n\n*Parameters:*\n\n* `tree`\n\n\n*Returns:*\n\n* `String`\n\n\n<br /><br />\n\n#### walk\n\n\n##### `traverse` (Object:node, Function:callback [, Number:depth] [, Boolean:recursive])\n\ntraverse AST\n\n*Parameters:*\n\n* `node`\n\n* `callback`: function(node, parent, property, index, depth)\n  You can return `false` to stop traverse\n\n* `depth`: (0) current depth\n\n* `recursive`: (true) recursively traverse\n\n\n\n<br /><br />\n\n##### `parentize` (Object:root [, Boolean:debug])\n\n`traverse` AST and set $parent node\n\n*Parameters:*\n\n* `root`\n\n* `debug`: display $parent in console.log (enumerable=true)\n\n\n\n<br /><br />\n\n##### `idze` (Object:node [, Boolean:debug])\n\n`traverse` AST and set an unique `$id` to every node\n\n*Parameters:*\n\n* `node`\n\n* `debug`: display $id in console.log (enumerable=true)\n\n\n\n<br /><br />\n\n##### `attachComments` (Object:root)\n\nTraverse the AST and add comments as nodes, so you can query them.\nLoop thought comments and find a proper place to inject (BlockStament or alike)\n* attach the comment to the before nearest children\n* if a children contains the comment it's considered invalid\n* push otherwise\n\n*Parameters:*\n\n* `root`\n\n\n\n<br /><br />\n\n##### `filter` (Object:node, Function:callback [, Function:traverse_fn]) -> Array\n\n`traverse` and `filter` given AST based on given `callback`\n\n*Parameters:*\n\n* `node`\n\n* `callback`\n\n* `traverse_fn`\n\n\n*Returns:*\n\n* `Array`: Every match of the `callback`\n\n\n<br /><br />\n\n##### `getParent` (Object:node, Function:callback) -> Object|NULL\n\nGet parent node based on given callback, stops on `true`\n\n*Parameters:*\n\n* `node`\n\n* `callback`\n\n\n*Returns:*\n\n* `Object|NULL`\n\n\n<br /><br />\n\n##### `getRoot` (Object:node) -> Object\n\nget the root of the AST\n\n*Parameters:*\n\n* `node`\n\n\n*Returns:*\n\n* `Object`\n\n\n<br /><br />\n\n##### `clone` (Object:node) -> Object\n\nRecursive clone a node. Do no include \"$\" properties like $parent or $id\nIf you want those, call `parentize` - `idze` after cloning\n\n*Parameters:*\n\n* `node`\n\n\n*Returns:*\n\n* `Object`\n\n\n<br /><br />\n\n#### debug\n\n\n##### `debug_tree` (Object:tree [, Number:max_width] [, Boolean:display_code_in_tree])\n\nShow your tree in various ways to easy debug\nBig trees will be always a pain, so keep it small if possible\n\n*Parameters:*\n\n* `tree`: Any node, if root tokens & source will be displayed\n\n* `max_width`: max tokens per line\n\n* `display_code_in_tree`: when display the tree attach the code on the right\n\n\n\n<br /><br />\n\n#### query\n\n\n##### `getFunction` (Object:node, String:fn_name) -> Object|NULL\n\n`filter` the AST and return the function with given name, null otherwise.\n\n*Parameters:*\n\n* `node`\n\n* `fn_name`\n\n\n*Returns:*\n\n* `Object|NULL`\n\n\n<br /><br />\n\n##### `getFunctionBlock` (Object:node, String:fn_name) -> Object|NULL\n\n`filter` the AST and return the function > block with given name, null otherwise.\n\n*Parameters:*\n\n* `node`\n\n* `fn_name`\n\n\n*Returns:*\n\n* `Object|NULL`\n\n\n<br /><br />\n\n##### `isFunctionDeclared` (Object:node, String:fn_name) -> Boolean\n\nshortcut\n\n*Parameters:*\n\n* `node`\n\n* `fn_name`\n\n\n*Returns:*\n\n* `Boolean`\n\n\n<br /><br />\n\n##### `hasVarDeclaration` (Object:node, String:var_name) -> Boolean\n\nshortcut\n\n*Parameters:*\n\n* `node`\n\n* `var_name`\n\n\n*Returns:*\n\n* `Boolean`\n\n\n<br /><br />\n\n##### `isVarDeclared` (Object:node, String:var_name) -> Boolean\n\nreverse from node to root and look for a Variable declaration\n\n*Parameters:*\n\n* `node`\n\n* `var_name`\n\n\n*Returns:*\n\n* `Boolean`\n\n*Note*: It's not perfect because `VariableDeclaration` it's not hoisted\n\n\n<br /><br />\n\n##### `contains` (Object:node, Object:subnode) -> Boolean\n\n`node` constains `subnode`\n\n*Parameters:*\n\n* `node`\n\n* `subnode`\n\n\n*Returns:*\n\n* `Boolean`\n\n\n<br /><br />\n\n##### `hasBody` (Object:node) -> Boolean\n\nHas a body property, use to freely attach/detach\n\n*Parameters:*\n\n* `node`\n\n\n*Returns:*\n\n* `Boolean`\n\n\n<br /><br />\n\n##### `isComment` (Object:node) -> Boolean\n\nshortcut: Is a comment (Line or Block) and has text\n\n*Parameters:*\n\n* `node`\n\n\n*Returns:*\n\n* `Boolean`\n\n\n<br /><br />\n\n##### `getComment` (Object:node, String:comment) -> Object\n\nshortcut: search for a comment (trim it's content for maximum compatibility)\n\n*Parameters:*\n\n* `node`\n\n* `comment`\n\n\n*Returns:*\n\n* `Object`\n\n\n<br /><br />\n\n##### `getCode` (Object:node) -> String\n\nshortcut: Return node code\n\n*Parameters:*\n\n* `node`\n\n\n*Returns:*\n\n* `String`\n\n\n<br /><br />\n\n##### `getArgumentList` (Object:node) -> Array\n\nReturn `FunctionDeclaration` arguments name as a list\n\n*Parameters:*\n\n* `node`\n\n\n*Returns:*\n\n* `Array`\n\n\n<br /><br />\n\n##### `getDefaultProperty` (Object:node)\n\n\n*Parameters:*\n\n* `node`\n\n\n\n<br /><br />\n\n#### manipulations\n\n\n##### `attach` (Object:node, String:property, Number|NULL:position, String|Object:str)\n\nAttach Code/Program to given node.\n\n*Parameters:*\n\n* `node`: node to attach\n\n* `property`: Where attach, could be an array or an object\n\n* `position`: index if an array is used as target property\n\n* `str`: String is preferred if not possible remember that only Program can be attached, you may consider using `toProgram`\n\n\n*Note*: tokens are updated\n\n*Note*: range is updated\n\n*Note*: comments are not attached to root.comments (invalid-comments)\n\n\n<br /><br />\n\n##### `attachPunctuator` (Object:tree, String:punctuator, Number:position) -> String\n\nAttach a punctuator and keep the tree ranges sane.\nThe Punctuator can be anything... be careful!\n\n*Parameters:*\n\n* `tree`\n\n* `punctuator`\n\n* `position`\n\n\n*Returns:*\n\n* `String`: detached code string\n\n*Note*: The Punctuator is not parsed and could be assigned to nearest literal or alike.\n\n\n<br /><br />\n\n##### `detach` (Object:node, String:property) -> String\n\nDetach given node from it's parent\n\n*Parameters:*\n\n* `node`\n\n* `property`\n\n\n*Returns:*\n\n* `String`: detached code string\n\n*Note*: `node.$parent` is set to `null`, remember to save it first if you need it.\n\n\n<br /><br />\n\n##### `attachAfter` (Object:node, String|Object:str [, String:property])\n\nAttach after node, that means `node.$parent.type` is a `BockStament`\n\n*Parameters:*\n\n* `node`\n\n* `str`: String is preferred if not possible remember that only Program can be attached, you may consider using `toProgram`\n\n* `property`: where to search node in the parent\n\n\n\n<br /><br />\n\n##### `attachBefore` (Object:node, String|Object:str)\n\nAttach before node, that means `node.$parent.type` is a `BockStament`\n\n*Parameters:*\n\n* `node`\n\n* `str`: String is preferred if not possible remember that only Program can be attached, you may consider using `toProgram`\n\n\n\n<br /><br />\n\n##### `attachAfterComment` (Object:node, String:comment, String|Object:str) -> Boolean\n\nShortcut: Search for given comment, and attachAfter\n\n*Parameters:*\n\n* `node`\n\n* `comment`\n\n* `str`: String is preferred if not possible remember that only Program can be attached, you may consider using `toProgram`\n\n\n*Returns:*\n\n* `Boolean`: success\n\n\n<br /><br />\n\n##### `replace` (Object:node, String|Object:str)\n\nShortcut: detach/attach\n\n*Parameters:*\n\n* `node`\n\n* `str`: String is preferred if not possible remember that only Program can be attached, you may consider using `toProgram`\n\n\n\n<br /><br />\n\n##### `replaceComment` (Object:node, String:comment, String|Object:str)\n\nShortcut: Search for a comment and replace\n\n*Parameters:*\n\n* `node`\n\n* `comment`\n\n* `str`: String is preferred if not possible remember that only Program can be attached, you may consider using `toProgram`\n\n\n\n<br /><br />\n\n##### `injectCode` (Object:tree, Array:range, String|Object:str, Boolean:debug)\n\nInject code directly intro the given range.\nAfter the injection the code will be parsed again so original `$id` will be lost\n\n*Parameters:*\n\n* `tree`\n\n* `range`\n\n* `str`: String is preferred if not possible remember that only Program can be attached, you may consider using `toProgram`\n\n* `debug`: display $id, $parent and $code in console.log (enumerable=true)\n\n\n*Note*: this is dangerous and powerful\n\n\n<br /><br />\n\n#### transformations\n\n\n##### `setIdentifier` (Object:node, String:new_name)\n\nrename `Identifier`\n\n*Parameters:*\n\n* `node`\n\n* `new_name`\n\n\n\n<br /><br />\n\n##### `renameProperty` (Object:node, Object:replacements)\n\n`traverse` and apply given `replacements`\n\n*Parameters:*\n\n* `node`\n\n* `replacements`\n\n\n*Example*:\n```js\nrenameProperty(node, {\"old_var\": \"new_var\", \"much_older\": \"shinnig_new\"})\n```\n\n\n<br /><br />\n\n##### `renameVariable` (Object:node, Object:replacements)\n\n`traverse` and apply given `replacements`\n\n*Parameters:*\n\n* `node`\n\n* `replacements`\n\n\n*Example*:\n```js\nrenameVariable(node, {\"old_var\": \"new_var\", \"much_older\": \"shinnig_new\"})\n```\n\n\n<br /><br />\n\n##### `renameFunction` (Object:node, Object:replacements)\n\ntraverse and apply given `replacements`\n\n*Parameters:*\n\n* `node`\n\n* `replacements`\n\n\n*Example*:\n```js\nrenameFunction(node, {\"old_var\": \"new_var\", \"much_older\": \"shinnig_new\"})\n```\n\n\n<br /><br />\n\n##### `toProgram` (Object|Array:node)\n\nClone given node(s) and extract tokens & code from root to given you a Program-like attachable node\n\n*Parameters:*\n\n* `node`: if array is provided will add all nodes to program.body\n\n\n\n<br /><br />\n\n#### tokens\n\n\n##### `getToken` (Object:tree, Number:start, Number:end) -> Object|NULL\n\nGet token based on given range\n\n*Parameters:*\n\n* `tree`\n\n* `start`\n\n* `end`\n\n\n*Returns:*\n\n* `Object|NULL`\n\n\n<br /><br />\n\n##### `getTokens` (Object:tree, Number:start, Number:end) -> Array|NULL\n\nGet tokens in range\n\n*Parameters:*\n\n* `tree`\n\n* `start`\n\n* `end`\n\n\n*Returns:*\n\n* `Array|NULL`\n\n\n<br /><br />\n\n##### `pushTokens` (Object:tree, Number:start, Number:amount)\n\nPush tokens range from start\n\n*Parameters:*\n\n* `tree`\n\n* `start`\n\n* `amount`\n\n\n*Note*: Update nodes range\n\n\n<br /><br />\n\n##### `growTokens` (Object:tree, Number:start, Number:end, Number:amount)\n\nGrow tokens in given range\n\n*Parameters:*\n\n* `tree`\n\n* `start`\n\n* `end`\n\n* `amount`\n\n\n*Note*: Update nodes range\n\n\n<br /><br />\n\n##### `tokenAt` (Object:tree, Number:start) -> Object\n\nGet the first token\n\n*Parameters:*\n\n* `tree`\n\n* `start`\n\n\n*Returns:*\n\n* `Object`\n\n\n<br /><br />\n\n##### `addTokens` (Object:dst_tree, Object|Array:src, Number:start)\n\nAdd `src` tokens to `dst` since `start` (so keep the order)\n\n*Parameters:*\n\n* `dst_tree`\n\n* `src`\n\n* `start`\n\n\n*Note*: Remember to push `src` tokens before `addTokens` otherwise won't be synced\n\n\n<br /><br />\n\n##### `replaceCodeRange` (Object:tree, Array:range, String:new_text)\n\nReplace code range with given text.\n\n*Parameters:*\n\n* `tree`\n\n* `range`\n\n* `new_text`\n\n\n\n<br /><br />\n\n##### `removeTokens` (Object:tree, Number:start, Number:end)\n\nRemove tokens in range and update ranges\n\n*Parameters:*\n\n* `tree`\n\n* `start`\n\n* `end`\n\n\n*Note*: Do not remove nodes.\n\n\n<br /><br />\n\n# LICENSE\n\n(The MIT License)\n\nCopyright (c) 2014 Luis Lafuente <llafuente@noboxout.com>\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n","maintainers":[{"name":"sealights","email":"alon@sealights.io"}],"time":{"modified":"2022-06-26T20:44:10.514Z","created":"2017-12-12T09:30:47.149Z","0.0.7":"2017-12-12T09:30:47.149Z"},"homepage":"https://github.com/llafuente/esprima-ast-utils","keywords":["arguments","falafel","browserify","jsdoc"],"repository":{"type":"git","url":"git+https://github.com/llafuente/esprima-ast-utils.git"},"author":{"name":"Luis Lafuente","email":"llafuente@noboxout.com","url":"http://www.noboxout.com"},"bugs":{"url":"https://github.com/llafuente/esprima-ast-utils/issues"},"license":"MIT","readmeFilename":"Readme.md"}