From 3a365bf24f9c56369ddb50b738b2f1398914b797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 3 Nov 2020 18:15:10 -0500 Subject: [PATCH] fix: remove parent cache when replacing node --- packages/babel-traverse/src/path/removal.js | 2 ++ packages/babel-traverse/src/path/replacement.js | 2 ++ 2 files changed, 4 insertions(+) diff --git a/packages/babel-traverse/src/path/removal.js b/packages/babel-traverse/src/path/removal.js index 07144f2b6bc5..a8bd4523fdc6 100644 --- a/packages/babel-traverse/src/path/removal.js +++ b/packages/babel-traverse/src/path/removal.js @@ -1,6 +1,7 @@ // This file contains methods responsible for removing a node. import { hooks } from "./lib/removal-hooks"; +import { path as pathCache } from "../cache"; import { REMOVED, SHOULD_SKIP } from "./index"; export function remove() { @@ -44,6 +45,7 @@ export function _remove() { export function _markRemoved() { // this.shouldSkip = true; this.removed = true; this._traverseFlags |= SHOULD_SKIP | REMOVED; + if (this.parent) pathCache.get(this.parent).delete(this.node); this.node = null; } diff --git a/packages/babel-traverse/src/path/replacement.js b/packages/babel-traverse/src/path/replacement.js index 211940841367..c3add1418745 100644 --- a/packages/babel-traverse/src/path/replacement.js +++ b/packages/babel-traverse/src/path/replacement.js @@ -3,6 +3,7 @@ import { codeFrameColumns } from "@babel/code-frame"; import traverse from "../index"; import NodePath from "./index"; +import { path as pathCache } from "../cache"; import { parse } from "@babel/parser"; import * as t from "@babel/types"; @@ -49,6 +50,7 @@ export function replaceWithMultiple(nodes: Array) { nodes = this._verifyNodeList(nodes); t.inheritLeadingComments(nodes[0], this.node); t.inheritTrailingComments(nodes[nodes.length - 1], this.node); + pathCache.get(this.parent).delete(this.node); this.node = this.container[this.key] = null; const paths = this.insertAfter(nodes);