pull/7/head
yann300 8 years ago
parent 5f5c4c7ce3
commit e8786f49ff
  1. 4
      src/index.js
  2. 40
      src/util/astWalker.js

@ -5,6 +5,7 @@ var BasicPanel = require('./ui/BasicPanel')
var TraceManager = require('./trace/traceManager')
var CodeManager = require('./code/codeManager')
var SourceMappingDecoder = require('./util/sourceMappingDecoder')
var AstWalker = require('./util/astWalker')
if (typeof (module) !== 'undefined' && typeof (module.exports) !== 'undefined') {
module.exports = modules()
@ -24,7 +25,8 @@ function modules () {
BasicPanel: BasicPanel
},
util: {
SourceMappingDecoder: SourceMappingDecoder
SourceMappingDecoder: SourceMappingDecoder,
AstWalker: AstWalker
}
}
}

@ -0,0 +1,40 @@
'use strict'
/*
Crawl the given AST through the function walk(ast, callback)
*/
function AstWalker () {
}
/*
* visit all the AST nodes
*
* @param {Object} ast - AST node
* @param {Object or Function} callback - if (Function) the function will be called for every node.
* - if (Object) callback[<Node Type>] will be called for every node of type <Node Type>. callback["*"] will be called fo all other nodes.
* in each case, if the callback returns false it does not descend into children. If no callback for the current type, children are visited.
*/
AstWalker.prototype.walk = function (ast, callback) {
crawlNode(ast, callback)
}
function crawlNode (node, callback) {
if (manageCallBack(node, callback) && node.children && node.children.length > 0) {
for (var k in node.children) {
var child = node.children[k]
crawlNode(child, callback)
}
}
}
function manageCallBack (node, callback) {
if (callback instanceof Function) {
return callback(null, node)
} else if (callback instanceof Object) {
if (callback[node.name] instanceof 'Function') {
return callback[node.name](null, node)
} else if (callback['*'] instanceof 'Function') {
return callback['*'](null, node)
} else {
return true
}
}
}
Loading…
Cancel
Save