react / wstein / node_modules / browserify / node_modules / module-deps / node_modules / detective / node_modules / escodegen / node_modules / estraverse / estraverse.js
80575 views/*1Copyright (C) 2012-2013 Yusuke Suzuki <[email protected]>2Copyright (C) 2012 Ariya Hidayat <[email protected]>34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:67* Redistributions of source code must retain the above copyright8notice, this list of conditions and the following disclaimer.9* Redistributions in binary form must reproduce the above copyright10notice, this list of conditions and the following disclaimer in the11documentation and/or other materials provided with the distribution.1213THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"14AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY17DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/24/*jslint vars:false, bitwise:true*/25/*jshint indent:4*/26/*global exports:true, define:true*/27(function (root, factory) {28'use strict';2930// Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,31// and plain browser loading,32if (typeof define === 'function' && define.amd) {33define(['exports'], factory);34} else if (typeof exports !== 'undefined') {35factory(exports);36} else {37factory((root.estraverse = {}));38}39}(this, function clone(exports) {40'use strict';4142var Syntax,43isArray,44VisitorOption,45VisitorKeys,46objectCreate,47objectKeys,48BREAK,49SKIP,50REMOVE;5152function ignoreJSHintError() { }5354isArray = Array.isArray;55if (!isArray) {56isArray = function isArray(array) {57return Object.prototype.toString.call(array) === '[object Array]';58};59}6061function deepCopy(obj) {62var ret = {}, key, val;63for (key in obj) {64if (obj.hasOwnProperty(key)) {65val = obj[key];66if (typeof val === 'object' && val !== null) {67ret[key] = deepCopy(val);68} else {69ret[key] = val;70}71}72}73return ret;74}7576function shallowCopy(obj) {77var ret = {}, key;78for (key in obj) {79if (obj.hasOwnProperty(key)) {80ret[key] = obj[key];81}82}83return ret;84}85ignoreJSHintError(shallowCopy);8687// based on LLVM libc++ upper_bound / lower_bound88// MIT License8990function upperBound(array, func) {91var diff, len, i, current;9293len = array.length;94i = 0;9596while (len) {97diff = len >>> 1;98current = i + diff;99if (func(array[current])) {100len = diff;101} else {102i = current + 1;103len -= diff + 1;104}105}106return i;107}108109function lowerBound(array, func) {110var diff, len, i, current;111112len = array.length;113i = 0;114115while (len) {116diff = len >>> 1;117current = i + diff;118if (func(array[current])) {119i = current + 1;120len -= diff + 1;121} else {122len = diff;123}124}125return i;126}127ignoreJSHintError(lowerBound);128129objectCreate = Object.create || (function () {130function F() { }131132return function (o) {133F.prototype = o;134return new F();135};136})();137138objectKeys = Object.keys || function (o) {139var keys = [], key;140for (key in o) {141keys.push(key);142}143return keys;144};145146function extend(to, from) {147var keys = objectKeys(from), key, i, len;148for (i = 0, len = keys.length; i < len; i += 1) {149key = keys[i];150to[key] = from[key];151}152return to;153}154155Syntax = {156AssignmentExpression: 'AssignmentExpression',157ArrayExpression: 'ArrayExpression',158ArrayPattern: 'ArrayPattern',159ArrowFunctionExpression: 'ArrowFunctionExpression',160AwaitExpression: 'AwaitExpression', // CAUTION: It's deferred to ES7.161BlockStatement: 'BlockStatement',162BinaryExpression: 'BinaryExpression',163BreakStatement: 'BreakStatement',164CallExpression: 'CallExpression',165CatchClause: 'CatchClause',166ClassBody: 'ClassBody',167ClassDeclaration: 'ClassDeclaration',168ClassExpression: 'ClassExpression',169ComprehensionBlock: 'ComprehensionBlock', // CAUTION: It's deferred to ES7.170ComprehensionExpression: 'ComprehensionExpression', // CAUTION: It's deferred to ES7.171ConditionalExpression: 'ConditionalExpression',172ContinueStatement: 'ContinueStatement',173DebuggerStatement: 'DebuggerStatement',174DirectiveStatement: 'DirectiveStatement',175DoWhileStatement: 'DoWhileStatement',176EmptyStatement: 'EmptyStatement',177ExportBatchSpecifier: 'ExportBatchSpecifier',178ExportDeclaration: 'ExportDeclaration',179ExportSpecifier: 'ExportSpecifier',180ExpressionStatement: 'ExpressionStatement',181ForStatement: 'ForStatement',182ForInStatement: 'ForInStatement',183ForOfStatement: 'ForOfStatement',184FunctionDeclaration: 'FunctionDeclaration',185FunctionExpression: 'FunctionExpression',186GeneratorExpression: 'GeneratorExpression', // CAUTION: It's deferred to ES7.187Identifier: 'Identifier',188IfStatement: 'IfStatement',189ImportDeclaration: 'ImportDeclaration',190ImportDefaultSpecifier: 'ImportDefaultSpecifier',191ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',192ImportSpecifier: 'ImportSpecifier',193Literal: 'Literal',194LabeledStatement: 'LabeledStatement',195LogicalExpression: 'LogicalExpression',196MemberExpression: 'MemberExpression',197MethodDefinition: 'MethodDefinition',198ModuleSpecifier: 'ModuleSpecifier',199NewExpression: 'NewExpression',200ObjectExpression: 'ObjectExpression',201ObjectPattern: 'ObjectPattern',202Program: 'Program',203Property: 'Property',204ReturnStatement: 'ReturnStatement',205SequenceExpression: 'SequenceExpression',206SpreadElement: 'SpreadElement',207SwitchStatement: 'SwitchStatement',208SwitchCase: 'SwitchCase',209TaggedTemplateExpression: 'TaggedTemplateExpression',210TemplateElement: 'TemplateElement',211TemplateLiteral: 'TemplateLiteral',212ThisExpression: 'ThisExpression',213ThrowStatement: 'ThrowStatement',214TryStatement: 'TryStatement',215UnaryExpression: 'UnaryExpression',216UpdateExpression: 'UpdateExpression',217VariableDeclaration: 'VariableDeclaration',218VariableDeclarator: 'VariableDeclarator',219WhileStatement: 'WhileStatement',220WithStatement: 'WithStatement',221YieldExpression: 'YieldExpression'222};223224VisitorKeys = {225AssignmentExpression: ['left', 'right'],226ArrayExpression: ['elements'],227ArrayPattern: ['elements'],228ArrowFunctionExpression: ['params', 'defaults', 'rest', 'body'],229AwaitExpression: ['argument'], // CAUTION: It's deferred to ES7.230BlockStatement: ['body'],231BinaryExpression: ['left', 'right'],232BreakStatement: ['label'],233CallExpression: ['callee', 'arguments'],234CatchClause: ['param', 'body'],235ClassBody: ['body'],236ClassDeclaration: ['id', 'body', 'superClass'],237ClassExpression: ['id', 'body', 'superClass'],238ComprehensionBlock: ['left', 'right'], // CAUTION: It's deferred to ES7.239ComprehensionExpression: ['blocks', 'filter', 'body'], // CAUTION: It's deferred to ES7.240ConditionalExpression: ['test', 'consequent', 'alternate'],241ContinueStatement: ['label'],242DebuggerStatement: [],243DirectiveStatement: [],244DoWhileStatement: ['body', 'test'],245EmptyStatement: [],246ExportBatchSpecifier: [],247ExportDeclaration: ['declaration', 'specifiers', 'source'],248ExportSpecifier: ['id', 'name'],249ExpressionStatement: ['expression'],250ForStatement: ['init', 'test', 'update', 'body'],251ForInStatement: ['left', 'right', 'body'],252ForOfStatement: ['left', 'right', 'body'],253FunctionDeclaration: ['id', 'params', 'defaults', 'rest', 'body'],254FunctionExpression: ['id', 'params', 'defaults', 'rest', 'body'],255GeneratorExpression: ['blocks', 'filter', 'body'], // CAUTION: It's deferred to ES7.256Identifier: [],257IfStatement: ['test', 'consequent', 'alternate'],258ImportDeclaration: ['specifiers', 'source'],259ImportDefaultSpecifier: ['id'],260ImportNamespaceSpecifier: ['id'],261ImportSpecifier: ['id', 'name'],262Literal: [],263LabeledStatement: ['label', 'body'],264LogicalExpression: ['left', 'right'],265MemberExpression: ['object', 'property'],266MethodDefinition: ['key', 'value'],267ModuleSpecifier: [],268NewExpression: ['callee', 'arguments'],269ObjectExpression: ['properties'],270ObjectPattern: ['properties'],271Program: ['body'],272Property: ['key', 'value'],273ReturnStatement: ['argument'],274SequenceExpression: ['expressions'],275SpreadElement: ['argument'],276SwitchStatement: ['discriminant', 'cases'],277SwitchCase: ['test', 'consequent'],278TaggedTemplateExpression: ['tag', 'quasi'],279TemplateElement: [],280TemplateLiteral: ['quasis', 'expressions'],281ThisExpression: [],282ThrowStatement: ['argument'],283TryStatement: ['block', 'handlers', 'handler', 'guardedHandlers', 'finalizer'],284UnaryExpression: ['argument'],285UpdateExpression: ['argument'],286VariableDeclaration: ['declarations'],287VariableDeclarator: ['id', 'init'],288WhileStatement: ['test', 'body'],289WithStatement: ['object', 'body'],290YieldExpression: ['argument']291};292293// unique id294BREAK = {};295SKIP = {};296REMOVE = {};297298VisitorOption = {299Break: BREAK,300Skip: SKIP,301Remove: REMOVE302};303304function Reference(parent, key) {305this.parent = parent;306this.key = key;307}308309Reference.prototype.replace = function replace(node) {310this.parent[this.key] = node;311};312313Reference.prototype.remove = function remove() {314if (isArray(this.parent)) {315this.parent.splice(this.key, 1);316return true;317} else {318this.replace(null);319return false;320}321};322323function Element(node, path, wrap, ref) {324this.node = node;325this.path = path;326this.wrap = wrap;327this.ref = ref;328}329330function Controller() { }331332// API:333// return property path array from root to current node334Controller.prototype.path = function path() {335var i, iz, j, jz, result, element;336337function addToPath(result, path) {338if (isArray(path)) {339for (j = 0, jz = path.length; j < jz; ++j) {340result.push(path[j]);341}342} else {343result.push(path);344}345}346347// root node348if (!this.__current.path) {349return null;350}351352// first node is sentinel, second node is root element353result = [];354for (i = 2, iz = this.__leavelist.length; i < iz; ++i) {355element = this.__leavelist[i];356addToPath(result, element.path);357}358addToPath(result, this.__current.path);359return result;360};361362// API:363// return type of current node364Controller.prototype.type = function () {365var node = this.current();366return node.type || this.__current.wrap;367};368369// API:370// return array of parent elements371Controller.prototype.parents = function parents() {372var i, iz, result;373374// first node is sentinel375result = [];376for (i = 1, iz = this.__leavelist.length; i < iz; ++i) {377result.push(this.__leavelist[i].node);378}379380return result;381};382383// API:384// return current node385Controller.prototype.current = function current() {386return this.__current.node;387};388389Controller.prototype.__execute = function __execute(callback, element) {390var previous, result;391392result = undefined;393394previous = this.__current;395this.__current = element;396this.__state = null;397if (callback) {398result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node);399}400this.__current = previous;401402return result;403};404405// API:406// notify control skip / break407Controller.prototype.notify = function notify(flag) {408this.__state = flag;409};410411// API:412// skip child nodes of current node413Controller.prototype.skip = function () {414this.notify(SKIP);415};416417// API:418// break traversals419Controller.prototype['break'] = function () {420this.notify(BREAK);421};422423// API:424// remove node425Controller.prototype.remove = function () {426this.notify(REMOVE);427};428429Controller.prototype.__initialize = function(root, visitor) {430this.visitor = visitor;431this.root = root;432this.__worklist = [];433this.__leavelist = [];434this.__current = null;435this.__state = null;436this.__fallback = visitor.fallback === 'iteration';437this.__keys = VisitorKeys;438if (visitor.keys) {439this.__keys = extend(objectCreate(this.__keys), visitor.keys);440}441};442443function isNode(node) {444if (node == null) {445return false;446}447return typeof node === 'object' && typeof node.type === 'string';448}449450function isProperty(nodeType, key) {451return (nodeType === Syntax.ObjectExpression || nodeType === Syntax.ObjectPattern) && 'properties' === key;452}453454Controller.prototype.traverse = function traverse(root, visitor) {455var worklist,456leavelist,457element,458node,459nodeType,460ret,461key,462current,463current2,464candidates,465candidate,466sentinel;467468this.__initialize(root, visitor);469470sentinel = {};471472// reference473worklist = this.__worklist;474leavelist = this.__leavelist;475476// initialize477worklist.push(new Element(root, null, null, null));478leavelist.push(new Element(null, null, null, null));479480while (worklist.length) {481element = worklist.pop();482483if (element === sentinel) {484element = leavelist.pop();485486ret = this.__execute(visitor.leave, element);487488if (this.__state === BREAK || ret === BREAK) {489return;490}491continue;492}493494if (element.node) {495496ret = this.__execute(visitor.enter, element);497498if (this.__state === BREAK || ret === BREAK) {499return;500}501502worklist.push(sentinel);503leavelist.push(element);504505if (this.__state === SKIP || ret === SKIP) {506continue;507}508509node = element.node;510nodeType = element.wrap || node.type;511candidates = this.__keys[nodeType];512if (!candidates) {513if (this.__fallback) {514candidates = objectKeys(node);515} else {516throw new Error('Unknown node type ' + nodeType + '.');517}518}519520current = candidates.length;521while ((current -= 1) >= 0) {522key = candidates[current];523candidate = node[key];524if (!candidate) {525continue;526}527528if (isArray(candidate)) {529current2 = candidate.length;530while ((current2 -= 1) >= 0) {531if (!candidate[current2]) {532continue;533}534if (isProperty(nodeType, candidates[current])) {535element = new Element(candidate[current2], [key, current2], 'Property', null);536} else if (isNode(candidate[current2])) {537element = new Element(candidate[current2], [key, current2], null, null);538} else {539continue;540}541worklist.push(element);542}543} else if (isNode(candidate)) {544worklist.push(new Element(candidate, key, null, null));545}546}547}548}549};550551Controller.prototype.replace = function replace(root, visitor) {552function removeElem(element) {553var i,554key,555nextElem,556parent;557558if (element.ref.remove()) {559// When the reference is an element of an array.560key = element.ref.key;561parent = element.ref.parent;562563// If removed from array, then decrease following items' keys.564i = worklist.length;565while (i--) {566nextElem = worklist[i];567if (nextElem.ref && nextElem.ref.parent === parent) {568if (nextElem.ref.key < key) {569break;570}571--nextElem.ref.key;572}573}574}575}576577var worklist,578leavelist,579node,580nodeType,581target,582element,583current,584current2,585candidates,586candidate,587sentinel,588outer,589key;590591this.__initialize(root, visitor);592593sentinel = {};594595// reference596worklist = this.__worklist;597leavelist = this.__leavelist;598599// initialize600outer = {601root: root602};603element = new Element(root, null, null, new Reference(outer, 'root'));604worklist.push(element);605leavelist.push(element);606607while (worklist.length) {608element = worklist.pop();609610if (element === sentinel) {611element = leavelist.pop();612613target = this.__execute(visitor.leave, element);614615// node may be replaced with null,616// so distinguish between undefined and null in this place617if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {618// replace619element.ref.replace(target);620}621622if (this.__state === REMOVE || target === REMOVE) {623removeElem(element);624}625626if (this.__state === BREAK || target === BREAK) {627return outer.root;628}629continue;630}631632target = this.__execute(visitor.enter, element);633634// node may be replaced with null,635// so distinguish between undefined and null in this place636if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {637// replace638element.ref.replace(target);639element.node = target;640}641642if (this.__state === REMOVE || target === REMOVE) {643removeElem(element);644element.node = null;645}646647if (this.__state === BREAK || target === BREAK) {648return outer.root;649}650651// node may be null652node = element.node;653if (!node) {654continue;655}656657worklist.push(sentinel);658leavelist.push(element);659660if (this.__state === SKIP || target === SKIP) {661continue;662}663664nodeType = element.wrap || node.type;665candidates = this.__keys[nodeType];666if (!candidates) {667if (this.__fallback) {668candidates = objectKeys(node);669} else {670throw new Error('Unknown node type ' + nodeType + '.');671}672}673674current = candidates.length;675while ((current -= 1) >= 0) {676key = candidates[current];677candidate = node[key];678if (!candidate) {679continue;680}681682if (isArray(candidate)) {683current2 = candidate.length;684while ((current2 -= 1) >= 0) {685if (!candidate[current2]) {686continue;687}688if (isProperty(nodeType, candidates[current])) {689element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2));690} else if (isNode(candidate[current2])) {691element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2));692} else {693continue;694}695worklist.push(element);696}697} else if (isNode(candidate)) {698worklist.push(new Element(candidate, key, null, new Reference(node, key)));699}700}701}702703return outer.root;704};705706function traverse(root, visitor) {707var controller = new Controller();708return controller.traverse(root, visitor);709}710711function replace(root, visitor) {712var controller = new Controller();713return controller.replace(root, visitor);714}715716function extendCommentRange(comment, tokens) {717var target;718719target = upperBound(tokens, function search(token) {720return token.range[0] > comment.range[0];721});722723comment.extendedRange = [comment.range[0], comment.range[1]];724725if (target !== tokens.length) {726comment.extendedRange[1] = tokens[target].range[0];727}728729target -= 1;730if (target >= 0) {731comment.extendedRange[0] = tokens[target].range[1];732}733734return comment;735}736737function attachComments(tree, providedComments, tokens) {738// At first, we should calculate extended comment ranges.739var comments = [], comment, len, i, cursor;740741if (!tree.range) {742throw new Error('attachComments needs range information');743}744745// tokens array is empty, we attach comments to tree as 'leadingComments'746if (!tokens.length) {747if (providedComments.length) {748for (i = 0, len = providedComments.length; i < len; i += 1) {749comment = deepCopy(providedComments[i]);750comment.extendedRange = [0, tree.range[0]];751comments.push(comment);752}753tree.leadingComments = comments;754}755return tree;756}757758for (i = 0, len = providedComments.length; i < len; i += 1) {759comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));760}761762// This is based on John Freeman's implementation.763cursor = 0;764traverse(tree, {765enter: function (node) {766var comment;767768while (cursor < comments.length) {769comment = comments[cursor];770if (comment.extendedRange[1] > node.range[0]) {771break;772}773774if (comment.extendedRange[1] === node.range[0]) {775if (!node.leadingComments) {776node.leadingComments = [];777}778node.leadingComments.push(comment);779comments.splice(cursor, 1);780} else {781cursor += 1;782}783}784785// already out of owned node786if (cursor === comments.length) {787return VisitorOption.Break;788}789790if (comments[cursor].extendedRange[0] > node.range[1]) {791return VisitorOption.Skip;792}793}794});795796cursor = 0;797traverse(tree, {798leave: function (node) {799var comment;800801while (cursor < comments.length) {802comment = comments[cursor];803if (node.range[1] < comment.extendedRange[0]) {804break;805}806807if (node.range[1] === comment.extendedRange[0]) {808if (!node.trailingComments) {809node.trailingComments = [];810}811node.trailingComments.push(comment);812comments.splice(cursor, 1);813} else {814cursor += 1;815}816}817818// already out of owned node819if (cursor === comments.length) {820return VisitorOption.Break;821}822823if (comments[cursor].extendedRange[0] > node.range[1]) {824return VisitorOption.Skip;825}826}827});828829return tree;830}831832exports.version = '1.8.1-dev';833exports.Syntax = Syntax;834exports.traverse = traverse;835exports.replace = replace;836exports.attachComments = attachComments;837exports.VisitorKeys = VisitorKeys;838exports.VisitorOption = VisitorOption;839exports.Controller = Controller;840exports.cloneEnvironment = function () { return clone({}); };841842return exports;843}));844/* vim: set sw=4 ts=4 et tw=80 : */845846847