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

The following examples show how to use com.google.javascript.rhino.Node#setOptionalArg() . 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: FunctionParamBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copies the parameter specification from the given node.
 */
public Node newParameterFromNode(Node n) {
  Node newParam = newParameter(n.getJSType());
  newParam.setVarArgs(n.isVarArgs());
  newParam.setOptionalArg(n.isOptionalArg());
  return newParam;
}
 
Example 2
Source File: FunctionParamBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copies the parameter specification from the given node,
 * but makes sure it's optional.
 */
public Node newOptionalParameterFromNode(Node n) {
  Node newParam = newParameterFromNode(n);
  if (!newParam.isVarArgs() && !newParam.isOptionalArg()) {
    newParam.setOptionalArg(true);
  }
  return newParam;
}
 
Example 3
Source File: FunctionType.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notice that "call" and "bind" have the same argument signature,
 * except that all the arguments of "bind" (except the first)
 * are optional.
 */
private FunctionType getCallOrBindSignature(boolean isCall) {
  boolean isBind = !isCall;
  FunctionBuilder builder = new FunctionBuilder(registry)
      .withReturnType(isCall ? getReturnType() : getBindReturnType(-1))
      .withTemplateKeys(getTemplateKeys());

  Node origParams = getParametersNode();
  if (origParams != null) {
    Node params = origParams.cloneTree();

    Node thisTypeNode = Node.newString(Token.NAME, "thisType");
    thisTypeNode.setJSType(
        registry.createOptionalNullableType(getTypeOfThis()));
    params.addChildToFront(thisTypeNode);

    if (isBind) {
      // The arguments of bind() are unique in that they are all
      // optional but not undefinable.
      for (Node current = thisTypeNode.getNext();
           current != null; current = current.getNext()) {
        current.setOptionalArg(true);
      }
    } else if (isCall) {
      // The first argument of call() is optional iff all the arguments
      // are optional. It's sufficient to check the first argument.
      Node firstArg = thisTypeNode.getNext();
      if (firstArg == null
          || firstArg.isOptionalArg()
          || firstArg.isVarArgs()) {
        thisTypeNode.setOptionalArg(true);
      }
    }

    builder.withParamsNode(params);
  }

  return builder.build();
}
 
Example 4
Source File: Closure_90_FunctionTypeBuilder_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Infer the parameter and return types of a function from
 * the parameter and return types of the function it is overriding.
 *
 * @param oldType The function being overridden. Does nothing if this is null.
 * @param paramsParent The LP node of the function that we're assigning to.
 *     If null, that just means we're not initializing this to a function
 *     literal.
 */
FunctionTypeBuilder inferFromOverriddenFunction(
    @Nullable FunctionType oldType, @Nullable Node paramsParent) {
  if (oldType == null) {
    return this;
  }

  returnType = oldType.getReturnType();
  returnTypeInferred = oldType.isReturnTypeInferred();
  if (paramsParent == null) {
    // Not a function literal.
    parametersNode = oldType.getParametersNode();
    if (parametersNode == null) {
      parametersNode = new FunctionParamBuilder(typeRegistry).build();
    }
  } else {
    // We're overriding with a function literal. Apply type information
    // to each parameter of the literal.
    FunctionParamBuilder paramBuilder =
        new FunctionParamBuilder(typeRegistry);
    Iterator<Node> oldParams = oldType.getParameters().iterator();
    boolean warnedAboutArgList = false;
    boolean oldParamsListHitOptArgs = false;
    for (Node currentParam = paramsParent.getFirstChild();
         currentParam != null; currentParam = currentParam.getNext()) {
      if (oldParams.hasNext()) {
        Node oldParam = oldParams.next();
        Node newParam = paramBuilder.newParameterFromNode(oldParam);

        oldParamsListHitOptArgs = oldParamsListHitOptArgs ||
            oldParam.isVarArgs() ||
            oldParam.isOptionalArg();

        // The subclass method might right its var_args as individual
        // arguments.
        if (currentParam.getNext() != null && newParam.isVarArgs()) {
          newParam.setVarArgs(false);
          newParam.setOptionalArg(true);
        }
      } else {
        warnedAboutArgList |= addParameter(
            paramBuilder,
            typeRegistry.getNativeType(UNKNOWN_TYPE),
            warnedAboutArgList,
            codingConvention.isOptionalParameter(currentParam) ||
                oldParamsListHitOptArgs,
            codingConvention.isVarArgsParameter(currentParam));
      }
    }
    parametersNode = paramBuilder.build();
  }
  return this;
}
 
