Java Code Examples for com.google.javascript.jscomp.NodeUtil#getName()

The following examples show how to use com.google.javascript.jscomp.NodeUtil#getName() . 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
/**
 * Adds a class node to the top level scope.
 *
 * <p>This determines the classname using the nearest available name node.
 */
private void addClassToScope(Node n) {
  Preconditions.checkState(n.isClass());
  String className = NodeUtil.getName(n);
  if (className == null) {
    // We do not emit an error here as there can be anonymous classes without names.
    return;
  }
  addTypeToScope(n, className);
}
 
Example 2
Source File: TypeConversionPass.java    From clutz with MIT License 5 votes vote down vote up
private String getEnclosingFunctionName(Node fnNode) {
  if (fnNode.isArrowFunction()) {
    return null;
  }

  // Use the QualifiedName if the function is on an object/namespace: `foo.moreFoo()`;
  // otherwise, use the string on the node: `foo` for `function foo()`
  Node fnParent = fnNode.getParent();
  if (fnParent.isGetProp() || fnParent.isCall()) {
    return NodeUtil.getName(fnNode);
  }

  /*
   * For the specific case below, when fnNode is the anonymous function then fnParent
   * is an ASSIGN node and getString() is an invalid operation on an ASSIGN node.
   *
   * Thus, in this case, there isn't an enclosing function name and so null should be
   * returned.
   *
   * class A {
   *   constructor() {
   *     this.x = function() {
   *       this.y;
   *     }
   *   }
   * }
   */
  if (fnParent.isAssign()) {
    return null;
  }

  return fnParent.getString();
}
 
Example 3
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;
    }
  }
}