Blame view

node_modules/estree-walker/src/sync.js 2.6 KB
7820380e   “wangming”   1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  // @ts-check
  import { WalkerBase } from './walker.js';
  
  /** @typedef { import('estree').BaseNode} BaseNode */
  /** @typedef { import('./walker.js').WalkerContext} WalkerContext */
  
  /** @typedef {(
   *    this: WalkerContext,
   *    node: BaseNode,
   *    parent: BaseNode,
   *    key: string,
   *    index: number
   * ) => void} SyncHandler */
  
  export class SyncWalker extends WalkerBase {
  	/**
  	 *
  	 * @param {SyncHandler} enter
  	 * @param {SyncHandler} leave
  	 */
  	constructor(enter, leave) {
  		super();
  
  		/** @type {SyncHandler} */
  		this.enter = enter;
  
  		/** @type {SyncHandler} */
  		this.leave = leave;
  	}
  
  	/**
  	 *
  	 * @param {BaseNode} node
  	 * @param {BaseNode} parent
  	 * @param {string} [prop]
  	 * @param {number} [index]
  	 * @returns {BaseNode}
  	 */
  	visit(node, parent, prop, index) {
  		if (node) {
  			if (this.enter) {
  				const _should_skip = this.should_skip;
  				const _should_remove = this.should_remove;
  				const _replacement = this.replacement;
  				this.should_skip = false;
  				this.should_remove = false;
  				this.replacement = null;
  
  				this.enter.call(this.context, node, parent, prop, index);
  
  				if (this.replacement) {
  					node = this.replacement;
  					this.replace(parent, prop, index, node);
  				}
  
  				if (this.should_remove) {
  					this.remove(parent, prop, index);
  				}
  
  				const skipped = this.should_skip;
  				const removed = this.should_remove;
  
  				this.should_skip = _should_skip;
  				this.should_remove = _should_remove;
  				this.replacement = _replacement;
  
  				if (skipped) return node;
  				if (removed) return null;
  			}
  
  			for (const key in node) {
  				const value = node[key];
  
  				if (typeof value !== "object") {
  					continue;
  				} else if (Array.isArray(value)) {
  					for (let i = 0; i < value.length; i += 1) {
  						if (value[i] !== null && typeof value[i].type === 'string') {
  							if (!this.visit(value[i], node, key, i)) {
  								// removed
  								i--;
  							}
  						}
  					}
  				} else if (value !== null && typeof value.type === "string") {
  					this.visit(value, node, key, null);
  				}
  			}
  
  			if (this.leave) {
  				const _replacement = this.replacement;
  				const _should_remove = this.should_remove;
  				this.replacement = null;
  				this.should_remove = false;
  
  				this.leave.call(this.context, node, parent, prop, index);
  
  				if (this.replacement) {
  					node = this.replacement;
  					this.replace(parent, prop, index, node);
  				}
  
  				if (this.should_remove) {
  					this.remove(parent, prop, index);
  				}
  
  				const removed = this.should_remove;
  
  				this.replacement = _replacement;
  				this.should_remove = _should_remove;
  
  				if (removed) return null;
  			}
  		}
  
  		return node;
  	}
  }