Example 5
Source File: Closure_90_FunctionTypeBuilder_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Infer the parameter and return types of a function from
 * the parameter and return types of the function it is overriding.
 *
 * @param oldType The function being overridden. Does nothing if this is null.
 * @param paramsParent The LP node of the function that we're assigning to.
 *     If null, that just means we're not initializing this to a function
 *     literal.
 */
FunctionTypeBuilder inferFromOverriddenFunction(
    @Nullable FunctionType oldType, @Nullable Node paramsParent) {
  if (oldType == null) {
    return this;
  }

  returnType = oldType.getReturnType();
  returnTypeInferred = oldType.isReturnTypeInferred();
  if (paramsParent == null) {
    // Not a function literal.
    parametersNode = oldType.getParametersNode();
    if (parametersNode == null) {
      parametersNode = new FunctionParamBuilder(typeRegistry).build();
    }
  } else {
    // We're overriding with a function literal. Apply type information
    // to each parameter of the literal.
    FunctionParamBuilder paramBuilder =
        new FunctionParamBuilder(typeRegistry);
    Iterator<Node> oldParams = oldType.getParameters().iterator();
    boolean warnedAboutArgList = false;
    boolean oldParamsListHitOptArgs = false;
    for (Node currentParam = paramsParent.getFirstChild();
         currentParam != null; currentParam = currentParam.getNext()) {
      if (oldParams.hasNext()) {
        Node oldParam = oldParams.next();
        Node newParam = paramBuilder.newParameterFromNode(oldParam);

        oldParamsListHitOptArgs = oldParamsListHitOptArgs ||
            oldParam.isVarArgs() ||
            oldParam.isOptionalArg();

        // The subclass method might right its var_args as individual
        // arguments.
        if (currentParam.getNext() != null && newParam.isVarArgs()) {
          newParam.setVarArgs(false);
          newParam.setOptionalArg(true);
        }
      } else {
        warnedAboutArgList |= addParameter(
            paramBuilder,
            typeRegistry.getNativeType(UNKNOWN_TYPE),
            warnedAboutArgList,
            codingConvention.isOptionalParameter(currentParam) ||
                oldParamsListHitOptArgs,
            codingConvention.isVarArgsParameter(currentParam));
      }
    }
    parametersNode = paramBuilder.build();
  }
  return this;
}
 
Example 6
Source File: Closure_90_FunctionType_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public JSType getPropertyType(String name) {
  if ("prototype".equals(name)) {
    return getPrototype();
  } else {
    if (!hasOwnProperty(name)) {
      if ("call".equals(name)) {
        // Define the "call" function lazily.
        Node params = getParametersNode();
        if (params == null) {
          // If there's no params array, don't do any type-checking
          // in this CALL function.
          defineDeclaredProperty(name,
              new FunctionBuilder(registry)
                  .withReturnType(getReturnType())
                  .build(),
              false);
        } else {
          params = params.cloneTree();
          Node thisTypeNode = Node.newString(Token.NAME, "thisType");
          thisTypeNode.setJSType(
              registry.createOptionalNullableType(getTypeOfThis()));
          params.addChildToFront(thisTypeNode);
          thisTypeNode.setOptionalArg(true);

          defineDeclaredProperty(name,
              new FunctionBuilder(registry)
                  .withParamsNode(params)
                  .withReturnType(getReturnType())
                  .build(),
              false);
        }
      } else if ("apply".equals(name)) {
        // Define the "apply" function lazily.
        FunctionParamBuilder builder = new FunctionParamBuilder(registry);

        // Ecma-262 says that apply's second argument must be an Array
        // or an arguments object. We don't model the arguments object,
        // so let's just be forgiving for now.
        // TODO(nicksantos): Model the Arguments object.
        builder.addOptionalParams(
            registry.createNullableType(getTypeOfThis()),
            registry.createNullableType(
                registry.getNativeType(JSTypeNative.OBJECT_TYPE)));

        defineDeclaredProperty(name,
            new FunctionBuilder(registry)
                .withParams(builder)
                .withReturnType(getReturnType())
                .build(),
            false);
      }
    }

    return super.getPropertyType(name);
  }
}
 
