| 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 |
|
|---|
| 7 | function walk(ast, { enter, leave }) {
|
|---|
| 8 | return visit(ast, null, enter, leave);
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | let should_skip = false;
|
|---|
| 12 | let should_remove = false;
|
|---|
| 13 | let replacement = null;
|
|---|
| 14 | const context = {
|
|---|
| 15 | skip: () => should_skip = true,
|
|---|
| 16 | remove: () => should_remove = true,
|
|---|
| 17 | replace: (node) => replacement = node
|
|---|
| 18 | };
|
|---|
| 19 |
|
|---|
| 20 | function replace(parent, prop, index, node) {
|
|---|
| 21 | if (parent) {
|
|---|
| 22 | if (index !== null) {
|
|---|
| 23 | parent[prop][index] = node;
|
|---|
| 24 | } else {
|
|---|
| 25 | parent[prop] = node;
|
|---|
| 26 | }
|
|---|
| 27 | }
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | function remove(parent, prop, index) {
|
|---|
| 31 | if (parent) {
|
|---|
| 32 | if (index !== null) {
|
|---|
| 33 | parent[prop].splice(index, 1);
|
|---|
| 34 | } else {
|
|---|
| 35 | delete parent[prop];
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | function visit(
|
|---|
| 41 | node,
|
|---|
| 42 | parent,
|
|---|
| 43 | enter,
|
|---|
| 44 | leave,
|
|---|
| 45 | prop,
|
|---|
| 46 | index
|
|---|
| 47 | ) {
|
|---|
| 48 | if (node) {
|
|---|
| 49 | if (enter) {
|
|---|
| 50 | const _should_skip = should_skip;
|
|---|
| 51 | const _should_remove = should_remove;
|
|---|
| 52 | const _replacement = replacement;
|
|---|
| 53 | should_skip = false;
|
|---|
| 54 | should_remove = false;
|
|---|
| 55 | replacement = null;
|
|---|
| 56 |
|
|---|
| 57 | enter.call(context, node, parent, prop, index);
|
|---|
| 58 |
|
|---|
| 59 | if (replacement) {
|
|---|
| 60 | node = replacement;
|
|---|
| 61 | replace(parent, prop, index, node);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | if (should_remove) {
|
|---|
| 65 | remove(parent, prop, index);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | const skipped = should_skip;
|
|---|
| 69 | const removed = should_remove;
|
|---|
| 70 |
|
|---|
| 71 | should_skip = _should_skip;
|
|---|
| 72 | should_remove = _should_remove;
|
|---|
| 73 | replacement = _replacement;
|
|---|
| 74 |
|
|---|
| 75 | if (skipped) return node;
|
|---|
| 76 | if (removed) return null;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | for (const key in node) {
|
|---|
| 80 | const value = (node )[key];
|
|---|
| 81 |
|
|---|
| 82 | if (typeof value !== 'object') {
|
|---|
| 83 | continue;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | else if (Array.isArray(value)) {
|
|---|
| 87 | for (let j = 0, k = 0; j < value.length; j += 1, k += 1) {
|
|---|
| 88 | if (value[j] !== null && typeof value[j].type === 'string') {
|
|---|
| 89 | if (!visit(value[j], node, enter, leave, key, k)) {
|
|---|
| 90 | // removed
|
|---|
| 91 | j--;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | else if (value !== null && typeof value.type === 'string') {
|
|---|
| 98 | visit(value, node, enter, leave, key, null);
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | if (leave) {
|
|---|
| 103 | const _replacement = replacement;
|
|---|
| 104 | const _should_remove = should_remove;
|
|---|
| 105 | replacement = null;
|
|---|
| 106 | should_remove = false;
|
|---|
| 107 |
|
|---|
| 108 | leave.call(context, node, parent, prop, index);
|
|---|
| 109 |
|
|---|
| 110 | if (replacement) {
|
|---|
| 111 | node = replacement;
|
|---|
| 112 | replace(parent, prop, index, node);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if (should_remove) {
|
|---|
| 116 | remove(parent, prop, index);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | const removed = should_remove;
|
|---|
| 120 |
|
|---|
| 121 | replacement = _replacement;
|
|---|
| 122 | should_remove = _should_remove;
|
|---|
| 123 |
|
|---|
| 124 | if (removed) return null;
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | return node;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | exports.walk = walk;
|
|---|
| 132 |
|
|---|
| 133 | Object.defineProperty(exports, '__esModule', { value: true });
|
|---|
| 134 |
|
|---|
| 135 | })));
|
|---|