You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

esrecurse.js 4.1KB

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