Refactor Es6RewriteModules to exit early instead of indenting forward.
@@ -234,12 +234,18 @@ public final class Es6RewriteModules extends AbstractPostOrderCallback
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
- if (n.isCall()) {
+ if (!n.isCall()) {
+ return;
+ }
+
boolean isRequire = n.getFirstChild().matchesQualifiedName("goog.require");
boolean isGet = n.getFirstChild().matchesQualifiedName("goog.module.get");
boolean isForwardDeclare = n.getFirstChild().matchesQualifiedName("goog.forwardDeclare");
- if (isRequire || isGet || isForwardDeclare) {
+ if (!isRequire && !isGet && !isForwardDeclare) {
+ return;
+ }
+
if (!n.hasTwoChildren() || !n.getLastChild().isString()) {
if (isRequire) {
t.report(n, INVALID_REQUIRE_NAMESPACE);
@@ -276,6 +282,7 @@ public final class Es6RewriteModules extends AbstractPostOrderCallback
Node statementNode = NodeUtil.getEnclosingStatement(n);
boolean importHasAlias = NodeUtil.isNameDeclaration(statementNode);
+
if (importHasAlias) {
if (statementNode.getFirstChild().isDestructuringLhs()) {
if (isForwardDeclare) {
@@ -304,16 +311,17 @@ public final class Es6RewriteModules extends AbstractPostOrderCallback
t.reportCodeChange();
} else {
if (isForwardDeclare) {
- if (statementNode.isConst()) {
+ if (!statementNode.isConst()) {
+ t.report(statementNode, FORWARD_DECLARE_FOR_ES6_SHOULD_BE_CONST);
+ return;
+ }
+ // const namespace = goog.forwardDeclare('an.es6.namespace');
forwardDeclareRenameTable.put(
t.getScopeRoot(),
statementNode.getFirstChild().getString(),
ModuleRenaming.getGlobalName(moduleMetadata, name));
statementNode.detach();
t.reportCodeChange();
- } else {
- t.report(statementNode, FORWARD_DECLARE_FOR_ES6_SHOULD_BE_CONST);
- }
} else {
// const module = goog.require('an.es6.namespace');
// const module = module$es6;
@@ -325,10 +333,12 @@ public final class Es6RewriteModules extends AbstractPostOrderCallback
}
} else {
if (isForwardDeclare) {
+ // goog.forwardDeclare('an.es6.namespace')
forwardDeclareRenameTable.put(
t.getScopeRoot(), name, ModuleRenaming.getGlobalName(moduleMetadata, name));
statementNode.detach();
} else {
+ // goog.require('an.es6.namespace')
if (statementNode.isExprResult() && statementNode.getFirstChild() == n) {
statementNode.detach();
} else {
@@ -343,8 +353,6 @@ public final class Es6RewriteModules extends AbstractPostOrderCallback
transpiled = true;
}
}
- }
- }
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
