Sha256: 4575ef0837f79110ed42ee4fdc95beaf09e2204127ca0e729a3d4a00adb7aab0

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

var parse = require('esprima').parse;

module.exports = function (src) {
    var ast = typeof src === 'string' ? parse(src) : 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;
};

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
sprockets-browserify-0.3.0 node_modules/browserify/node_modules/insert-module-globals/node_modules/lexical-scope/node_modules/astw/index.js
sprockets-browserify-0.2.0 node_modules/browserify/node_modules/insert-module-globals/node_modules/lexical-scope/node_modules/astw/index.js
ruby-wisp-source-0.8.0 vendor/node_modules/browserify/node_modules/insert-module-globals/node_modules/lexical-scope/node_modules/astw/index.js
ruby-wisp-source-0.7.0 vendor/node_modules/browserify/node_modules/insert-module-globals/node_modules/lexical-scope/node_modules/astw/index.js