estree-walker.umd.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. visit(ast, null, enter, leave);
  8. }
  9. let shouldSkip = false;
  10. const context = { skip: () => shouldSkip = true };
  11. const childKeys = {};
  12. const toString = Object.prototype.toString;
  13. function isArray(thing) {
  14. return toString.call(thing) === '[object Array]';
  15. }
  16. function visit(node, parent, enter, leave, prop, index) {
  17. if (!node) return;
  18. if (enter) {
  19. const _shouldSkip = shouldSkip;
  20. shouldSkip = false;
  21. enter.call(context, node, parent, prop, index);
  22. const skipped = shouldSkip;
  23. shouldSkip = _shouldSkip;
  24. if (skipped) return;
  25. }
  26. const keys = node.type && childKeys[node.type] || (
  27. childKeys[node.type] = Object.keys(node).filter(key => typeof node[key] === 'object')
  28. );
  29. for (let i = 0; i < keys.length; i += 1) {
  30. const key = keys[i];
  31. const value = node[key];
  32. if (isArray(value)) {
  33. for (let j = 0; j < value.length; j += 1) {
  34. value[j] && value[j].type && visit(value[j], node, enter, leave, key, j);
  35. }
  36. }
  37. else if (value && value.type) {
  38. visit(value, node, enter, leave, key, null);
  39. }
  40. }
  41. if (leave) {
  42. leave(node, parent, prop, index);
  43. }
  44. }
  45. exports.walk = walk;
  46. exports.childKeys = childKeys;
  47. Object.defineProperty(exports, '__esModule', { value: true });
  48. })));
  49. //# sourceMappingURL=estree-walker.umd.js.map