/** * Some nodes (such as arrow functions) must be visited pre-order in order to rewrite the * references to {@code this} correctly. Everything else is translated post-order in {@link * #visit}. */ @Override public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { switch (n.getType()) { case Token.FUNCTION: if (n.isArrowFunction()) { visitArrowFunction(t, n); } break; case Token.CLASS: // Need to check for super references before they get rewritten. checkClassSuperReferences(n); break; case Token.PARAM_LIST: visitParamList(n, parent); break; } return true; }
private void validateFunctionExpression(Node n) { validateNodeType(Token.FUNCTION, n); validateChildCount(n); validateParameters(n.getChildAtIndex(1)); if (n.isArrowFunction()) { validateEs6Feature("arrow functions", n); validateEmptyName(n.getFirstChild()); if (n.getLastChild().getType() == Token.BLOCK) { validateBlock(n.getLastChild()); } else { validateExpression(n.getLastChild()); } } else { validateOptionalName(n.getFirstChild()); validateBlock(n.getLastChild()); } }
/** * Arrow functions must be visited pre-order in order to rewrite the references to {@code this} * correctly. Everything else is translated post-order in {@link #visit}. */ @Override public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { switch (n.getType()) { case Token.FUNCTION: if (n.isArrowFunction()) { visitArrowFunction(t, n); } break; case Token.ARRAY_COMP: case Token.ARRAY_PATTERN: case Token.FOR_OF: case Token.OBJECT_PATTERN: case Token.SUPER: cannotConvertYet(n, Token.name(n.getType())); // Don't bother visiting the children of a node if we // already know we can't convert the node itself. return false; } return true; }
private static boolean isDeclarationHelper(Node node) { Node parent = node.getParent(); // Special case for class B extends A, A is not a declaration. if (parent.isClass() && node != parent.getFirstChild()) { return false; } // This condition can be true during InlineVariables. if (parent.getParent() == null) { return false; } if (NodeUtil.isNameDeclaration(parent.getParent()) && node == parent.getLastChild()) { // Unless it is something like "for (var/let/const a of x){}", // this is the RHS of a var/let/const and thus not a declaration. if (parent.getParent().getParent() == null || !parent.getParent().getParent().isForOf()) { return false; } } // Special cases for destructuring patterns. if (parent.isDestructuringPattern() || (parent.isStringKey() && parent.getParent().isObjectPattern()) || (parent.isComputedProp() && parent.getParent().isObjectPattern() && node == parent.getLastChild()) || (parent.isDefaultValue() && node == parent.getFirstChild())) { return isDeclarationHelper(parent); } // Special case for arrow function if (parent.isArrowFunction()) { return node == parent.getFirstChild(); } return DECLARATION_PARENTS.contains(parent.getType()); }
@Override public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { return !n.isFunction() || n.isArrowFunction(); }