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