rename Path#remove to Path#dangerouslyRemove
This commit is contained in:
parent
2d43ffb5b4
commit
98eb30c482
@ -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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,5 +4,5 @@ export var metadata = {
|
||||
};
|
||||
|
||||
export function DebuggerStatement(node) {
|
||||
this.remove();
|
||||
this.dangerouslyRemove();
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
},
|
||||
|
||||
@ -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")) {
|
||||
|
||||
@ -41,7 +41,7 @@ export function replaceWithMultiple(nodes: Array<Object>) {
|
||||
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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user