Example 7
Source File: Closure_90_FunctionType_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public JSType getPropertyType(String name) {
  if ("prototype".equals(name)) {
    return getPrototype();
  } else {
    if (!hasOwnProperty(name)) {
      if ("call".equals(name)) {
        // Define the "call" function lazily.
        Node params = getParametersNode();
        if (params == null) {
          // If there's no params array, don't do any type-checking
          // in this CALL function.
          defineDeclaredProperty(name,
              new FunctionBuilder(registry)
                  .withReturnType(getReturnType())
                  .build(),
              false);
        } else {
          params = params.cloneTree();
          Node thisTypeNode = Node.newString(Token.NAME, "thisType");
          thisTypeNode.setJSType(
              registry.createOptionalNullableType(getTypeOfThis()));
          params.addChildToFront(thisTypeNode);
          thisTypeNode.setOptionalArg(true);

          defineDeclaredProperty(name,
              new FunctionBuilder(registry)
                  .withParamsNode(params)
                  .withReturnType(getReturnType())
                  .build(),
              false);
        }
      } else if ("apply".equals(name)) {
        // Define the "apply" function lazily.
        FunctionParamBuilder builder = new FunctionParamBuilder(registry);

        // Ecma-262 says that apply's second argument must be an Array
        // or an arguments object. We don't model the arguments object,
        // so let's just be forgiving for now.
        // TODO(nicksantos): Model the Arguments object.
        builder.addOptionalParams(
            registry.createNullableType(getTypeOfThis()),
            registry.createNullableType(
                registry.getNativeType(JSTypeNative.OBJECT_TYPE)));

        defineDeclaredProperty(name,
            new FunctionBuilder(registry)
                .withParams(builder)
                .withReturnType(getReturnType())
                .build(),
            false);
      }
    }

    return super.getPropertyType(name);
  }
}
 
Example 8
Source File: Closure_41_FunctionTypeBuilder_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Infer the parameter and return types of a function from
 * the parameter and return types of the function it is overriding.
 *
 * @param oldType The function being overridden. Does nothing if this is null.
 * @param paramsParent The LP node of the function that we're assigning to.
 *     If null, that just means we're not initializing this to a function
 *     literal.
 */
FunctionTypeBuilder inferFromOverriddenFunction(
    @Nullable FunctionType oldType, @Nullable Node paramsParent) {
  if (oldType == null) {
    return this;
  }

  returnType = oldType.getReturnType();
  returnTypeInferred = oldType.isReturnTypeInferred();
  if (paramsParent == null) {
    // Not a function literal.
    parametersNode = oldType.getParametersNode();
    if (parametersNode == null) {
      parametersNode = new FunctionParamBuilder(typeRegistry).build();
    }
  } else {
    // We're overriding with a function literal. Apply type information
    // to each parameter of the literal.
    FunctionParamBuilder paramBuilder =
        new FunctionParamBuilder(typeRegistry);
    Iterator<Node> oldParams = oldType.getParameters().iterator();
    boolean warnedAboutArgList = false;
    boolean oldParamsListHitOptArgs = false;
    for (Node currentParam = paramsParent.getFirstChild();
         currentParam != null; currentParam = currentParam.getNext()) {
      if (oldParams.hasNext()) {
        Node oldParam = oldParams.next();
        Node newParam = paramBuilder.newParameterFromNode(oldParam);

        oldParamsListHitOptArgs = oldParamsListHitOptArgs ||
            oldParam.isVarArgs() ||
            oldParam.isOptionalArg();

        // The subclass method might write its var_args as individual
        // arguments.
        if (currentParam.getNext() != null && newParam.isVarArgs()) {
          newParam.setVarArgs(false);
          newParam.setOptionalArg(true);
        }
      } else {
        warnedAboutArgList |= addParameter(
            paramBuilder,
            typeRegistry.getNativeType(UNKNOWN_TYPE),
            warnedAboutArgList,
            codingConvention.isOptionalParameter(currentParam) ||
                oldParamsListHitOptArgs,
            codingConvention.isVarArgsParameter(currentParam));
      }
    }

    // Clone any remaining params that aren't in the function literal.

    parametersNode = paramBuilder.build();
  }
  return this;
}
 
