/
removal.js
56 lines (46 loc) 路 1.24 KB
/
removal.js
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
// This file contains methods responsible for removing a node.
import { hooks } from "./lib/removal-hooks";
import { REMOVED, SHOULD_SKIP } from "./index";
export function remove() {
this._assertUnremoved();
this.resync();
if (!this.opts || !this.opts.noScope) {
this._removeFromScope();
}
if (this._callRemovalHooks()) {
this._markRemoved();
return;
}
this.shareCommentsWithSiblings();
this._remove();
this._markRemoved();
}
export function _removeFromScope() {
const bindings = this.getBindingIdentifiers();
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
}
export function _callRemovalHooks() {
for (const fn of (hooks: Array<Function>)) {
if (fn(this, this.parentPath)) return true;
}
}
export function _remove() {
if (Array.isArray(this.container)) {
this.container.splice(this.key, 1);
this.updateSiblingKeys(this.key, -1);
} else {
this._replaceWith(null);
}
}
export function _markRemoved() {
// this.shouldSkip = true; this.removed = true;
this._traverseFlags |= SHOULD_SKIP | REMOVED;
this.node = null;
}
export function _assertUnremoved() {
if (this.removed) {
throw this.buildCodeFrameError(
"NodePath has been removed so is read-only.",
);
}
}