Java Code Examples for com.google.javascript.rhino.Node#getToken()

The following examples show how to use com.google.javascript.rhino.Node#getToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeConversionPass.java    From clutz with MIT License 6 votes vote down vote up
private void createTypeAlias(Node n, Node parent) {
  JSDocInfo bestJSDocInfo = NodeUtil.getBestJSDocInfo(n);
  if (bestJSDocInfo != null && bestJSDocInfo.hasTypedefType()) {
    String name;
    switch (n.getToken()) {
      case NAME:
        name = n.getString();
        break;
      case GETPROP:
        // Inner typedef
        name = n.getSecondChild().getString();
        break;
      default:
        name = n.getFirstChild().getString();
        break;
    }
    Node typeDef = Node.newString(Token.TYPE_ALIAS, name);
    nodeComments.moveComment(n, typeDef);
    types.put(name, typeDef);
    typeDef.setJSDocInfo(bestJSDocInfo);
    replaceExpressionOrAssignment(n, parent, typeDef);
  }
}
 
Example 2
Source File: TypeConversionPass.java    From clutz with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getToken()) {
    case CLASS:
      addClassToScope(n);
      break;
    case EXPR_RESULT:
      ClassMemberDeclaration declaration = ClassMemberDeclaration.newDeclaration(n, types);
      if (declaration == null) {
        break;
      }
      if (declaration.rhs != null && declaration.rhs.isFunction()) {
        moveMethodsIntoClasses(declaration);
      } else {
        moveFieldsIntoClasses(declaration);
      }
      break;
    default:
      break;
  }
}
 
Example 3
Source File: TypeConversionPass.java    From clutz with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getToken()) {
    case VAR:
    case LET:
    case CONST:
      if (n.getJSDocInfo() == null || n.getJSDocInfo().getEnumParameterType() == null) return;
      JSTypeExpression enumExp = n.getJSDocInfo().getEnumParameterType();
      if (!enumExp.getRoot().isString()) return;
      String enumTypeStr = enumExp.getRoot().getString();
      if (!enumTypeStr.equals("number") && !enumTypeStr.equals("string")) return;

      Node name = n.removeFirstChild();
      Node members = name.removeFirstChild();

      Node enumMembers = transformMembers(members, enumTypeStr.equals("number"));
      Node enumNode = new Node(Token.ENUM, name, enumMembers);
      parent.replaceChild(n, enumNode);
      compiler.reportChangeToEnclosingScope(parent);
      break;
    default:
      break;
  }
}
 
Example 4
Source File: TypeConversionPass.java    From clutz with MIT License 6 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getToken()) {
    case CLASS:
    case MEMBER_FUNCTION_DEF:
      JSDocInfo jsDoc = n.getJSDocInfo();
      if (jsDoc != null && jsDoc.isAbstract()) {
        // This is a hack, we should be using a property like Node.IS_ABSTRACT
        // but it doesn't exist. So I just picked a random boolean property
        // that is not likely to be used on a class or member-function-def.
        // This is read in the GentsCodeGenerator.
        n.putBooleanProp(Node.WAS_PREVIOUSLY_PROVIDED, true);
      }
      break;
    default:
      break;
  }
}
 
Example 5
Source File: TypeConversionPass.java    From clutz with MIT License 5 votes vote down vote up
private boolean containsObject(Node typedefNode) {
  Token typedefToken = typedefNode.getToken();
  if (typedefToken == Token.LC) {
    return true;
  }

  if ((typedefToken == Token.QMARK || typedefToken == Token.BANG)
      && typedefNode.hasOneChild()) {
    // child Node is a simple type or a LC
    return containsObject(typedefNode.getFirstChild());
  }

  return false;
}
 
Example 6
Source File: TypeConversionPass.java    From clutz with MIT License 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getToken()) {
    case EXPR_RESULT:
      maybeRemoveInherits(n);
      break;
    case CALL:
      maybeReplaceSuperCall(n);
      break;
    default:
      break;
  }
}
 
Example 7
Source File: TypeAnnotationPass.java    From clutz with MIT License 5 votes vote down vote up
/** Returns whether a union type contains `undefined`. */
private static boolean unionContainsUndefined(TypeDeclarationNode union) {
  for (Node unionChild : union.children()) {
    if (unionChild.getToken() == Token.UNDEFINED_TYPE) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: TypeAnnotationPass.java    From clutz with MIT License 5 votes vote down vote up
/** Converts a union type to one without `undefined`, if it is present, detaching the original union. */
private static TypeDeclarationNode getUnionTypeNoUndefined(TypeDeclarationNode union) {
  List<TypeDeclarationNode> nonUndefinedChildren = new ArrayList<>();
  for (Node unionChild : union.children()) {
    if (unionChild.getToken() != Token.UNDEFINED_TYPE) {
      nonUndefinedChildren.add((TypeDeclarationNode) unionChild);
    }
  }
  union.detachChildren();
  return unionType(nonUndefinedChildren);
}
 
Example 9
Source File: GentsCodeGenerator.java    From clutz with MIT License 5 votes vote down vote up
@Override
protected void add(Node n, Context ctx) {
  @Nullable Node parent = n.getParent();
  maybeAddNewline(n);

  addNewComments(astComments.getComments(n), nodeComments.getComments(n));

  if (maybeOverrideCodeGen(n)) {
    return;
  }
  super.add(n, ctx);

  // Default field values
  switch (n.getToken()) {
    case NEW:
      // The Closure Compiler code generator drops off the extra () for new statements.
      // We add them back in to maintain a consistent style.
      if (n.hasOneChild()) {
        add("()");
      }
      break;
    case FUNCTION_TYPE:
      // Match the "(" in maybeOverrideCodeGen for FUNCTION_TYPE nodes.
      if (parent != null && parent.getToken() == Token.UNION_TYPE) {
        add(")");
      }
      break;
    default:
      break;
  }
}
 
Example 10
Source File: StyleFixPass.java    From clutz with MIT License 4 votes vote down vote up
private boolean isTypeDefinition(Node rhs) {
  return rhs.isClass() || rhs.getToken() == Token.INTERFACE;
}