Example 9
Source File: Closure_41_FunctionTypeBuilder_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Infer the parameter and return types of a function from
 * the parameter and return types of the function it is overriding.
 *
 * @param oldType The function being overridden. Does nothing if this is null.
 * @param paramsParent The LP node of the function that we're assigning to.
 *     If null, that just means we're not initializing this to a function
 *     literal.
 */
FunctionTypeBuilder inferFromOverriddenFunction(
    @Nullable FunctionType oldType, @Nullable Node paramsParent) {
  if (oldType == null) {
    return this;
  }

  returnType = oldType.getReturnType();
  returnTypeInferred = oldType.isReturnTypeInferred();
  if (paramsParent == null) {
    // Not a function literal.
    parametersNode = oldType.getParametersNode();
    if (parametersNode == null) {
      parametersNode = new FunctionParamBuilder(typeRegistry).build();
    }
  } else {
    // We're overriding with a function literal. Apply type information
    // to each parameter of the literal.
    FunctionParamBuilder paramBuilder =
        new FunctionParamBuilder(typeRegistry);
    Iterator<Node> oldParams = oldType.getParameters().iterator();
    boolean warnedAboutArgList = false;
    boolean oldParamsListHitOptArgs = false;
    for (Node currentParam = paramsParent.getFirstChild();
         currentParam != null; currentParam = currentParam.getNext()) {
      if (oldParams.hasNext()) {
        Node oldParam = oldParams.next();
        Node newParam = paramBuilder.newParameterFromNode(oldParam);

        oldParamsListHitOptArgs = oldParamsListHitOptArgs ||
            oldParam.isVarArgs() ||
            oldParam.isOptionalArg();

        // The subclass method might write its var_args as individual
        // arguments.
        if (currentParam.getNext() != null && newParam.isVarArgs()) {
          newParam.setVarArgs(false);
          newParam.setOptionalArg(true);
        }
      } else {
        warnedAboutArgList |= addParameter(
            paramBuilder,
            typeRegistry.getNativeType(UNKNOWN_TYPE),
            warnedAboutArgList,
            codingConvention.isOptionalParameter(currentParam) ||
                oldParamsListHitOptArgs,
            codingConvention.isVarArgsParameter(currentParam));
      }
    }

    // Clone any remaining params that aren't in the function literal.
    while (oldParams.hasNext()) {
      paramBuilder.newParameterFromNode(oldParams.next());
    }

    parametersNode = paramBuilder.build();
  }
  return this;
}
 
Example 10
Source File: Closure_54_FunctionType_t.java    From coming with MIT License 4 votes vote down vote up
@Override
public JSType getPropertyType(String name) {
  if (!hasOwnProperty(name)) {
    if ("call".equals(name)) {
      // Define the "call" function lazily.
      Node params = getParametersNode();
      if (params == null) {
        // If there's no params array, don't do any type-checking
        // in this CALL function.
        defineDeclaredProperty(name,
            new FunctionBuilder(registry)
            .withReturnType(getReturnType())
            .build(),
            source);
      } else {
        params = params.cloneTree();
        Node thisTypeNode = Node.newString(Token.NAME, "thisType");
        thisTypeNode.setJSType(
            registry.createOptionalNullableType(getTypeOfThis()));
        params.addChildToFront(thisTypeNode);
        thisTypeNode.setOptionalArg(true);

        defineDeclaredProperty(name,
            new FunctionBuilder(registry)
            .withParamsNode(params)
            .withReturnType(getReturnType())
            .build(),
            source);
      }
    } else if ("apply".equals(name)) {
      // Define the "apply" function lazily.
      FunctionParamBuilder builder = new FunctionParamBuilder(registry);

      // Ecma-262 says that apply's second argument must be an Array
      // or an arguments object. We don't model the arguments object,
      // so let's just be forgiving for now.
      // TODO(nicksantos): Model the Arguments object.
      builder.addOptionalParams(
          registry.createNullableType(getTypeOfThis()),
          registry.createNullableType(
              registry.getNativeType(JSTypeNative.OBJECT_TYPE)));

      defineDeclaredProperty(name,
          new FunctionBuilder(registry)
          .withParams(builder)
          .withReturnType(getReturnType())
          .build(),
          source);
    }
  }

  return super.getPropertyType(name);
}
 
