Debian Codegraph @

packages/babel-traverse/src/path/removal.ts

raw symbols SCIP
1 // This file contains methods responsible for removing a node.
2
3 import { hooks } from "./lib/removal-hooks";
4 import { path as pathCache } from "../cache";
5 import type NodePath from "./index";
6 import { REMOVED, SHOULD_SKIP } from "./index";
7
8 export function remove(this: NodePath) {
9 this._assertUnremoved();
10
11 this.resync();
12 if (!this.opts?.noScope) {
13 this._removeFromScope();
14 }
15
16 if (this._callRemovalHooks()) {
17 this._markRemoved();
18 return;
19 }
20
21 this.shareCommentsWithSiblings();
22 this._remove();
23 this._markRemoved();
24 }
25
26 export function _removeFromScope(this: NodePath) {
27 const bindings = this.getBindingIdentifiers();
28 Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
29 }
30
31 export function _callRemovalHooks(this: NodePath) {
32 for (const fn of hooks) {
33 if (fn(this, this.parentPath)) return true;
34 }
35 }
36
37 export function _remove(this: NodePath) {
38 if (Array.isArray(this.container)) {
39 this.container.splice(this.key as number, 1);
40 this.updateSiblingKeys(this.key as number, -1);
41 } else {
42 this._replaceWith(null);
43 }
44 }
45
46 export function _markRemoved(this: NodePath) {
47 // this.shouldSkip = true; this.removed = true;
48 this._traverseFlags |= SHOULD_SKIP | REMOVED;
49 if (this.parent) pathCache.get(this.parent).delete(this.node);
50 this.node = null;
51 }
52
53 export function _assertUnremoved(this: NodePath) {
54 if (this.removed) {
55 throw this.buildCodeFrameError(
56 "NodePath has been removed so is read-only.",
57 );
58 }
59 }