Remove unnecessary inner class to make this pass more consistent with other compiler passes.
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkState;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.Node;
@@ -39,7 +38,7 @@ import com.google.javascript.rhino.Node;
* "function f" is block scoped, which may cause issues.
*
*/
-class CollapseAnonymousFunctions implements CompilerPass {
+class CollapseAnonymousFunctions extends AbstractPostOrderCallback implements CompilerPass {
private final AbstractCompiler compiler;
public CollapseAnonymousFunctions(AbstractCompiler compiler) {
@@ -49,10 +48,9 @@ class CollapseAnonymousFunctions implements CompilerPass {
@Override
public void process(Node externs, Node root) {
- NodeTraversal.traverseEs6(compiler, root, new Callback());
+ NodeTraversal.traverseEs6(compiler, root, this);
}
- private class Callback extends AbstractPostOrderCallback {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (!n.isVar()) {
@@ -67,18 +65,15 @@ class CollapseAnonymousFunctions implements CompilerPass {
Node grandparent = parent.getParent();
if (!(parent.isScript()
- || grandparent != null && grandparent.isFunction() && parent.isNormalBlock())) {
+ || (grandparent != null && grandparent.isFunction() && parent.isNormalBlock()))) {
return;
}
// Need to store the next name in case the current name is removed from
// the linked list.
- checkState(n.hasOneChild(), n);
- Node name = n.getFirstChild();
+ Node name = n.getOnlyChild();
Node value = name.getFirstChild();
- if (value != null &&
- value.isFunction() &&
- !isRecursiveFunction(value)) {
+ if (value != null && value.isFunction() && !isRecursiveFunction(value)) {
Node fnName = value.getFirstChild();
fnName.setString(name.getString());
NodeUtil.copyNameAnnotations(name, fnName);
@@ -86,8 +81,7 @@ class CollapseAnonymousFunctions implements CompilerPass {
parent.replaceChild(n, value);
// Renormalize the code.
- if (!t.inGlobalScope() &&
- NodeUtil.isHoistedFunctionDeclaration(value)) {
+ if (!t.inGlobalScope() && NodeUtil.isHoistedFunctionDeclaration(value)) {
parent.addChildToFront(value.detach());
}
@@ -120,4 +114,3 @@ class CollapseAnonymousFunctions implements CompilerPass {
return false;
}
}
-}
