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

The following examples show how to use com.google.javascript.rhino.Node#children() . 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: Cardumen_0020_s.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 2
Source File: Nopol2017_0027_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares all of a function's arguments.
 */
private void declareArguments(Node functionNode) {
  Node astParameters = functionNode.getFirstChild().getNext();
  Node body = astParameters.getNext();
  FunctionType functionType =
      JSType.toMaybeFunctionType(functionNode.getJSType());
  if (functionType != null) {
    Node jsDocParameters = functionType.getParametersNode();
    if (jsDocParameters != null) {
      Node jsDocParameter = jsDocParameters.getFirstChild();
      for (Node astParameter : astParameters.children()) {
        if (jsDocParameter != null) {
          defineSlot(astParameter, functionNode,
              jsDocParameter.getJSType(), false);
          jsDocParameter = jsDocParameter.getNext();
        } else {
          defineSlot(astParameter, functionNode, null, true);
        }
      }
    }
  }
}
 
Example 3
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares all of a function's arguments.
 */
private void declareArguments(Node functionNode) {
  Node astParameters = functionNode.getFirstChild().getNext();
  Node body = astParameters.getNext();
  FunctionType functionType =
      JSType.toMaybeFunctionType(functionNode.getJSType());
  if (functionType != null) {
    Node jsDocParameters = functionType.getParametersNode();
    if (jsDocParameters != null) {
      Node jsDocParameter = jsDocParameters.getFirstChild();
      for (Node astParameter : astParameters.children()) {
        if (jsDocParameter != null) {
          defineSlot(astParameter, functionNode,
              jsDocParameter.getJSType(), false);
          jsDocParameter = jsDocParameter.getNext();
        } else {
          defineSlot(astParameter, functionNode, null, true);
        }
      }
    }
  }
}
 
Example 4
Source File: Cardumen_00151_s.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 5
Source File: DevirtualizePrototypeMethods.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Replaces references to "this" with references to name.  Do not
 * traverse function boundaries.
 */
private void replaceReferencesToThis(Node node, String name) {
  if (node.isFunction()) {
    return;
  }

  for (Node child : node.children()) {
    if (child.isThis()) {
      Node newName = IR.name(name);
      newName.setJSType(child.getJSType());
      node.replaceChild(child, newName);
    } else {
      replaceReferencesToThis(child, name);
    }
  }
}
 
Example 6
Source File: Closure_40_NameAnalyzer_t.java    From coming with MIT License 6 votes vote down vote up
private void addSimplifiedExpression(Node n, Node parent) {
  if (parent.isVar()) {
    Node value = n.getFirstChild();
    if (value != null) {
      addSimplifiedChildren(value);
    }
  } else if (n.isAssign() &&
      (parent.isExprResult() ||
       parent.isFor() ||
       parent.isReturn())) {
    for (Node child : n.children()) {
      addSimplifiedChildren(child);
    }
  } else if (n.isCall() &&
             parent.isExprResult()) {
    addSimplifiedChildren(n);
  } else {
    addAllChildren(n);
  }
}
 
Example 7
Source File: FunctionToBlockMutatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private static Node findFunction(Node n, String name) {
  if (n.isFunction()) {
    if (n.getFirstChild().getString().equals(name)) {
      return n;
    }
  }

  for (Node c : n.children()) {
    Node result = findFunction(c, name);
    if (result != null) {
      return result;
    }
  }

  return null;
}
 
Example 8
Source File: Closure_54_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Declares all of a function's arguments.
 */
private void declareArguments(Node functionNode) {
  Node astParameters = functionNode.getFirstChild().getNext();
  Node body = astParameters.getNext();
  FunctionType functionType =
      JSType.toMaybeFunctionType(functionNode.getJSType());
  if (functionType != null) {
    Node jsDocParameters = functionType.getParametersNode();
    if (jsDocParameters != null) {
      Node jsDocParameter = jsDocParameters.getFirstChild();
      for (Node astParameter : astParameters.children()) {
        if (jsDocParameter != null) {
          defineSlot(astParameter, functionNode,
              jsDocParameter.getJSType(), false);
          jsDocParameter = jsDocParameter.getNext();
        } else {
          defineSlot(astParameter, functionNode, null, true);
        }
      }
    }
  }
}
 
Example 9
Source File: FunctionArgumentInjectorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static Node findCall(Node n, String name) {
  if (n.isCall()) {
    Node callee;
    if (NodeUtil.isGet(n.getFirstChild())) {
      callee = n.getFirstChild().getFirstChild();
      Node prop = callee.getNext();
      // Only "call" is support at this point.
      Preconditions.checkArgument(prop.isString() &&
          prop.getString().equals("call"));
    } else {
      callee = n.getFirstChild();
    }

    if (callee.isName()
        && callee.getString().equals(name)) {
      return n;
    }
  }

  for (Node c : n.children()) {
    Node result = findCall(c, name);
    if (result != null) {
      return result;
    }
  }

  return null;
}
 
