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

The following examples show how to use com.google.javascript.rhino.Node#isMemberFunctionDef() . 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 5 votes vote down vote up
/** Converts goog.defineClass calls into class definitions. */
private void convertDefineClassToClass(Node n) {
  Preconditions.checkState(n.isCall());
  Node superClass = n.getSecondChild();
  if (superClass.isNull()) {
    superClass = IR.empty();
  } else {
    superClass.detach();
  }

  Node classMembers = new Node(Token.CLASS_MEMBERS);
  classMembers.useSourceInfoFrom(n);
  for (Node child : n.getLastChild().children()) {
    if (child.isStringKey() || child.isMemberFunctionDef()) {
      // Handle static methods
      if ("statics".equals(child.getString())) {
        for (Node child2 : child.getFirstChild().children()) {
          convertObjectLiteral(classMembers, child2, true);
        }
      } else { // prototype methods
        convertObjectLiteral(classMembers, child, false);
      }
    } else {
      // Add all other members, such as EMPTY comment nodes, as is.
      child.detach();
      classMembers.addChildToBack(child);
    }
  }
  Node classNode = new Node(Token.CLASS, IR.empty(), superClass, classMembers);
  classNode.useSourceInfoFrom(n);

  nodeComments.replaceWithComment(n, classNode);
}
 
Example 2
Source File: TypeConversionPass.java    From clutz with MIT License 5 votes vote down vote up
/** Adds a field node before the first method node in classMembers */
private void addFieldToClassMembers(Node classMembers, Node field) {
  for (Node n : classMembers.children()) {
    if (n.isMemberFunctionDef()) {
      classMembers.addChildBefore(field, n);
      return;
    }
  }
  classMembers.addChildToBack(field);
}
 
Example 3
Source File: DeclarationGenerator.java    From clutz with MIT License 4 votes vote down vote up
private void visitFunctionParameters(
    FunctionType ftype, boolean emitTemplatizedTypes, List<String> alreadyEmittedTemplateType) {
  final boolean shouldEmitThis = shouldEmitThisParam(ftype);
  if (!shouldEmitThis) {
    // alreadyEmittedTemplateType might be an immutable list.
    alreadyEmittedTemplateType = new ArrayList<>(alreadyEmittedTemplateType);
    alreadyEmittedTemplateType.add(ftype.getTypeOfThis().getDisplayName());
  }
  if (emitTemplatizedTypes) {
    visitTemplateTypes(ftype, alreadyEmittedTemplateType, true);
  }
  // When all the parameters to the function are unknown, it might be the
  // case that the function is overriding/implementing a superclass or interface that is in an
  // unseen input.
  // If so, we can't know whether the parameters are optional or not, so mark them optional.
  // This is too broad (we also affect callback types) but we can fix that if it's a problem.
  boolean makeAllParametersOptional = allParametersUnknown(ftype);
  emit("(");
  Iterator<Parameter> parameters = ftype.getParameters().iterator();
  if (shouldEmitThis) {
    emitThisParameter(ftype, parameters);
  }
  Iterator<String> names = null;
  Node functionSource = ftype.getSource();
  if (functionSource != null && functionSource.isClass()) {
    Node members = functionSource.getLastChild();
    for (Node member : members.children()) {
      if (member.isMemberFunctionDef() && member.getString().equals("constructor")) {
        functionSource = member.getOnlyChild();
        break;
      }
    }
  }
  if (functionSource != null && functionSource.isFunction()) {
    // functionSource AST: FUNCTION -> (NAME, PARAM_LIST, BLOCK ...)
    Iterable<Node> parameterNodes = functionSource.getFirstChild().getNext().children();
    // TODO(bradfordcsmith): This will need to be updated when transpilation of default
    //   and destructured parameters stops happening in checks-only compilation.
    names = transform(parameterNodes, (node) -> this.getParameterName(node)).iterator();
  }

  int paramCount = 0;
  while (parameters.hasNext()) {
    Parameter param = parameters.next();
    if (param.isVariadic()) {
      emit("...");
    }

    String pName = "";
    if (names != null && names.hasNext()) {
      pName = names.next();
    }
    if (pName.isEmpty()) {
      // If we cannot find a name for the parameter, just generate one.
      if (paramCount < 26) {
        pName = Character.toString((char) (97 + paramCount));
      } else {
        pName = "p" + (paramCount - 26);
      }
    }
    emitNoSpace(pName);
    paramCount++;

    // In TypeScript ...a?: any[] is illegal, so we can only make non-varargs optional.
    if ((param.isOptional() || makeAllParametersOptional) && !param.isVariadic()) {
      emit("?");
      visitTypeDeclaration(param.getJSType(), param.isVariadic(), true);
    } else {
      visitTypeDeclaration(param.getJSType(), param.isVariadic(), false);
    }
    if (parameters.hasNext()) {
      emit(", ");
    }
  }
  emit(")");
}