Example 11
Source File: Closure_54_FunctionType_s.java    From coming with MIT License 4 votes vote down vote up
@Override
public JSType getPropertyType(String name) {
  if (!hasOwnProperty(name)) {
    if ("call".equals(name)) {
      // Define the "call" function lazily.
      Node params = getParametersNode();
      if (params == null) {
        // If there's no params array, don't do any type-checking
        // in this CALL function.
        defineDeclaredProperty(name,
            new FunctionBuilder(registry)
            .withReturnType(getReturnType())
            .build(),
            source);
      } else {
        params = params.cloneTree();
        Node thisTypeNode = Node.newString(Token.NAME, "thisType");
        thisTypeNode.setJSType(
            registry.createOptionalNullableType(getTypeOfThis()));
        params.addChildToFront(thisTypeNode);
        thisTypeNode.setOptionalArg(true);

        defineDeclaredProperty(name,
            new FunctionBuilder(registry)
            .withParamsNode(params)
            .withReturnType(getReturnType())
            .build(),
            source);
      }
    } else if ("apply".equals(name)) {
      // Define the "apply" function lazily.
      FunctionParamBuilder builder = new FunctionParamBuilder(registry);

      // Ecma-262 says that apply's second argument must be an Array
      // or an arguments object. We don't model the arguments object,
      // so let's just be forgiving for now.
      // TODO(nicksantos): Model the Arguments object.
      builder.addOptionalParams(
          registry.createNullableType(getTypeOfThis()),
          registry.createNullableType(
              registry.getNativeType(JSTypeNative.OBJECT_TYPE)));

      defineDeclaredProperty(name,
          new FunctionBuilder(registry)
          .withParams(builder)
          .withReturnType(getReturnType())
          .build(),
          source);
    }
  }

  return super.getPropertyType(name);
}
 
Example 12
Source File: FunctionTypeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Infer the parameter and return types of a function from
 * the parameter and return types of the function it is overriding.
 *
 * @param oldType The function being overridden. Does nothing if this is null.
 * @param paramsParent The LP node of the function that we're assigning to.
 *     If null, that just means we're not initializing this to a function
 *     literal.
 */
FunctionTypeBuilder inferFromOverriddenFunction(
    @Nullable FunctionType oldType, @Nullable Node paramsParent) {
  if (oldType == null) {
    return this;
  }

  returnType = oldType.getReturnType();
  returnTypeInferred = oldType.isReturnTypeInferred();
  if (paramsParent == null) {
    // Not a function literal.
    parametersNode = oldType.getParametersNode();
    if (parametersNode == null) {
      parametersNode = new FunctionParamBuilder(typeRegistry).build();
    }
  } else {
    // We're overriding with a function literal. Apply type information
    // to each parameter of the literal.
    FunctionParamBuilder paramBuilder =
        new FunctionParamBuilder(typeRegistry);
    Iterator<Node> oldParams = oldType.getParameters().iterator();
    boolean warnedAboutArgList = false;
    boolean oldParamsListHitOptArgs = false;
    for (Node currentParam = paramsParent.getFirstChild();
         currentParam != null; currentParam = currentParam.getNext()) {
      if (oldParams.hasNext()) {
        Node oldParam = oldParams.next();
        Node newParam = paramBuilder.newParameterFromNode(oldParam);

        oldParamsListHitOptArgs = oldParamsListHitOptArgs ||
            oldParam.isVarArgs() ||
            oldParam.isOptionalArg();

        // The subclass method might write its var_args as individual
        // arguments.
        if (currentParam.getNext() != null && newParam.isVarArgs()) {
          newParam.setVarArgs(false);
          newParam.setOptionalArg(true);
        }
      } else {
        warnedAboutArgList |= addParameter(
            paramBuilder,
            typeRegistry.getNativeType(UNKNOWN_TYPE),
            warnedAboutArgList,
            codingConvention.isOptionalParameter(currentParam) ||
                oldParamsListHitOptArgs,
            codingConvention.isVarArgsParameter(currentParam));
      }
    }

    // Clone any remaining params that aren't in the function literal,
    // but make them optional.
    while (oldParams.hasNext()) {
      paramBuilder.newOptionalParameterFromNode(oldParams.next());
    }

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