Example 10
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a VAR node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitVar(NodeTraversal t, Node n) {
  // TODO(nicksantos): Fix this so that the doc info always shows up
  // on the NAME node. We probably want to wait for the parser
  // merge to fix this.
  JSDocInfo varInfo = n.hasOneChild() ? n.getJSDocInfo() : null;
  for (Node name : n.children()) {
    Node value = name.getFirstChild();
    // A null var would indicate a bug in the scope creation logic.
    Var var = t.getScope().getVar(name.getString());

    if (value != null) {
      JSType valueType = getJSType(value);
      JSType nameType = var.getType();
      nameType = (nameType == null) ? getNativeType(UNKNOWN_TYPE) : nameType;

      JSDocInfo info = name.getJSDocInfo();
      if (info == null) {
        info = varInfo;
      }
      if (info != null && info.hasEnumParameterType()) {
        // var.getType() can never be null, this would indicate a bug in the
        // scope creation logic.
        checkEnumInitializer(
            t, value,
            info.getEnumParameterType().evaluate(t.getScope(), typeRegistry));
      } else if (var.isTypeInferred()) {
        ensureTyped(t, name, valueType);
      } else {
        validator.expectCanAssignTo(
            t, value, valueType, nameType, "initializing variable");
      }
    }
  }
}
 
Example 11
Source File: LegacyNamespaceReexportMapBuilder.java    From clutz with MIT License 5 votes vote down vote up
private boolean isLegacyNamespaceModule(Node moduleBody) {
  for (Node statement : moduleBody.children()) {
    if (isDeclareLegacyNamespaceStatement(statement)) {
      return true;
    }
  }
  return false;
}
 
Example 12
Source File: Closure_54_FunctionType_s.java    From coming with MIT License 5 votes vote down vote up
public Iterable<Node> getParameters() {
  Node n = getParametersNode();
  if (n != null) {
    return n.children();
  } else {
    return Collections.emptySet();
  }
}
 
Example 13
Source File: Closure_54_FunctionType_t.java    From coming with MIT License 5 votes vote down vote up
public Iterable<Node> getParameters() {
  Node n = getParametersNode();
  if (n != null) {
    return n.children();
  } else {
    return Collections.emptySet();
  }
}
 
Example 14
Source File: ImportBasedMapBuilder.java    From clutz with MIT License 5 votes vote down vote up
protected static List<Node> getTopLevelGoogScopes(Node astRoot) {
  List<Node> googScopes = new ArrayList<>();
  for (Node statement : astRoot.children()) {
    if (isGoogScopeCall(statement)) {
      googScopes.add(statement.getFirstChild().getSecondChild().getChildAtIndex(2));
    }
  }
  return googScopes;
}
 
Example 15
Source File: ClosureRewriteClass.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private List<MemberDefinition> objectLitToList(
    Node objlit) {
  List<MemberDefinition> result = Lists.newArrayList();
  for (Node keyNode : objlit.children()) {
    result.add(
        new MemberDefinition(
              NodeUtil.getBestJSDocInfo(keyNode),
              keyNode,
              keyNode.removeFirstChild()));
  }
  objlit.detachChildren();
  return result;
}
 
Example 16
Source File: ReplaceMessagesForChrome.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Node getPlaceholderValue(
    Node placeholderValues, String placeholderName) {
  for (Node key : placeholderValues.children()) {
    if (key.getString().equals(placeholderName)) {
      return key.getFirstChild().cloneTree();
    }
  }
  return null;
}
 
Example 17
Source File: MinerrPass.java    From ng-closure-runner with MIT License 4 votes vote down vote up
private void unmarkInstancesBelow(Node ast) {
  minerrInstances.remove(ast);
  for (Node child : ast.children()) {
    unmarkInstancesBelow(child);
  }
}
 
Example 18
Source File: TypeInference.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Infers all of a function's arguments if their types aren't declared.
 */
private void inferArguments(Scope functionScope) {
  Node functionNode = functionScope.getRootNode();
  Node astParameters = functionNode.getFirstChild().getNext();
  Node iifeArgumentNode = null;

  if (NodeUtil.isCallOrNewTarget(functionNode)) {
    iifeArgumentNode = functionNode.getNext();
  }

  FunctionType functionType =
      JSType.toMaybeFunctionType(functionNode.getJSType());
  if (functionType != null) {
    Node parameterTypes = functionType.getParametersNode();
    if (parameterTypes != null) {
      Node parameterTypeNode = parameterTypes.getFirstChild();
      for (Node astParameter : astParameters.children()) {
        Var var = functionScope.getVar(astParameter.getString());
        Preconditions.checkNotNull(var);
        if (var.isTypeInferred() &&
            var.getType() == unknownType) {
          JSType newType = null;

          if (iifeArgumentNode != null) {
            newType = iifeArgumentNode.getJSType();
          } else if (parameterTypeNode != null) {
            newType = parameterTypeNode.getJSType();
          }

          if (newType != null) {
            var.setType(newType);
            astParameter.setJSType(newType);
          }
        }

        if (parameterTypeNode != null) {
          parameterTypeNode = parameterTypeNode.getNext();
        }
        if (iifeArgumentNode != null) {
          iifeArgumentNode = iifeArgumentNode.getNext();
        }
      }
    }
  }
}
 
