Path: blob/master/node_modules/acorn-walk/dist/walk.js
1126 views
(function (global, factory) {1typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :2typeof define === 'function' && define.amd ? define(['exports'], factory) :3(global = global || self, factory((global.acorn = global.acorn || {}, global.acorn.walk = {})));4}(this, (function (exports) { 'use strict';56// AST walker module for Mozilla Parser API compatible trees78// A simple walk is one where you simply specify callbacks to be9// called on specific nodes. The last two arguments are optional. A10// simple use would be11//12// walk.simple(myTree, {13// Expression: function(node) { ... }14// });15//16// to do something with all expressions. All Parser API node types17// can be used to identify node types, as well as Expression and18// Statement, which denote categories of nodes.19//20// The base argument can be used to pass a custom (recursive)21// walker, and state can be used to give this walked an initial22// state.2324function simple(node, visitors, baseVisitor, state, override) {25if (!baseVisitor) { baseVisitor = base26; }(function c(node, st, override) {27var type = override || node.type, found = visitors[type];28baseVisitor[type](node, st, c);29if (found) { found(node, st); }30})(node, state, override);31}3233// An ancestor walk keeps an array of ancestor nodes (including the34// current node) and passes them to the callback as third parameter35// (and also as state parameter when no other state is present).36function ancestor(node, visitors, baseVisitor, state, override) {37var ancestors = [];38if (!baseVisitor) { baseVisitor = base39; }(function c(node, st, override) {40var type = override || node.type, found = visitors[type];41var isNew = node !== ancestors[ancestors.length - 1];42if (isNew) { ancestors.push(node); }43baseVisitor[type](node, st, c);44if (found) { found(node, st || ancestors, ancestors); }45if (isNew) { ancestors.pop(); }46})(node, state, override);47}4849// A recursive walk is one where your functions override the default50// walkers. They can modify and replace the state parameter that's51// threaded through the walk, and can opt how and whether to walk52// their child nodes (by calling their third argument on these53// nodes).54function recursive(node, state, funcs, baseVisitor, override) {55var visitor = funcs ? make(funcs, baseVisitor || undefined) : baseVisitor56;(function c(node, st, override) {57visitor[override || node.type](node, st, c);58})(node, state, override);59}6061function makeTest(test) {62if (typeof test === "string")63{ return function (type) { return type === test; } }64else if (!test)65{ return function () { return true; } }66else67{ return test }68}6970var Found = function Found(node, state) { this.node = node; this.state = state; };7172// A full walk triggers the callback on each node73function full(node, callback, baseVisitor, state, override) {74if (!baseVisitor) { baseVisitor = base75; }(function c(node, st, override) {76var type = override || node.type;77baseVisitor[type](node, st, c);78if (!override) { callback(node, st, type); }79})(node, state, override);80}8182// An fullAncestor walk is like an ancestor walk, but triggers83// the callback on each node84function fullAncestor(node, callback, baseVisitor, state) {85if (!baseVisitor) { baseVisitor = base; }86var ancestors = []87;(function c(node, st, override) {88var type = override || node.type;89var isNew = node !== ancestors[ancestors.length - 1];90if (isNew) { ancestors.push(node); }91baseVisitor[type](node, st, c);92if (!override) { callback(node, st || ancestors, ancestors, type); }93if (isNew) { ancestors.pop(); }94})(node, state);95}9697// Find a node with a given start, end, and type (all are optional,98// null can be used as wildcard). Returns a {node, state} object, or99// undefined when it doesn't find a matching node.100function findNodeAt(node, start, end, test, baseVisitor, state) {101if (!baseVisitor) { baseVisitor = base; }102test = makeTest(test);103try {104(function c(node, st, override) {105var type = override || node.type;106if ((start == null || node.start <= start) &&107(end == null || node.end >= end))108{ baseVisitor[type](node, st, c); }109if ((start == null || node.start === start) &&110(end == null || node.end === end) &&111test(type, node))112{ throw new Found(node, st) }113})(node, state);114} catch (e) {115if (e instanceof Found) { return e }116throw e117}118}119120// Find the innermost node of a given type that contains the given121// position. Interface similar to findNodeAt.122function findNodeAround(node, pos, test, baseVisitor, state) {123test = makeTest(test);124if (!baseVisitor) { baseVisitor = base; }125try {126(function c(node, st, override) {127var type = override || node.type;128if (node.start > pos || node.end < pos) { return }129baseVisitor[type](node, st, c);130if (test(type, node)) { throw new Found(node, st) }131})(node, state);132} catch (e) {133if (e instanceof Found) { return e }134throw e135}136}137138// Find the outermost matching node after a given position.139function findNodeAfter(node, pos, test, baseVisitor, state) {140test = makeTest(test);141if (!baseVisitor) { baseVisitor = base; }142try {143(function c(node, st, override) {144if (node.end < pos) { return }145var type = override || node.type;146if (node.start >= pos && test(type, node)) { throw new Found(node, st) }147baseVisitor[type](node, st, c);148})(node, state);149} catch (e) {150if (e instanceof Found) { return e }151throw e152}153}154155// Find the outermost matching node before a given position.156function findNodeBefore(node, pos, test, baseVisitor, state) {157test = makeTest(test);158if (!baseVisitor) { baseVisitor = base; }159var max160;(function c(node, st, override) {161if (node.start > pos) { return }162var type = override || node.type;163if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))164{ max = new Found(node, st); }165baseVisitor[type](node, st, c);166})(node, state);167return max168}169170// Fallback to an Object.create polyfill for older environments.171var create = Object.create || function(proto) {172function Ctor() {}173Ctor.prototype = proto;174return new Ctor175};176177// Used to create a custom walker. Will fill in all missing node178// type properties with the defaults.179function make(funcs, baseVisitor) {180var visitor = create(baseVisitor || base);181for (var type in funcs) { visitor[type] = funcs[type]; }182return visitor183}184185function skipThrough(node, st, c) { c(node, st); }186function ignore(_node, _st, _c) {}187188// Node walkers.189190var base = {};191192base.Program = base.BlockStatement = function (node, st, c) {193for (var i = 0, list = node.body; i < list.length; i += 1)194{195var stmt = list[i];196197c(stmt, st, "Statement");198}199};200base.Statement = skipThrough;201base.EmptyStatement = ignore;202base.ExpressionStatement = base.ParenthesizedExpression = base.ChainExpression =203function (node, st, c) { return c(node.expression, st, "Expression"); };204base.IfStatement = function (node, st, c) {205c(node.test, st, "Expression");206c(node.consequent, st, "Statement");207if (node.alternate) { c(node.alternate, st, "Statement"); }208};209base.LabeledStatement = function (node, st, c) { return c(node.body, st, "Statement"); };210base.BreakStatement = base.ContinueStatement = ignore;211base.WithStatement = function (node, st, c) {212c(node.object, st, "Expression");213c(node.body, st, "Statement");214};215base.SwitchStatement = function (node, st, c) {216c(node.discriminant, st, "Expression");217for (var i$1 = 0, list$1 = node.cases; i$1 < list$1.length; i$1 += 1) {218var cs = list$1[i$1];219220if (cs.test) { c(cs.test, st, "Expression"); }221for (var i = 0, list = cs.consequent; i < list.length; i += 1)222{223var cons = list[i];224225c(cons, st, "Statement");226}227}228};229base.SwitchCase = function (node, st, c) {230if (node.test) { c(node.test, st, "Expression"); }231for (var i = 0, list = node.consequent; i < list.length; i += 1)232{233var cons = list[i];234235c(cons, st, "Statement");236}237};238base.ReturnStatement = base.YieldExpression = base.AwaitExpression = function (node, st, c) {239if (node.argument) { c(node.argument, st, "Expression"); }240};241base.ThrowStatement = base.SpreadElement =242function (node, st, c) { return c(node.argument, st, "Expression"); };243base.TryStatement = function (node, st, c) {244c(node.block, st, "Statement");245if (node.handler) { c(node.handler, st); }246if (node.finalizer) { c(node.finalizer, st, "Statement"); }247};248base.CatchClause = function (node, st, c) {249if (node.param) { c(node.param, st, "Pattern"); }250c(node.body, st, "Statement");251};252base.WhileStatement = base.DoWhileStatement = function (node, st, c) {253c(node.test, st, "Expression");254c(node.body, st, "Statement");255};256base.ForStatement = function (node, st, c) {257if (node.init) { c(node.init, st, "ForInit"); }258if (node.test) { c(node.test, st, "Expression"); }259if (node.update) { c(node.update, st, "Expression"); }260c(node.body, st, "Statement");261};262base.ForInStatement = base.ForOfStatement = function (node, st, c) {263c(node.left, st, "ForInit");264c(node.right, st, "Expression");265c(node.body, st, "Statement");266};267base.ForInit = function (node, st, c) {268if (node.type === "VariableDeclaration") { c(node, st); }269else { c(node, st, "Expression"); }270};271base.DebuggerStatement = ignore;272273base.FunctionDeclaration = function (node, st, c) { return c(node, st, "Function"); };274base.VariableDeclaration = function (node, st, c) {275for (var i = 0, list = node.declarations; i < list.length; i += 1)276{277var decl = list[i];278279c(decl, st);280}281};282base.VariableDeclarator = function (node, st, c) {283c(node.id, st, "Pattern");284if (node.init) { c(node.init, st, "Expression"); }285};286287base.Function = function (node, st, c) {288if (node.id) { c(node.id, st, "Pattern"); }289for (var i = 0, list = node.params; i < list.length; i += 1)290{291var param = list[i];292293c(param, st, "Pattern");294}295c(node.body, st, node.expression ? "Expression" : "Statement");296};297298base.Pattern = function (node, st, c) {299if (node.type === "Identifier")300{ c(node, st, "VariablePattern"); }301else if (node.type === "MemberExpression")302{ c(node, st, "MemberPattern"); }303else304{ c(node, st); }305};306base.VariablePattern = ignore;307base.MemberPattern = skipThrough;308base.RestElement = function (node, st, c) { return c(node.argument, st, "Pattern"); };309base.ArrayPattern = function (node, st, c) {310for (var i = 0, list = node.elements; i < list.length; i += 1) {311var elt = list[i];312313if (elt) { c(elt, st, "Pattern"); }314}315};316base.ObjectPattern = function (node, st, c) {317for (var i = 0, list = node.properties; i < list.length; i += 1) {318var prop = list[i];319320if (prop.type === "Property") {321if (prop.computed) { c(prop.key, st, "Expression"); }322c(prop.value, st, "Pattern");323} else if (prop.type === "RestElement") {324c(prop.argument, st, "Pattern");325}326}327};328329base.Expression = skipThrough;330base.ThisExpression = base.Super = base.MetaProperty = ignore;331base.ArrayExpression = function (node, st, c) {332for (var i = 0, list = node.elements; i < list.length; i += 1) {333var elt = list[i];334335if (elt) { c(elt, st, "Expression"); }336}337};338base.ObjectExpression = function (node, st, c) {339for (var i = 0, list = node.properties; i < list.length; i += 1)340{341var prop = list[i];342343c(prop, st);344}345};346base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration;347base.SequenceExpression = function (node, st, c) {348for (var i = 0, list = node.expressions; i < list.length; i += 1)349{350var expr = list[i];351352c(expr, st, "Expression");353}354};355base.TemplateLiteral = function (node, st, c) {356for (var i = 0, list = node.quasis; i < list.length; i += 1)357{358var quasi = list[i];359360c(quasi, st);361}362363for (var i$1 = 0, list$1 = node.expressions; i$1 < list$1.length; i$1 += 1)364{365var expr = list$1[i$1];366367c(expr, st, "Expression");368}369};370base.TemplateElement = ignore;371base.UnaryExpression = base.UpdateExpression = function (node, st, c) {372c(node.argument, st, "Expression");373};374base.BinaryExpression = base.LogicalExpression = function (node, st, c) {375c(node.left, st, "Expression");376c(node.right, st, "Expression");377};378base.AssignmentExpression = base.AssignmentPattern = function (node, st, c) {379c(node.left, st, "Pattern");380c(node.right, st, "Expression");381};382base.ConditionalExpression = function (node, st, c) {383c(node.test, st, "Expression");384c(node.consequent, st, "Expression");385c(node.alternate, st, "Expression");386};387base.NewExpression = base.CallExpression = function (node, st, c) {388c(node.callee, st, "Expression");389if (node.arguments)390{ for (var i = 0, list = node.arguments; i < list.length; i += 1)391{392var arg = list[i];393394c(arg, st, "Expression");395} }396};397base.MemberExpression = function (node, st, c) {398c(node.object, st, "Expression");399if (node.computed) { c(node.property, st, "Expression"); }400};401base.ExportNamedDeclaration = base.ExportDefaultDeclaration = function (node, st, c) {402if (node.declaration)403{ c(node.declaration, st, node.type === "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression"); }404if (node.source) { c(node.source, st, "Expression"); }405};406base.ExportAllDeclaration = function (node, st, c) {407if (node.exported)408{ c(node.exported, st); }409c(node.source, st, "Expression");410};411base.ImportDeclaration = function (node, st, c) {412for (var i = 0, list = node.specifiers; i < list.length; i += 1)413{414var spec = list[i];415416c(spec, st);417}418c(node.source, st, "Expression");419};420base.ImportExpression = function (node, st, c) {421c(node.source, st, "Expression");422};423base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore;424425base.TaggedTemplateExpression = function (node, st, c) {426c(node.tag, st, "Expression");427c(node.quasi, st, "Expression");428};429base.ClassDeclaration = base.ClassExpression = function (node, st, c) { return c(node, st, "Class"); };430base.Class = function (node, st, c) {431if (node.id) { c(node.id, st, "Pattern"); }432if (node.superClass) { c(node.superClass, st, "Expression"); }433c(node.body, st);434};435base.ClassBody = function (node, st, c) {436for (var i = 0, list = node.body; i < list.length; i += 1)437{438var elt = list[i];439440c(elt, st);441}442};443base.MethodDefinition = base.Property = function (node, st, c) {444if (node.computed) { c(node.key, st, "Expression"); }445c(node.value, st, "Expression");446};447448exports.ancestor = ancestor;449exports.base = base;450exports.findNodeAfter = findNodeAfter;451exports.findNodeAround = findNodeAround;452exports.findNodeAt = findNodeAt;453exports.findNodeBefore = findNodeBefore;454exports.full = full;455exports.fullAncestor = fullAncestor;456exports.make = make;457exports.recursive = recursive;458exports.simple = simple;459460Object.defineProperty(exports, '__esModule', { value: true });461462})));463464465