pax_global_header00006660000000000000000000000064130556475610014526gustar00rootroot0000000000000052 comment=259814b32cb1e519b4ab000ef78e3a2e41e06b83 astw-2.2.0/000077500000000000000000000000001305564756100125055ustar00rootroot00000000000000astw-2.2.0/.travis.yml000066400000000000000000000001641305564756100146170ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "iojs" before_install: - npm install -g npm@~1.4.6 astw-2.2.0/LICENSE000066400000000000000000000020611305564756100135110ustar00rootroot00000000000000This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. astw-2.2.0/example/000077500000000000000000000000001305564756100141405ustar00rootroot00000000000000astw-2.2.0/example/types.js000066400000000000000000000003401305564756100156370ustar00rootroot00000000000000var astw = require('../'); var deparse = require('escodegen').generate; var walk = astw('4 + beep(5 * 2)'); walk(function (node) { var src = deparse(node); console.log(node.type + ' :: ' + JSON.stringify(src)); }); astw-2.2.0/index.js000066400000000000000000000025401305564756100141530ustar00rootroot00000000000000var parse = require('acorn').parse; module.exports = function (src, opts) { if (!opts) opts = {} var ast = src; if (typeof src === 'string') { try { ast = parse(src, { ecmaVersion: opts.ecmaVersion || 8, allowReturnOutsideFunction: true }) } catch (err) { ast = parse('(' + src + ')') } } return function (cb) { walk(ast, undefined, cb); }; }; function walk (node, parent, cb) { var keys = objectKeys(node); for (var i = 0; i < keys.length; i++) { var key = keys[i]; if (key === 'parent') continue; var child = node[key]; if (isArray(child)) { for (var j = 0; j < child.length; j++) { var c = child[j]; if (c && typeof c.type === 'string') { c.parent = node; walk(c, node, cb); } } } else if (child && typeof child.type === 'string') { child.parent = node; walk(child, node, cb); } } cb(node); } var isArray = Array.isArray || function (xs) { return Object.prototype.toString.call(xs) === '[object Array]'; }; var objectKeys = Object.keys || function (obj) { var keys = []; for (var key in obj) keys.push(key); return keys; }; astw-2.2.0/package.json000066400000000000000000000015611305564756100147760ustar00rootroot00000000000000{ "name": "astw", "version": "2.2.0", "description": "walk the ast with references to parent nodes", "main": "index.js", "dependencies": { "acorn": "^4.0.3" }, "devDependencies": { "tape": "~2.4.1", "escodegen": "~0.0.17" }, "scripts": { "test": "tape test/*.js" }, "testling": { "files": "test/*.js", "browsers": [ "ie/6..latest", "chrome/20..latest", "firefox/10..latest", "safari/latest", "opera/11.0..latest", "iphone/6", "ipad/6" ] }, "repository": { "type": "git", "url": "git://github.com/substack/astw.git" }, "homepage": "https://github.com/substack/astw", "keywords": [ "ast", "walk", "source", "acorn" ], "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" }, "license": "MIT" } astw-2.2.0/readme.markdown000066400000000000000000000021431305564756100155060ustar00rootroot00000000000000# astw walk the ast [![browser support](http://ci.testling.com/substack/astw.png)](http://ci.testling.com/substack/astw) [![build status](https://secure.travis-ci.org/substack/astw.png)](http://travis-ci.org/substack/astw) This module is a faster version of [falafel](https://github.com/substack/node-falafel) that only does ast walking and `.parent` tracking, not source transforms. # example ``` js var astw = require('astw'); var deparse = require('escodegen').generate; var walk = astw('4 + beep(5 * 2)'); walk(function (node) { var src = deparse(node); console.log(node.type + ' :: ' + JSON.stringify(src)); }); ``` # methods ``` js var astw = require('astw') ``` ## var walk = astw(src, opts={}) Return a `walk()` function from the source string or ast object `src`. Optionally: * `opts.ecmaVersion` - default: 8 ## walk(cb) Walk the nodes in the ast with `cb(node)` where `node` is each element in the ast from [esprima](http://esprima.org/) but with an additional `.parent` reference to the parent node. # install With [npm](https://npmjs.org) do: ``` npm install astw ``` # license MIT astw-2.2.0/test/000077500000000000000000000000001305564756100134645ustar00rootroot00000000000000astw-2.2.0/test/json.js000066400000000000000000000005211305564756100147710ustar00rootroot00000000000000var astw = require('../'); var test = require('tape'); test('json', function (t) { t.plan(2); var numbers = [ 1, 2 ]; var walk = astw('{"a":1,"b":2}'); walk(function (node) { if (node.type === 'Literal' && typeof node.value === 'number') { t.equal(node.value, numbers.shift()); } }); }); astw-2.2.0/test/parent.js000066400000000000000000000012641305564756100153160ustar00rootroot00000000000000var astw = require('../'); var test = require('tape'); var generate = require('escodegen').generate; function unparse (s) { return generate(s, { format: { compact: true } }); } test('parent', function (t) { t.plan(4); var walk = astw('(' + function () { var xs = [ 1, 2, 3 ]; fn(ys); } + ')()'); walk(function (node) { if (node.type === 'ArrayExpression') { t.equal(node.parent.type, 'VariableDeclarator'); t.equal(unparse(node.parent), 'xs=[1,2,3]'); t.equal(node.parent.parent.type, 'VariableDeclaration'); t.equal(unparse(node.parent.parent), 'var xs=[1,2,3];'); } }); });