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

The following examples show how to use com.google.javascript.rhino.Node#getFirstFirstChild() . 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: TypeAnnotationPass.java    From clutz with MIT License 6 votes vote down vote up
private static ImmutableList<String> getExtendedTemplateTypeNames(JSDocInfo bestJSDocInfo) {
  ImmutableList.Builder<String> extendedTemplateTypeNames = ImmutableList.<String>builder();
  JSTypeExpression baseType = bestJSDocInfo.getBaseType();
  if (baseType != null) {
    Node root = baseType.getRoot();
    if (root != null) {
      Node firstFirstChild = root.getFirstFirstChild();
      if (firstFirstChild != null) {
        Iterable<Node> children = firstFirstChild.children();
        for (Node child : children) {
          if (child.isString()) {
            extendedTemplateTypeNames.add(child.getString());
          }
        }
      }
    }
  }
  return extendedTemplateTypeNames.build();
}
 
Example 2
Source File: StyleFixPass.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Attempts to lift class or functions declarations of the form 'var/let/const x = class/function
 * {...}' into 'class/function x {...}'
 */
private void liftClassOrFunctionDefinition(Node n) {
  Node rhs = n.getFirstFirstChild();
  Node oldName = rhs.getFirstChild();
  Node newName = n.getFirstChild();

  // Replace name node with declared name
  rhs.detach();
  newName.detach();
  nodeComments.replaceWithComment(oldName, newName);
  nodeComments.replaceWithComment(n, rhs);
}
 
Example 3
Source File: ImportBasedMapBuilder.java    From clutz with MIT License 5 votes vote down vote up
/**
 * Matches either `const foo = goog.require()` or `const foo = goog.module.get()` or `const foo =
 * goog.forwardDeclare()` or `const foo = goog.requireType` depending on if statement is in a
 * goog.module or a goog.scope.
 */
protected static boolean isImportAssignment(Node statement) {
  if (!(statement.isConst() || statement.isVar() || statement.isLet())) {
    return false;
  }

  Node rightHandSide = statement.getFirstFirstChild();

  return rightHandSide != null
      && rightHandSide.isCall()
      && (rightHandSide.getFirstChild().matchesQualifiedName("goog.require")
          || rightHandSide.getFirstChild().matchesQualifiedName("goog.module.get")
          || rightHandSide.getFirstChild().matchesQualifiedName("goog.forwardDeclare")
          || rightHandSide.getFirstChild().matchesQualifiedName("goog.requireType"));
}
 
Example 4
Source File: LegacyNamespaceReexportMapBuilder.java    From clutz with MIT License 5 votes vote down vote up
/** Matches `goog.module.declareLegacyNamespace();` */
protected boolean isDeclareLegacyNamespaceStatement(Node statement) {
  if (!statement.isExprResult()) {
    return false;
  }

  if (!statement.getFirstChild().isCall()) {
    return false;
  }

  Node callBody = statement.getFirstFirstChild();

  return callBody.matchesQualifiedName("goog.module.declareLegacyNamespace");
}
 
Example 5
Source File: TypeConversionPass.java    From clutz with MIT License 4 votes vote down vote up
/**
 * Attempts to convert a ES5 superclass call into a ES6 super() call.
 *
 * <p>Examples:
 *
 * <pre>
 * B.call(this, args) -> super(args);
 * B.prototype.foo.call(this, args) ->super.foo(args);
 * A.base(this, 'constructor', args) -> super(args);
 * A.base(this, 'foo', args) -> super.foo(args);
 * </pre>
 *
 * <p>This returns without any modification if the node is not an superclass call statement.
 */
private void maybeReplaceSuperCall(Node callNode) {
  Preconditions.checkState(callNode.isCall());
  String callName = callNode.getFirstChild().getQualifiedName();

  // First validate that we are inside a constructor call that extends another class
  Node classNode = NodeUtil.getEnclosingClass(callNode);
  if (callName == null || classNode == null) {
    return;
  }

  String className = NodeUtil.getName(classNode);

  // Translate super constructor or super method calls as follows:
  // A.base(this, 'constructor', args) -> super(args);
  // A.base(this, 'foo', args) -> super.foo(args);
  if (callName.equals(className + ".base") && callNode.getSecondChild().isThis()) {
    // Super calls for root classes are not converted
    if (classNode.getSecondChild().isEmpty()) {
      compiler.report(
          JSError.make(
              callNode,
              GentsErrorManager.GENTS_CLASS_PASS_ERROR,
              String.format("Cannot call superclass in root class %s", className)));
      return;
    }
    String methodName = callNode.getChildAtIndex(2).getString();

    if ("constructor".equals(methodName)) {
      nodeComments.replaceWithComment(callNode.getFirstChild(), IR.superNode());
    } else {
      nodeComments.replaceWithComment(
          callNode.getFirstChild(), NodeUtil.newQName(compiler, "super." + methodName));
    }

    // Remove twice to get rid of "this" and the method name
    callNode.removeChild(callNode.getSecondChild());
    callNode.removeChild(callNode.getSecondChild());
    compiler.reportChangeToEnclosingScope(callNode);
    return;
  }

  String superClassName = classNode.getSecondChild().getQualifiedName();
  // B.call(this, args) -> super(args);
  if (callName.equals(superClassName + ".call") && callNode.getSecondChild().isThis()) {
    nodeComments.replaceWithComment(callNode.getFirstChild(), IR.superNode());

    callNode.removeChild(callNode.getSecondChild());
    compiler.reportChangeToEnclosingScope(callNode);
    return;
  }

  // B.prototype.foo.call(this, args) -> super.foo(args);
  if (callName.startsWith(superClassName + ".prototype.") && callName.endsWith(".call")) {
    if (callNode.getSecondChild().isThis()) {
      // Determine name of method being called
      Node nameNode = callNode.getFirstFirstChild();
      Node n = nameNode;
      while (!n.getLastChild().getString().equals("prototype")) {
        n = n.getFirstChild();
      }
      nameNode.detach();

      nodeComments.replaceWithComment(n, IR.superNode());
      nodeComments.replaceWithComment(callNode.getFirstChild(), nameNode);
      callNode.removeChild(callNode.getSecondChild());
      compiler.reportChangeToEnclosingScope(callNode);
      return;
    }
  }
}
 
Example 6
Source File: TypeConversionPass.java    From clutz with MIT License 4 votes vote down vote up
/** Returns the full name of the class member being declared. */
static Node getFullName(Node n) {
  return n.getFirstChild().isAssign() ? n.getFirstFirstChild() : n.getFirstChild();
}