{"name":"astravel","version":"0.2.0","description":"ESTree-compliant AST walker and modifier.","main":"./dist/astravel.min.js","scripts":{"test":"./node_modules/mocha/bin/mocha test/index.js","prepublish":"./node_modules/browserify/bin/cmd.js --transform [ babelify ] --plugin [ minifyify --no-map ] --no-builtins --standalone astravel --require ./src/astravel.js:astravel --outfile dist/astravel.min.js","build":"./node_modules/browserify/bin/cmd.js --transform [ babelify ] --no-builtins --standalone astravel --require ./src/astravel.js:astravel --outfile dist/astravel.js","start":"./node_modules/watchify/bin/cmd.js --transform [ babelify ] --no-builtins --debug --verbose --standalone astravel --require ./src/astravel.js:astravel --outfile dist/astravel.debug.js"},"keywords":["ast","ast walker","ast modifier","estree"],"repository":{"type":"git","url":"git+https://github.com/davidbonnet/astravel.git"},"author":{"name":"David Bonnet","email":"david@bonnet.cc"},"license":"MIT","devDependencies":{"acorn":"^2.1.0","astring":"^0.3.0","babelify":"^6.1.3","browserify":"^11.0.1","minifyify":"^7.0.3","mocha":"^2.2.5","watchify":"^3.3.1"},"gitHead":"0b25d254d82b12b16d78142376dcd5de80fb20b7","bugs":{"url":"https://github.com/davidbonnet/astravel/issues"},"homepage":"https://github.com/davidbonnet/astravel#readme","_id":"astravel@0.2.0","_shasum":"e18aaabcb25f65545ac02d26f1fcaaabe4e719c5","_from":".","_npmVersion":"2.12.0","_nodeVersion":"2.0.2","_npmUser":{"name":"davidbonnet","email":"david@bonnet.cc"},"dist":{"shasum":"e18aaabcb25f65545ac02d26f1fcaaabe4e719c5","tarball":"http://registry-npm.ywwl.com/astravel/download/astravel-0.2.0.tgz","integrity":"sha512-5XiNjDwq3fuNtFQEwFOFtB7/lh16cdNvv8WXLyjZywH9iET4eRovWMQoNMMPbCPTjiu1pdETrZ3sJ9WjIpPV3w==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHk2yE9iBBN8cOpTTdvvv7lJA8vzOalZYAgnQ3pLBkF7AiEA2eq/f2H7V97rWsVgN/Rvjs0PXSJJb+txxv6YGibsFNo="}],"size":6657,"noattachment":false,"key":"/astravel/-/astravel-0.2.0.tgz"},"maintainers":[{"name":"davidbonnet","email":"david@bonnet.cc"}],"directories":{},"readme":"# Astravel\n\n[![NPM Version](https://img.shields.io/npm/v/astravel.svg)](https://www.npmjs.org/package/astravel)\n[![Build Status](https://travis-ci.org/davidbonnet/astravel.svg?branch=master)](https://travis-ci.org/davidbonnet/astravel)\n[![Coverage](https://codecov.io/gh/davidbonnet/astravel/branch/master/graph/badge.svg)](https://codecov.io/gh/davidbonnet/astravel)\n[![devDependency Status](https://david-dm.org/davidbonnet/astravel/dev-status.svg)](https://david-dm.org/davidbonnet/astravel#info=devDependencies)\n[![Greenkeeper](https://badges.greenkeeper.io/davidbonnet/astravel.svg)](https://greenkeeper.io/)\n\n👟 A tiny and fast [ESTree](https://github.com/estree/estree)-compliant AST walker and modifier.\n\n### Key features\n\n- Works on [ESTree](https://github.com/estree/estree)-compliant ASTs (JavaScript [version 13 (2022)](https://tc39.github.io/ecma262/)), such as the ones produced by [Meriyah](https://github.com/meriyah/meriyah).\n- Out-of-the-box functions such as source code comments insertion for [Astring](https://github.com/davidbonnet/astring).\n- Extensible with custom nodes.\n- No dependencies and small footprint.\n\n## Installation\n\nInstall with the [Node Package Manager](https://www.npmjs.com/package/astravel):\n\n```bash\nnpm install astravel\n```\n\nAlternatively, checkout this repository and install the development dependencies to build the module file:\n\n```bash\ngit clone https://github.com/davidbonnet/astravel.git\ncd astravel\nnpm install\n```\n\n## Usage\n\nThe `astravel` module exports the following items:\n\n<!-- MarkdownTOC autolink=\"true\" levels=\"4\" -->\n\n- [`defaultTraveler`](#defaulttraveler)\n- [`makeTraveler()`](#maketraveler)\n- [`attachComments()`](#attachcomments)\n\n<!-- /MarkdownTOC -->\n\n#### `defaultTraveler`\n\n> ⬅️ `traveler`\n\n> ⚠️ Deprecated in favor of ES6 class notation.\n\nThis object describes a basic AST traveler. It contains the following methods:\n\n- `go(node, state)`: Travels through the provided AST `node` with a given `state` (an object that can be of any type) by recursively calling this method.\n- `find(predicate, node, state) ➞ { node, state }?`: Returns `{ node, state }` for which `predicate(node, state)` returns truthy, starting at the specified AST `node` and with the provided `state`. Otherwise, returns `undefined`.\n- `[NodeType](node, state)`: Method handler for a specific `NodeType`.\n- `makeChild(properties) ➞ traveler`: Returns a custom AST traveler that inherits from `this` traveler with its own provided `properties` and the property `super` that points to `this` traveler.\n\n#### `makeTraveler()`\n\n> ➡️ `(properties)`\n> ⬅️ `traveler`\n\n> ⚠️ Deprecated in favor of ES6 class notation.\n\nThis function is similar to `astravel.defaultTraveler.makeChild`: it returns a traveler that inherits from the `defaultTraveler` with its own provided `properties` and the property `super` that points to the `defaultTraveler` object. These properties should redefine the traveler's behavior by implementing the `go(node, state)` method and/or any node handler.\n\nWhen redefining the `go` method, make sure its basic functionality is kept by calling the parent's `go` method to keep traveling through the AST:\n\n```javascript\nconst customTraveler = makeTraveler({\n  go: function (node, state) {\n    // Code before entering the node\n    console.log('Entering ' + node.type)\n    // Call the parent's `go` method\n    this.super.go.call(this, node, state)\n    // Code after leaving the node\n    console.log('Leaving ' + node.type)\n  },\n})\n```\n\nTo skip specific node types, the most effective way is to replace the corresponding node handlers with a function that does nothing:\n\n```javascript\nimport { makeTraveler } from 'astravel'\n\nconst ignore = Function.prototype\nconst customTraveler = makeTraveler({\n  FunctionDeclaration: ignore,\n  FunctionExpression: ignore,\n  ArrowFunctionExpression: ignore,\n})\n```\n\n#### `attachComments()`\n\n> ➡️ `(ast, comments)`\n> ⬅️ `ast`\n\nThis function attaches a list of `comments` to the corresponding nodes of a provided `ast` and returns that same `ast`. The `ast` is modified in-place and only the nodes getting comments are augmented with a `comments` and/or a `trailingComments` array property.\n\nEach comment should be an object with the following properties:\n\n- `type`: `\"Line\"` or `\"Block\"`\n- `value`: Comment string value\n- `start`: Comment starting character offset number\n- `end`: Comment ending character offset number\n- `loc`: Location object with `start` and `end` properties containing one-based `line` number and zero-based `column` number properties.\n\nThe following examples show how to obtain a proper list of `comments` of a given source `code` and how to attach them on the generated `ast`:\n\n##### Usage with [Meriyah](https://github.com/meriyah/meriyah)\n\n```javascript\nimport { parse } from 'meriyah'\nimport { attachComments } from 'astravel'\n\nconst comments = []\nconst ast = parse(code, {\n  // Comments are stored in this array\n  onComment: comments,\n})\n// Attach comments on the AST\nattachComments(ast, comments)\n```\n\n##### Usage with [Acorn](https://github.com/acornjs/acorn)\n\n```javascript\nimport { parse } from 'acorn'\nimport { attachComments } from 'astravel'\n\nconst comments = []\nconst ast = parse(code, {\n  // This ensures that the `loc` property is present on comment objects\n  locations: true,\n  onComment: comments,\n})\nattachComments(ast, comments)\n```\n\nThe algorithm assumes that comments are not put in exotic places, such as in-between function arguments, and proceeds as follows:\n\n- For a given statement, it stores all comments right above it and on the same line to it's right side in a `comments` property.\n- If a comment block is at the beginning of a code block, it is attached to that code block.\n- Comments not followed by any statement in a code block are attached as `trailingComments` to that code block.\n\nIn this example, the comments tell to which statement they are attached:\n\n```javascript\n// Attached to the variable declaration just below\nconst point = {\n  // Attached to the property definition just below\n  x: 0,\n  y: 0, // Attached to the property definition on its left\n}\n/*\nAttached to the function declaration just below.\n*/\nfunction add(a, b) {\n  /*\n   Attached to the function body because it is the first comment block.\n   */\n  return a + b // Attached to the return statement on its left\n  // Trailing comment attached as such to the function body\n}\n// Trailing comment attached as such to the program body\n```\n","publish_time":1439453391891,"_cnpm_publish_time":1439453391891,"dist-tags":{"latest":"0.6.1"}}