merge remove parent context checks

This commit is contained in:
Sebastian McKenzie 2015-05-25 04:30:17 +01:00
parent 73ada57a17
commit dbe6f1b9a9
2 changed files with 19 additions and 19 deletions

View File

@ -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;
}

View File

@ -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;
}