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

The following examples show how to use com.google.javascript.rhino.Node#getSecondChild() . 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 stripFunctionDefaultParamsAndBody(Node member) {
  Node functionNode = member.getFirstChild();
  Node functionName = functionNode.getFirstChild();
  Node functionParams = functionNode.getSecondChild();
  // Remove defaults from parameters
  Node functionParamsNoDefaults = new Node(Token.PARAM_LIST);
  for (Node param : functionParams.children()) {
    Node paramNoDefault = param.isDefaultValue() ? param.getFirstChild() : param;
    functionParamsNoDefaults.addChildToBack(paramNoDefault.detach());
  }
  // Strip body from function definitions.
  Node newFunction =
      new Node(
          Token.FUNCTION,
          functionName.detach(),
          functionParamsNoDefaults,
          new Node(Token.EMPTY));
  newFunction.useSourceInfoFrom(functionNode);
  nodeComments.replaceWithComment(functionNode, newFunction);
}
 
Example 2
Source File: ImportBasedMapBuilder.java    From clutz with MIT License 6 votes vote down vote up
/** Matches destructing from a variable ie `const {foo, bar: baz} = quux;` */
protected static boolean isVariableDestructuringAssignment(Node statement) {
  if (!(statement.isConst() || statement.isVar() || statement.isLet())) {
    return false;
  }

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

  Node destructuringAssignment = statement.getFirstChild();

  Node rightHandSide = destructuringAssignment.getSecondChild();

  return rightHandSide.isName();
}
 
Example 3
Source File: ImportBasedMapBuilder.java    From clutz with MIT License 6 votes vote down vote up
/**
 * Matches either `const {foo} = goog.require()` or `const {foo} = goog.module.get()` depending on
 * if statement is in a goog.module or a goog.scope.
 */
protected static boolean isImportDestructuringAssignment(Node statement) {
  if (!(statement.isConst() || statement.isVar() || statement.isLet())) {
    return false;
  }

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

  Node destructuringAssignment = statement.getFirstChild();

  Node rightHandSide = destructuringAssignment.getSecondChild();

  return rightHandSide.isCall()
      && (rightHandSide.getFirstChild().matchesQualifiedName("goog.require")
          || rightHandSide.getFirstChild().matchesQualifiedName("goog.module.get"));
}
 
Example 4
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 5
Source File: LinkCommentsForOneFile.java    From clutz with MIT License 5 votes vote down vote up
private boolean canHaveComment(Node n) {
  if (TOKENS_IGNORE_COMMENTS.contains(n.getToken())) {
    return false;
  }

  // Special case: GETPROP node's second child(the property name) loses comments.
  Node parent = n.getParent();
  if (parent != null && parent.isGetProp() && parent.getSecondChild() == n) {
    return false;
  }

  return true;
}
 
Example 6
Source File: TypeInspector.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static Node getParamList(Node node) {
  verify(node.isFunction(), "not a function: %s", node);

  Node list = node.getSecondChild();
  verify(list != null && list.isParamList(), "not a param list: %s", list);

  return list;
}
 
Example 7
Source File: BuildSymbolTablePass.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private void visitImport(SymbolTable table, Node importNode) {
  checkState(
      isString(importNode.getLastChild()),
      "expect last to be a string: %s",
      importNode.getLastChild());
  Module.Id fromModuleId = es6ModuleId(importNode.getLastChild());

  if (isName(importNode.getFirstChild())) {
    table.add(
        Symbol.builder(fs, importNode)
            .setName(importNode.getFirstChild().getQualifiedName())
            .setReferencedSymbol(fromModuleId + ".default")
            .build());
  }

  if (importNode.getSecondChild() != null && importNode.getSecondChild().isImportStar()) {
    table.add(
        Symbol.builder(fs, importNode)
            .setName(importNode.getSecondChild().getString())
            .setReferencedSymbol(fromModuleId.toString())
            .build());
  }

  if (importNode.getSecondChild() != null && importNode.getSecondChild().isImportSpecs()) {
    for (Node spec = importNode.getSecondChild().getFirstChild();
        spec != null;
        spec = spec.getNext()) {
      checkState(spec.getFirstChild() != null);
      checkState(spec.getSecondChild() != null);
      table.add(
          Symbol.builder(fs, importNode)
              .setName(spec.getSecondChild().getQualifiedName())
              .setReferencedSymbol(fromModuleId + "." + spec.getFirstChild().getQualifiedName())
              .build());
    }
  }
}
 
Example 8
Source File: NodeModulePass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private static boolean isCall(Node n, String name) {
  return Nodes.isCall(n, name) && n.getSecondChild() != null && n.getSecondChild().isString();
}
 
Example 9
Source File: Nodes.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public static String getStringParam(Node n) {
  checkArgument(n != null && n.isCall(), "not a call node: %s", n);
  Node child = n.getSecondChild();
  checkArgument(isString(child), "second child is not a string: %s", child);
  return child.getString();
}
 
Example 10
Source File: Nodes.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public static boolean isGoogScopeCall(@Nullable Node n) {
  return n != null
      && isCall(n, "goog.scope")
      && n.getSecondChild() != null
      && n.getSecondChild().isFunction();
}
 
Example 11
Source File: Nodes.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
public static boolean isRequireCall(Node n) {
  return n != null
      && isCall(n, "require")
      && n.getSecondChild() != null
      && n.getSecondChild().isString();
}