source: imaps-frontend/node_modules/esrecurse/esrecurse.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[d565449]1/*
2 Copyright (C) 2014 Yusuke Suzuki <utatane.tea@gmail.com>
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24(function () {
25 'use strict';
26
27 var estraverse = require('estraverse');
28
29 function isNode(node) {
30 if (node == null) {
31 return false;
32 }
33 return typeof node === 'object' && typeof node.type === 'string';
34 }
35
36 function isProperty(nodeType, key) {
37 return (nodeType === estraverse.Syntax.ObjectExpression || nodeType === estraverse.Syntax.ObjectPattern) && key === 'properties';
38 }
39
40 function Visitor(visitor, options) {
41 options = options || {};
42
43 this.__visitor = visitor || this;
44 this.__childVisitorKeys = options.childVisitorKeys
45 ? Object.assign({}, estraverse.VisitorKeys, options.childVisitorKeys)
46 : estraverse.VisitorKeys;
47 if (options.fallback === 'iteration') {
48 this.__fallback = Object.keys;
49 } else if (typeof options.fallback === 'function') {
50 this.__fallback = options.fallback;
51 }
52 }
53
54 /* Default method for visiting children.
55 * When you need to call default visiting operation inside custom visiting
56 * operation, you can use it with `this.visitChildren(node)`.
57 */
58 Visitor.prototype.visitChildren = function (node) {
59 var type, children, i, iz, j, jz, child;
60
61 if (node == null) {
62 return;
63 }
64
65 type = node.type || estraverse.Syntax.Property;
66
67 children = this.__childVisitorKeys[type];
68 if (!children) {
69 if (this.__fallback) {
70 children = this.__fallback(node);
71 } else {
72 throw new Error('Unknown node type ' + type + '.');
73 }
74 }
75
76 for (i = 0, iz = children.length; i < iz; ++i) {
77 child = node[children[i]];
78 if (child) {
79 if (Array.isArray(child)) {
80 for (j = 0, jz = child.length; j < jz; ++j) {
81 if (child[j]) {
82 if (isNode(child[j]) || isProperty(type, children[i])) {
83 this.visit(child[j]);
84 }
85 }
86 }
87 } else if (isNode(child)) {
88 this.visit(child);
89 }
90 }
91 }
92 };
93
94 /* Dispatching node. */
95 Visitor.prototype.visit = function (node) {
96 var type;
97
98 if (node == null) {
99 return;
100 }
101
102 type = node.type || estraverse.Syntax.Property;
103 if (this.__visitor[type]) {
104 this.__visitor[type].call(this, node);
105 return;
106 }
107 this.visitChildren(node);
108 };
109
110 exports.version = require('./package.json').version;
111 exports.Visitor = Visitor;
112 exports.visit = function (node, visitor, options) {
113 var v = new Visitor(visitor, options);
114 v.visit(node);
115 };
116}());
117/* vim: set sw=4 ts=4 et tw=80 : */
Note: See TracBrowser for help on using the repository browser.