michael@0: // AST walker module for Mozilla Parser API compatible trees michael@0: michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS michael@0: if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD michael@0: mod((this.acorn || (this.acorn = {})).walk = {}); // Plain browser env michael@0: })(function(exports) { michael@0: "use strict"; michael@0: michael@0: // A simple walk is one where you simply specify callbacks to be michael@0: // called on specific nodes. The last two arguments are optional. A michael@0: // simple use would be michael@0: // michael@0: // walk.simple(myTree, { michael@0: // Expression: function(node) { ... } michael@0: // }); michael@0: // michael@0: // to do something with all expressions. All Parser API node types michael@0: // can be used to identify node types, as well as Expression, michael@0: // Statement, and ScopeBody, which denote categories of nodes. michael@0: // michael@0: // The base argument can be used to pass a custom (recursive) michael@0: // walker, and state can be used to give this walked an initial michael@0: // state. michael@0: exports.simple = function(node, visitors, base, state) { michael@0: if (!base) base = exports.base; michael@0: function c(node, st, override) { michael@0: var type = override || node.type, found = visitors[type]; michael@0: base[type](node, st, c); michael@0: if (found) found(node, st); michael@0: } michael@0: c(node, state); michael@0: }; michael@0: michael@0: // A recursive walk is one where your functions override the default michael@0: // walkers. They can modify and replace the state parameter that's michael@0: // threaded through the walk, and can opt how and whether to walk michael@0: // their child nodes (by calling their third argument on these michael@0: // nodes). michael@0: exports.recursive = function(node, state, funcs, base) { michael@0: var visitor = funcs ? exports.make(funcs, base) : base; michael@0: function c(node, st, override) { michael@0: visitor[override || node.type](node, st, c); michael@0: } michael@0: c(node, state); michael@0: }; michael@0: michael@0: function makeTest(test) { michael@0: if (typeof test == "string") michael@0: return function(type) { return type == test; }; michael@0: else if (!test) michael@0: return function() { return true; }; michael@0: else michael@0: return test; michael@0: } michael@0: michael@0: function Found(node, state) { this.node = node; this.state = state; } michael@0: michael@0: // Find a node with a given start, end, and type (all are optional, michael@0: // null can be used as wildcard). Returns a {node, state} object, or michael@0: // undefined when it doesn't find a matching node. michael@0: exports.findNodeAt = function(node, start, end, test, base, state) { michael@0: test = makeTest(test); michael@0: try { michael@0: if (!base) base = exports.base; michael@0: var c = function(node, st, override) { michael@0: var type = override || node.type; michael@0: if ((start == null || node.start <= start) && michael@0: (end == null || node.end >= end)) michael@0: base[type](node, st, c); michael@0: if (test(type, node) && michael@0: (start == null || node.start == start) && michael@0: (end == null || node.end == end)) michael@0: throw new Found(node, st); michael@0: }; michael@0: c(node, state); michael@0: } catch (e) { michael@0: if (e instanceof Found) return e; michael@0: throw e; michael@0: } michael@0: }; michael@0: michael@0: // Find the innermost node of a given type that contains the given michael@0: // position. Interface similar to findNodeAt. michael@0: exports.findNodeAround = function(node, pos, test, base, state) { michael@0: test = makeTest(test); michael@0: try { michael@0: if (!base) base = exports.base; michael@0: var c = function(node, st, override) { michael@0: var type = override || node.type; michael@0: if (node.start > pos || node.end < pos) return; michael@0: base[type](node, st, c); michael@0: if (test(type, node)) throw new Found(node, st); michael@0: }; michael@0: c(node, state); michael@0: } catch (e) { michael@0: if (e instanceof Found) return e; michael@0: throw e; michael@0: } michael@0: }; michael@0: michael@0: // Find the outermost matching node after a given position. michael@0: exports.findNodeAfter = function(node, pos, test, base, state) { michael@0: test = makeTest(test); michael@0: try { michael@0: if (!base) base = exports.base; michael@0: var c = function(node, st, override) { michael@0: if (node.end < pos) return; michael@0: var type = override || node.type; michael@0: if (node.start >= pos && test(type, node)) throw new Found(node, st); michael@0: base[type](node, st, c); michael@0: }; michael@0: c(node, state); michael@0: } catch (e) { michael@0: if (e instanceof Found) return e; michael@0: throw e; michael@0: } michael@0: }; michael@0: michael@0: // Find the outermost matching node before a given position. michael@0: exports.findNodeBefore = function(node, pos, test, base, state) { michael@0: test = makeTest(test); michael@0: if (!base) base = exports.base; michael@0: var max; michael@0: var c = function(node, st, override) { michael@0: if (node.start > pos) return; michael@0: var type = override || node.type; michael@0: if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node)) michael@0: max = new Found(node, st); michael@0: base[type](node, st, c); michael@0: }; michael@0: c(node, state); michael@0: return max; michael@0: }; michael@0: michael@0: // Used to create a custom walker. Will fill in all missing node michael@0: // type properties with the defaults. michael@0: exports.make = function(funcs, base) { michael@0: if (!base) base = exports.base; michael@0: var visitor = {}; michael@0: for (var type in base) visitor[type] = base[type]; michael@0: for (var type in funcs) visitor[type] = funcs[type]; michael@0: return visitor; michael@0: }; michael@0: michael@0: function skipThrough(node, st, c) { c(node, st); } michael@0: function ignore(_node, _st, _c) {} michael@0: michael@0: // Node walkers. michael@0: michael@0: var base = exports.base = {}; michael@0: base.Program = base.BlockStatement = function(node, st, c) { michael@0: for (var i = 0; i < node.body.length; ++i) michael@0: c(node.body[i], st, "Statement"); michael@0: }; michael@0: base.Statement = skipThrough; michael@0: base.EmptyStatement = ignore; michael@0: base.ExpressionStatement = function(node, st, c) { michael@0: c(node.expression, st, "Expression"); michael@0: }; michael@0: base.IfStatement = function(node, st, c) { michael@0: c(node.test, st, "Expression"); michael@0: c(node.consequent, st, "Statement"); michael@0: if (node.alternate) c(node.alternate, st, "Statement"); michael@0: }; michael@0: base.LabeledStatement = function(node, st, c) { michael@0: c(node.body, st, "Statement"); michael@0: }; michael@0: base.BreakStatement = base.ContinueStatement = ignore; michael@0: base.WithStatement = function(node, st, c) { michael@0: c(node.object, st, "Expression"); michael@0: c(node.body, st, "Statement"); michael@0: }; michael@0: base.SwitchStatement = function(node, st, c) { michael@0: c(node.discriminant, st, "Expression"); michael@0: for (var i = 0; i < node.cases.length; ++i) { michael@0: var cs = node.cases[i]; michael@0: if (cs.test) c(cs.test, st, "Expression"); michael@0: for (var j = 0; j < cs.consequent.length; ++j) michael@0: c(cs.consequent[j], st, "Statement"); michael@0: } michael@0: }; michael@0: base.ReturnStatement = function(node, st, c) { michael@0: if (node.argument) c(node.argument, st, "Expression"); michael@0: }; michael@0: base.ThrowStatement = function(node, st, c) { michael@0: c(node.argument, st, "Expression"); michael@0: }; michael@0: base.TryStatement = function(node, st, c) { michael@0: c(node.block, st, "Statement"); michael@0: if (node.handler) c(node.handler.body, st, "ScopeBody"); michael@0: if (node.finalizer) c(node.finalizer, st, "Statement"); michael@0: }; michael@0: base.WhileStatement = function(node, st, c) { michael@0: c(node.test, st, "Expression"); michael@0: c(node.body, st, "Statement"); michael@0: }; michael@0: base.DoWhileStatement = base.WhileStatement; michael@0: base.ForStatement = function(node, st, c) { michael@0: if (node.init) c(node.init, st, "ForInit"); michael@0: if (node.test) c(node.test, st, "Expression"); michael@0: if (node.update) c(node.update, st, "Expression"); michael@0: c(node.body, st, "Statement"); michael@0: }; michael@0: base.ForInStatement = function(node, st, c) { michael@0: c(node.left, st, "ForInit"); michael@0: c(node.right, st, "Expression"); michael@0: c(node.body, st, "Statement"); michael@0: }; michael@0: base.ForInit = function(node, st, c) { michael@0: if (node.type == "VariableDeclaration") c(node, st); michael@0: else c(node, st, "Expression"); michael@0: }; michael@0: base.DebuggerStatement = ignore; michael@0: michael@0: base.FunctionDeclaration = function(node, st, c) { michael@0: c(node, st, "Function"); michael@0: }; michael@0: base.VariableDeclaration = function(node, st, c) { michael@0: for (var i = 0; i < node.declarations.length; ++i) { michael@0: var decl = node.declarations[i]; michael@0: if (decl.init) c(decl.init, st, "Expression"); michael@0: } michael@0: }; michael@0: michael@0: base.Function = function(node, st, c) { michael@0: c(node.body, st, "ScopeBody"); michael@0: }; michael@0: base.ScopeBody = function(node, st, c) { michael@0: c(node, st, "Statement"); michael@0: }; michael@0: michael@0: base.Expression = skipThrough; michael@0: base.ThisExpression = ignore; michael@0: base.ArrayExpression = function(node, st, c) { michael@0: for (var i = 0; i < node.elements.length; ++i) { michael@0: var elt = node.elements[i]; michael@0: if (elt) c(elt, st, "Expression"); michael@0: } michael@0: }; michael@0: base.ObjectExpression = function(node, st, c) { michael@0: for (var i = 0; i < node.properties.length; ++i) michael@0: c(node.properties[i].value, st, "Expression"); michael@0: }; michael@0: base.FunctionExpression = base.FunctionDeclaration; michael@0: base.SequenceExpression = function(node, st, c) { michael@0: for (var i = 0; i < node.expressions.length; ++i) michael@0: c(node.expressions[i], st, "Expression"); michael@0: }; michael@0: base.UnaryExpression = base.UpdateExpression = function(node, st, c) { michael@0: c(node.argument, st, "Expression"); michael@0: }; michael@0: base.BinaryExpression = base.AssignmentExpression = base.LogicalExpression = function(node, st, c) { michael@0: c(node.left, st, "Expression"); michael@0: c(node.right, st, "Expression"); michael@0: }; michael@0: base.ConditionalExpression = function(node, st, c) { michael@0: c(node.test, st, "Expression"); michael@0: c(node.consequent, st, "Expression"); michael@0: c(node.alternate, st, "Expression"); michael@0: }; michael@0: base.NewExpression = base.CallExpression = function(node, st, c) { michael@0: c(node.callee, st, "Expression"); michael@0: if (node.arguments) for (var i = 0; i < node.arguments.length; ++i) michael@0: c(node.arguments[i], st, "Expression"); michael@0: }; michael@0: base.MemberExpression = function(node, st, c) { michael@0: c(node.object, st, "Expression"); michael@0: if (node.computed) c(node.property, st, "Expression"); michael@0: }; michael@0: base.Identifier = base.Literal = ignore; michael@0: michael@0: // A custom walker that keeps track of the scope chain and the michael@0: // variables defined in it. michael@0: function makeScope(prev, isCatch) { michael@0: return {vars: Object.create(null), prev: prev, isCatch: isCatch}; michael@0: } michael@0: function normalScope(scope) { michael@0: while (scope.isCatch) scope = scope.prev; michael@0: return scope; michael@0: } michael@0: exports.scopeVisitor = exports.make({ michael@0: Function: function(node, scope, c) { michael@0: var inner = makeScope(scope); michael@0: for (var i = 0; i < node.params.length; ++i) michael@0: inner.vars[node.params[i].name] = {type: "argument", node: node.params[i]}; michael@0: if (node.id) { michael@0: var decl = node.type == "FunctionDeclaration"; michael@0: (decl ? normalScope(scope) : inner).vars[node.id.name] = michael@0: {type: decl ? "function" : "function name", node: node.id}; michael@0: } michael@0: c(node.body, inner, "ScopeBody"); michael@0: }, michael@0: TryStatement: function(node, scope, c) { michael@0: c(node.block, scope, "Statement"); michael@0: if (node.handler) { michael@0: var inner = makeScope(scope, true); michael@0: inner.vars[node.handler.param.name] = {type: "catch clause", node: node.handler.param}; michael@0: c(node.handler.body, inner, "ScopeBody"); michael@0: } michael@0: if (node.finalizer) c(node.finalizer, scope, "Statement"); michael@0: }, michael@0: VariableDeclaration: function(node, scope, c) { michael@0: var target = normalScope(scope); michael@0: for (var i = 0; i < node.declarations.length; ++i) { michael@0: var decl = node.declarations[i]; michael@0: target.vars[decl.id.name] = {type: "var", node: decl.id}; michael@0: if (decl.init) c(decl.init, scope, "Expression"); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: });