From 98eb30c4828e5210f767180ae1aa19db21334a82 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 25 May 2015 16:17:28 +0100 Subject: [PATCH] rename Path#remove to Path#dangerouslyRemove --- src/babel/transformation/modules/system.js | 4 +- .../transformation/transformers/es6/for-of.js | 2 +- .../minification/dead-code-elimination.js | 6 +- .../minification/remove-console.js | 4 +- .../minification/remove-debugger.js | 2 +- .../transformation/transformers/other/flow.js | 6 +- src/babel/traversal/path/lib/removal-hooks.js | 86 +------------------ src/babel/traversal/path/removal.js | 9 ++ src/babel/traversal/path/replacement.js | 6 +- 9 files changed, 26 insertions(+), 99 deletions(-) diff --git a/src/babel/transformation/modules/system.js b/src/babel/transformation/modules/system.js index 6dab2e44c2..866fc168f1 100644 --- a/src/babel/transformation/modules/system.js +++ b/src/babel/transformation/modules/system.js @@ -49,7 +49,7 @@ var hoistFunctionsVisitor = { if (t.isFunctionDeclaration(node) || state.formatter._canHoist(node)) { state.handlerBody.push(node); - this.remove(); + this.dangerouslyRemove(); } } }; @@ -68,7 +68,7 @@ var runnerSettersVisitor = { state.nodes.push(node); } - this.remove(); + this.dangerouslyRemove(); } } }; diff --git a/src/babel/transformation/transformers/es6/for-of.js b/src/babel/transformation/transformers/es6/for-of.js index 631f54af59..82c4904d5c 100644 --- a/src/babel/transformation/transformers/es6/for-of.js +++ b/src/babel/transformation/transformers/es6/for-of.js @@ -31,7 +31,7 @@ export function ForOfStatement(node, parent, scope, file) { if (build.replaceParent) { this.parentPath.replaceWithMultiple(build.node); - this.remove(); + this.dangerouslyRemove(); } else { return build.node; } diff --git a/src/babel/transformation/transformers/minification/dead-code-elimination.js b/src/babel/transformation/transformers/minification/dead-code-elimination.js index ec94d976b3..f39d8ab2b2 100644 --- a/src/babel/transformation/transformers/minification/dead-code-elimination.js +++ b/src/babel/transformation/transformers/minification/dead-code-elimination.js @@ -49,14 +49,14 @@ export function ReferencedIdentifier(node, parent, scope) { t.toExpression(replacement); scope.removeBinding(node.name); - binding.path.remove(); + binding.path.dangerouslyRemove(); return replacement; } export function FunctionDeclaration(node, parent, scope) { var bindingInfo = scope.getBinding(node.id.name); if (bindingInfo && !bindingInfo.referenced) { - this.remove(); + this.dangerouslyRemove(); } } @@ -106,7 +106,7 @@ export var IfStatement = { if (alternate) { return toStatements(alternate); } else { - return this.remove(); + return this.dangerouslyRemove(); } } diff --git a/src/babel/transformation/transformers/minification/remove-console.js b/src/babel/transformation/transformers/minification/remove-console.js index cdbeb53a38..9c97208cc9 100644 --- a/src/babel/transformation/transformers/minification/remove-console.js +++ b/src/babel/transformation/transformers/minification/remove-console.js @@ -8,9 +8,9 @@ export var metadata = { export function CallExpression(node, parent) { if (this.get("callee").matchesPattern("console", true)) { if (t.isExpressionStatement(parent)) { - this.parentPath.remove(); + this.parentPath.dangerouslyRemove(); } else { - this.remove(); + this.dangerouslyRemove(); } } } diff --git a/src/babel/transformation/transformers/minification/remove-debugger.js b/src/babel/transformation/transformers/minification/remove-debugger.js index 0ac60ffa59..c3ab38f5af 100644 --- a/src/babel/transformation/transformers/minification/remove-debugger.js +++ b/src/babel/transformation/transformers/minification/remove-debugger.js @@ -4,5 +4,5 @@ export var metadata = { }; export function DebuggerStatement(node) { - this.remove(); + this.dangerouslyRemove(); } diff --git a/src/babel/transformation/transformers/other/flow.js b/src/babel/transformation/transformers/other/flow.js index d493527fd5..828bbecba4 100644 --- a/src/babel/transformation/transformers/other/flow.js +++ b/src/babel/transformation/transformers/other/flow.js @@ -1,5 +1,5 @@ export function Flow(node) { - this.remove(); + this.dangerouslyRemove(); } export function ClassProperty(node) { @@ -22,9 +22,9 @@ export function TypeCastExpression(node) { } export function ImportDeclaration(node) { - if (node.isType) this.remove(); + if (node.isType) this.dangerouslyRemove(); } export function ExportDeclaration(node) { - if (this.get("declaration").isTypeAlias()) this.remove(); + if (this.get("declaration").isTypeAlias()) this.dangerouslyRemove(); } diff --git a/src/babel/traversal/path/lib/removal-hooks.js b/src/babel/traversal/path/lib/removal-hooks.js index c2face1c6e..04aaf47bdd 100644 --- a/src/babel/traversal/path/lib/removal-hooks.js +++ b/src/babel/traversal/path/lib/removal-hooks.js @@ -1,22 +1,7 @@ -// this file contains all the cases where we have to perform additional logic when performing -// specific operations in order to retain correct javascript semantics - -// guidelines for rules: -// -// - removing a node must **never** result in invalid javascript -// - always clean up the ancestry -// - worry about execution sideeffects of removing entire nodes and replace with siblings/children -// +// this file contains hooks that handle ancestry cleanup of parent nodes when removing children import * as t from "../../../types"; -// todo: -// -// - catch clause param -// - property key -// - tagged template literal tag -// - // pre hooks should be used for either rejecting removal or delegating removal to a replacement export var pre = [ function (self) { @@ -30,62 +15,6 @@ export var pre = [ } }, - function (self, parent) { - if (self.key === "id" && parent.isClassDeclaration() && !self.scope.isPure(self.node.superClass)) { - // class NODE extends superClass() {} - // removing the id of a class declaration with an impure super class so we just replace it with the - // super class - self.replaceWithMultiple([ - t.expressionStatement(self.node.superClass), - t.identifier("undefined") - ]); - return true; - } - }, - - function (self, parent) { - if (self.key === "callee" && parent.isCallExpression()) { - // NODE(); - // attempting to remove the callee of a call expression, in order to retain semantics we'll turn all - // the arguments into a sequence expression and return `undefined` - - var expressions = parent.node.arguments; - - // filter expressions that don't effect the execution of the code - expressions = expressions.filter((node) => !self.scope.isPure(node)); - - // push undefined as otherwise we'll evaluate to the last argument - expressions.push(t.identifier("undefined")); - - if (expressions.length === 1) { - parent.replaceWith(expressions[0]); - } else { - parent.replaceWith(t.sequenceExpression(expressions)); - } - - return true; - } - }, - - function (self, parent) { - if (self.key === "test" && parent.isConditionalExpression()) { - // NODE ? consequent : alternate - // removing the test of a conditional so just use the alternate since consequent will never be hit - parent.replaceWith(parent.node.alternate); - return true; - } - }, - - function (self, parent) { - if (self.key === "test" && parent.IfStatement() && parent.node.alternate) { - // if (NODE) consequent else alternate - // removing the test of an if statement and there's an alternate so just replace the entire if with it - // todo: check if this is a BlockStatement and whether it has any block scoped variables - parent.replaceWith(parent.node.alternate); - return true; - } - }, - function (self, parent) { var replace = false; @@ -114,17 +43,6 @@ export var post = [ // unlikely that the latter will ever be what's wanted so we just remove the loop to avoid infinite recursion removeParent = removeParent || (self.key === "test" && (parent.isWhile() || parent.isSwitchCase())); - // function NODE() {} - // class NODE {} - // attempting to remove the id of a declaration, no choice really but to purge it - // if it's a class that has a super class that's not prue then it's handled in a pre hook - removeParent = removeParent || (self.key === "id" && (parent.isClassDeclaration() || parent.isFunctionDeclaration())); - - // var NODE = init; - // kill variable declarator if we're removing it's initializer binding - // todo: possibly explode the variable declarations to retain it's initializer - removeParent = removeParent || (self.key === "init" && parent.isVariableDeclarator()); - // export NODE; // just remove a declaration for an export as this is no longer valid removeParent = removeParent || (self.key === "declaration" && parent.isExportDeclaration()); @@ -147,7 +65,7 @@ export var post = [ removeParent = removeParent || (self.key === "test" && parent.isIfStatement()); if (removeParent) { - parent.remove(); + parent.dangerouslyRemove(); return true; } }, diff --git a/src/babel/traversal/path/removal.js b/src/babel/traversal/path/removal.js index 6e832f0cb8..6e89210126 100644 --- a/src/babel/traversal/path/removal.js +++ b/src/babel/traversal/path/removal.js @@ -5,6 +5,15 @@ import * as removalHooks from "./lib/removal-hooks"; */ export function remove() { + console.trace("Path#remove has been renamed to Path#dangerouslyRemove, removing a node is extremely dangerous so please refrain using it."); + return this.dangerouslyRemove(); +} + +/** + * Description + */ + +export function dangerouslyRemove() { this.resync(); if (this._callRemovalHooks("pre")) { diff --git a/src/babel/traversal/path/replacement.js b/src/babel/traversal/path/replacement.js index 4e2e7c8ff7..49b772868f 100644 --- a/src/babel/traversal/path/replacement.js +++ b/src/babel/traversal/path/replacement.js @@ -41,7 +41,7 @@ export function replaceWithMultiple(nodes: Array) { t.inheritsComments(nodes[0], this.node); this.node = this.container[this.key] = null; this.insertAfter(nodes); - if (!this.node) this.remove(); + if (!this.node) this.dangerouslyRemove(); } /** @@ -80,7 +80,7 @@ export function replaceWith(replacement, whateverAllowed) { } if (!replacement) { - throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead"); + throw new Error("You passed `path.replaceWith()` a falsy node, use `path.dangerouslyRemove()` instead"); } if (this.node === replacement) { @@ -181,7 +181,7 @@ export function replaceInline(nodes) { if (Array.isArray(this.container)) { nodes = this._verifyNodeList(nodes); this._containerInsertAfter(nodes); - return this.remove(); + return this.dangerouslyRemove(); } else { return this.replaceWithMultiple(nodes); }