react / wstein / node_modules / browserify / node_modules / insert-module-globals / node_modules / lexical-scope / node_modules / astw / index.js
80551 viewsvar parse = require('acorn').parse;12module.exports = function (src) {3var ast = src;4if (typeof src === 'string') {5try {6ast = parse(src, {7ecmaVersion: 6,8allowReturnOutsideFunction: true9})10}11catch (err) { ast = parse('(' + src + ')') }12}13return function (cb) {14walk(ast, undefined, cb);15};16};1718function walk (node, parent, cb) {19var keys = objectKeys(node);20for (var i = 0; i < keys.length; i++) {21var key = keys[i];22if (key === 'parent') continue;2324var child = node[key];25if (isArray(child)) {26for (var j = 0; j < child.length; j++) {27var c = child[j];28if (c && typeof c.type === 'string') {29c.parent = node;30walk(c, node, cb);31}32}33}34else if (child && typeof child.type === 'string') {35child.parent = node;36walk(child, node, cb);37}38}39cb(node);40}4142var isArray = Array.isArray || function (xs) {43return Object.prototype.toString.call(xs) === '[object Array]';44};4546var objectKeys = Object.keys || function (obj) {47var keys = [];48for (var key in obj) keys.push(key);49return keys;50};515253