estree-walker.umd.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (factory((global.estreeWalker = {})));
  5. }(this, (function (exports) { 'use strict';
  6. function walk(ast, { enter, leave }) {
  7. return visit(ast, null, enter, leave);
  8. }
  9. let should_skip = false;
  10. let should_remove = false;
  11. let replacement = null;
  12. const context = {
  13. skip: () => should_skip = true,
  14. remove: () => should_remove = true,
  15. replace: (node) => replacement = node
  16. };
  17. function replace(parent, prop, index, node) {
  18. if (parent) {
  19. if (index !== null) {
  20. parent[prop][index] = node;
  21. } else {
  22. parent[prop] = node;
  23. }
  24. }
  25. }
  26. function remove(parent, prop, index) {
  27. if (parent) {
  28. if (index !== null) {
  29. parent[prop].splice(index, 1);
  30. } else {
  31. delete parent[prop];
  32. }
  33. }
  34. }
  35. function visit(
  36. node,
  37. parent,
  38. enter,
  39. leave,
  40. prop,
  41. index
  42. ) {
  43. if (node) {
  44. if (enter) {
  45. const _should_skip = should_skip;
  46. const _should_remove = should_remove;
  47. const _replacement = replacement;
  48. should_skip = false;
  49. should_remove = false;
  50. replacement = null;
  51. enter.call(context, node, parent, prop, index);
  52. if (replacement) {
  53. node = replacement;
  54. replace(parent, prop, index, node);
  55. }
  56. if (should_remove) {
  57. remove(parent, prop, index);
  58. }
  59. const skipped = should_skip;
  60. const removed = should_remove;
  61. should_skip = _should_skip;
  62. should_remove = _should_remove;
  63. replacement = _replacement;
  64. if (skipped) return node;
  65. if (removed) return null;
  66. }
  67. for (const key in node) {
  68. const value = (node )[key];
  69. if (typeof value !== 'object') {
  70. continue;
  71. }
  72. else if (Array.isArray(value)) {
  73. for (let j = 0, k = 0; j < value.length; j += 1, k += 1) {
  74. if (value[j] !== null && typeof value[j].type === 'string') {
  75. if (!visit(value[j], node, enter, leave, key, k)) {
  76. // removed
  77. j--;
  78. }
  79. }
  80. }
  81. }
  82. else if (value !== null && typeof value.type === 'string') {
  83. visit(value, node, enter, leave, key, null);
  84. }
  85. }
  86. if (leave) {
  87. const _replacement = replacement;
  88. const _should_remove = should_remove;
  89. replacement = null;
  90. should_remove = false;
  91. leave.call(context, node, parent, prop, index);
  92. if (replacement) {
  93. node = replacement;
  94. replace(parent, prop, index, node);
  95. }
  96. if (should_remove) {
  97. remove(parent, prop, index);
  98. }
  99. const removed = should_remove;
  100. replacement = _replacement;
  101. should_remove = _should_remove;
  102. if (removed) return null;
  103. }
  104. }
  105. return node;
  106. }
  107. exports.walk = walk;
  108. Object.defineProperty(exports, '__esModule', { value: true });
  109. })));