class DefinitionsRemover.Definition {
/**
* Subclasses should override to remove the definition from the AST.
*/
protected abstract void performRemove();
}
class DefinitionsRemover.UnknownDefinition {
@Override
public void performRemove() {
throw new IllegalArgumentException("Can't remove an UnknownDefinition");
}
}
class DefinitionsRemover.ExternalNameOnlyDefinition {
@Override
public void performRemove() {
throw new IllegalArgumentException(
"Can't remove external name-only definition");
}
}
class DefinitionsRemover.FunctionArgumentDefinition {
@Override
public void performRemove() {
throw new IllegalArgumentException(
"Can't remove a FunctionArgumentDefinition");
}
}
class DefinitionsRemover.NamedFunctionDefinition {
@Override
public void performRemove() {
function.detachFromParent();
}
}
class DefinitionsRemover.FunctionExpressionDefinition {
@Override
public void performRemove() {
// replace internal name with ""
function.replaceChild(function.getFirstChild(),
Node.newString(Token.NAME, ""));
}
}
class DefinitionsRemover.AssignmentDefinition {
@Override
public void performRemove() {
// A simple assignment. foo = bar() -> bar();
Node parent = assignment.getParent();
Node last = assignment.getLastChild();
assignment.removeChild(last);
parent.replaceChild(assignment, last);
}
}
class DefinitionsRemover.ObjectLiteralPropertyDefinition {
@Override
public void performRemove() {
literal.removeChild(name);
}
}
class DefinitionsRemover.VarDefinition {
@Override
public void performRemove() {
Node var = name.getParent();
Preconditions.checkState(var.getFirstChild() == var.getLastChild(),
"AST should be normalized first");
Node parent = var.getParent();
Node rValue = name.removeFirstChild();
Preconditions.checkState(parent.getType() != Token.FOR);
parent.replaceChild(var, NodeUtil.newExpr(rValue));
}
}
|