Example 19
Source File: ExpressionDecomposer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param expr The expression to extract.
 * @param injectionPoint The node before which to added the extracted
 *     expression.
 * @return The extract statement node.
 */
private Node extractExpression(Node expr, Node injectionPoint) {
  Node parent = expr.getParent();

  boolean isLhsOfAssignOp = NodeUtil.isAssignmentOp(parent)
      && !parent.isAssign()
      && parent.getFirstChild() == expr;

  Node firstExtractedNode = null;

  // Expressions on the LHS of an assignment-op must have any possible
  // side-effects extracted as the value must be duplicated:
  //    next().foo += 2;
  // becomes:
  //    var t1 = next();
  //    t1.foo = t1.foo + 2;
  if (isLhsOfAssignOp && NodeUtil.isGet(expr)) {
    for (Node n : expr.children()) {
      if (!n.isString() && !isConstantName(n, knownConstants)) {
        Node extractedNode = extractExpression(n, injectionPoint);
        if (firstExtractedNode == null) {
          firstExtractedNode = extractedNode;
        }
      }
    }
  }

  // The temp is known to be constant.
  String tempName = getTempConstantValueName();
  Node replacementValueNode = IR.name(tempName).srcref(expr);

  Node tempNameValue;

  // If it is ASSIGN_XXX, keep the assignment in place and extract the
  // original value of the LHS operand.
  if (isLhsOfAssignOp) {
    Preconditions.checkState(expr.isName() || NodeUtil.isGet(expr));
    // Transform "x += 2" into "x = temp + 2"
    Node opNode = new Node(NodeUtil.getOpFromAssignmentOp(parent))
        .copyInformationFrom(parent);

    Node rightOperand = parent.getLastChild();

    parent.setType(Token.ASSIGN);
    parent.replaceChild(rightOperand, opNode);
    opNode.addChildToFront(replacementValueNode);
    opNode.addChildToBack(rightOperand);

    // The original expression is still being used, so make a clone.
    tempNameValue = expr.cloneTree();
  } else {
    // Replace the expression with the temporary name.
    parent.replaceChild(expr, replacementValueNode);

    // Keep the original node so that CALL expressions can still be found
    // and inlined properly.
    tempNameValue = expr;
  }

  // Re-add the expression in the declaration of the temporary name.
  Node tempVarNode = NodeUtil.newVarNode(tempName, tempNameValue);

  Node injectionPointParent = injectionPoint.getParent();
  injectionPointParent.addChildBefore(tempVarNode, injectionPoint);

  if (firstExtractedNode == null) {
    firstExtractedNode = tempVarNode;
  }
  return firstExtractedNode;
}
 
Example 20
Source File: Closure_90_FunctionTypeBuilder_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Infer the parameter types from the list of argument names and
 * the doc info.
 */
FunctionTypeBuilder inferParameterTypes(@Nullable Node argsParent,
    @Nullable JSDocInfo info) {
  if (argsParent == null) {
    if (info == null) {
      return this;
    } else {
      return inferParameterTypes(info);
    }
  }

  // arguments
  Node oldParameterType = null;
  if (parametersNode != null) {
    oldParameterType = parametersNode.getFirstChild();
  }

  FunctionParamBuilder builder = new FunctionParamBuilder(typeRegistry);
  boolean warnedAboutArgList = false;
  Set<String> allJsDocParams = (info == null) ?
      Sets.<String>newHashSet() :
      Sets.newHashSet(info.getParameterNames());
  boolean foundTemplateType = false;
  for (Node arg : argsParent.children()) {
    String argumentName = arg.getString();
    allJsDocParams.remove(argumentName);

    // type from JSDocInfo
    JSType parameterType = null;
    boolean isOptionalParam = isOptionalParameter(arg, info);
    boolean isVarArgs = isVarArgsParameter(arg, info);
    if (info != null && info.hasParameterType(argumentName)) {
      parameterType =
          info.getParameterType(argumentName).evaluate(scope, typeRegistry);
    } else if (oldParameterType != null &&
        oldParameterType.getJSType() != null) {
      parameterType = oldParameterType.getJSType();
      isOptionalParam = oldParameterType.isOptionalArg();
      isVarArgs = oldParameterType.isVarArgs();
    } else {
      parameterType = typeRegistry.getNativeType(UNKNOWN_TYPE);
    }

    if (templateTypeName != null &&
        parameterType.restrictByNotNullOrUndefined().isTemplateType()) {
      if (foundTemplateType) {
        reportError(TEMPLATE_TYPE_DUPLICATED, fnName);
      }
      foundTemplateType = true;
    }
    warnedAboutArgList |= addParameter(
        builder, parameterType, warnedAboutArgList,
        isOptionalParam,
        isVarArgs);

    if (oldParameterType != null) {
      oldParameterType = oldParameterType.getNext();
    }
  }

  if (templateTypeName != null && !foundTemplateType) {
    reportError(TEMPLATE_TYPE_EXPECTED, fnName);
  }

  for (String inexistentName : allJsDocParams) {
    reportWarning(INEXISTANT_PARAM, inexistentName, fnName);
  }

  parametersNode = builder.build();
  return this;
}