From dbe6f1b9a969f8401b82caf60db5f3c1cd9a7141 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 25 May 2015 04:30:17 +0100 Subject: [PATCH] merge remove parent context checks --- src/babel/traversal/path/context.js | 8 +++-- src/babel/traversal/path/lib/removal-hooks.js | 30 ++++++++----------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/babel/traversal/path/context.js b/src/babel/traversal/path/context.js index 41e18b6870..11674a9c2f 100644 --- a/src/babel/traversal/path/context.js +++ b/src/babel/traversal/path/context.js @@ -135,7 +135,9 @@ export function setContext(context, file) { } /** - * Description + * Here we resync the node paths `key` and `container`. If they've changed according + * to what we have stored internally then we attempt to resync by crawling and looking + * for the new values. */ export function resync() { @@ -149,7 +151,7 @@ export function _resyncKey() { if (this.node === this.container[this.key]) return; // grrr, path key is out of sync. this is likely due to a modification to the AST - // not through our path APIs + // not done through our path APIs if (Array.isArray(this.container)) { for (var i = 0; i < this.container.length; i++) { @@ -176,6 +178,8 @@ export function _resyncContainer() { var newContainer = parentPath.node[containerKey]; if (!newContainer || this.container === newContainer) return; + // container is out of sync. this is likely the result of it being reassigned + this.container = newContainer; } diff --git a/src/babel/traversal/path/lib/removal-hooks.js b/src/babel/traversal/path/lib/removal-hooks.js index fc6e60ae9b..31909f755c 100644 --- a/src/babel/traversal/path/lib/removal-hooks.js +++ b/src/babel/traversal/path/lib/removal-hooks.js @@ -24,25 +24,21 @@ export var pre = [ // post hooks should be used for cleaning up parents export var post = [ function (self, parent) { - // just remove a declaration for an export so this is no longer valid - if (self.key === "declaration" && parent.isExportDeclaration()) { - parent.remove(); - return true; - } - }, + var removeParent = false; - function (self, parent) { - // we've just removed the last declarator of a variable declaration so there's no point in - // keeping it - if (parent.isVariableDeclaration() && parent.node.declarations.length === 0) { - parent.remove(); - return true; - } - }, + // just remove a declaration for an export as this is no longer valid + removeParent = removeParent || (self.key === "declaration" && parent.isExportDeclaration()); - function (self, parent) { - // we're the child of an expression statement so we should remove the parent - if (parent.isExpressionStatement()) { + // stray labels with no body + removeParent = removeParent || (self.key === "body" && parent.isLabeledStatement()); + + // remove an entire declaration if there are no declarators left + removeParent = removeParent || (parent.isVariableDeclaration() && parent.node.declarations.length === 0); + + // remove the entire expression statement if there's no expression + removeParent = removeParent || (self.key === "expression" && parent.isExpressionStatement()); + + if (removeParent) { parent.remove(); return true; }