Java Code Examples for com.google.javascript.rhino.Token#PARAM_LIST

The following examples show how to use com.google.javascript.rhino.Token#PARAM_LIST . 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: GoogleCodingConventionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testVarAndOptionalParams() {
  Node args = new Node(Token.PARAM_LIST,
      Node.newString(Token.NAME, "a"),
      Node.newString(Token.NAME, "b"));
  Node optArgs = new Node(Token.PARAM_LIST,
      Node.newString(Token.NAME, "opt_a"),
      Node.newString(Token.NAME, "opt_b"));

  assertFalse(conv.isVarArgsParameter(args.getFirstChild()));
  assertFalse(conv.isVarArgsParameter(args.getLastChild()));
  assertFalse(conv.isVarArgsParameter(optArgs.getFirstChild()));
  assertFalse(conv.isVarArgsParameter(optArgs.getLastChild()));

  assertFalse(conv.isOptionalParameter(args.getFirstChild()));
  assertFalse(conv.isOptionalParameter(args.getLastChild()));
  assertTrue(conv.isOptionalParameter(optArgs.getFirstChild()));
  assertTrue(conv.isOptionalParameter(optArgs.getLastChild()));
}
 
Example 2
Source File: ConcreteTypeTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Creates a fake function with the given description. */
private ConcreteFunctionType createFunction(
    String name, String... paramNames) {
  Node args = new Node(Token.PARAM_LIST);
  for (int i = 0; i < paramNames.length; ++i) {
    args.addChildToBack(Node.newString(Token.NAME, paramNames[i]));
  }

  Node decl = new Node(Token.FUNCTION,
                       Node.newString(Token.NAME, name),
                       args,
                       new Node(Token.BLOCK));

  JSType[] paramTypes = new JSType[paramNames.length];
  Arrays.fill(paramTypes, unknownType);
  decl.setJSType(typeRegistry.createConstructorType(
      name, decl, args, unknownType, null));

  return new ConcreteFunctionType(factory, decl, null);
}
 
Example 3
Source File: ClosureCodingConventionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testVarAndOptionalParams() {
  Node args = new Node(Token.PARAM_LIST,
      Node.newString(Token.NAME, "a"),
      Node.newString(Token.NAME, "b"));
  Node optArgs = new Node(Token.PARAM_LIST,
      Node.newString(Token.NAME, "opt_a"),
      Node.newString(Token.NAME, "opt_b"));

  assertFalse(conv.isVarArgsParameter(args.getFirstChild()));
  assertFalse(conv.isVarArgsParameter(args.getLastChild()));
  assertFalse(conv.isVarArgsParameter(optArgs.getFirstChild()));
  assertFalse(conv.isVarArgsParameter(optArgs.getLastChild()));

  assertFalse(conv.isOptionalParameter(args.getFirstChild()));
  assertFalse(conv.isOptionalParameter(args.getLastChild()));
  assertFalse(conv.isOptionalParameter(optArgs.getFirstChild()));
  assertFalse(conv.isOptionalParameter(optArgs.getLastChild()));
}
 
Example 4
Source File: DefaultCodingConventionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testVarAndOptionalParams() {
  Node args = new Node(Token.PARAM_LIST,
      Node.newString(Token.NAME, "a"),
      Node.newString(Token.NAME, "b"));
  Node optArgs = new Node(Token.PARAM_LIST,
      Node.newString(Token.NAME, "opt_a"),
      Node.newString(Token.NAME, "opt_b"));

  assertFalse(conv.isVarArgsParameter(args.getFirstChild()));
  assertTrue(conv.isVarArgsParameter(args.getLastChild()));
  assertFalse(conv.isVarArgsParameter(optArgs.getFirstChild()));
  assertTrue(conv.isVarArgsParameter(optArgs.getLastChild()));

  assertTrue(conv.isOptionalParameter(args.getFirstChild()));
  assertFalse(conv.isOptionalParameter(args.getLastChild()));
  assertTrue(conv.isOptionalParameter(optArgs.getFirstChild()));
  assertFalse(conv.isOptionalParameter(optArgs.getLastChild()));
}
 
Example 5
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 6
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 7
Source File: MakeDeclaredNamesUnique.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
  switch (n.getType()) {
    case Token.NAME:
      String newName = getReplacementName(n.getString());
      if (newName != null) {
        Renamer renamer = nameStack.peek();
        if (renamer.stripConstIfReplaced()) {
          // TODO(johnlenz): Do we need to do anything about the Javadoc?
          n.removeProp(Node.IS_CONSTANT_NAME);
        }
        n.setString(newName);
        t.getCompiler().reportCodeChange();
      }
      break;

    case Token.FUNCTION:
      // Remove the function body scope
      nameStack.pop();
      // Remove function recursive name (if any).
      nameStack.pop();
      break;

    case Token.PARAM_LIST:
      // Note: The parameters and function body variables live in the
      // same scope, we introduce the scope when in the "shouldTraverse"
      // visit of LP, but remove it when when we exit the function above.
      break;

    case Token.CATCH:
      // Remove catch except name from the stack of names.
      nameStack.pop();
      break;
  }
}
 
Example 8
Source File: StrictModeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines if the given name is a declaration, which can be a declaration
 * of a variable, function, or argument.
 */
private static boolean isDeclaration(Node n) {
  switch (n.getParent().getType()) {
    case Token.VAR:
    case Token.FUNCTION:
    case Token.CATCH:
      return true;

    case Token.PARAM_LIST:
      return n.getParent().getParent().isFunction();

    default:
      return false;
  }
}
 
Example 9
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 10
Source File: FunctionType.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Creates an instance for a function that is an interface. */
private FunctionType(JSTypeRegistry registry, String name, Node source) {
  super(registry, name,
      registry.getNativeObjectType(JSTypeNative.FUNCTION_INSTANCE_TYPE));
  setPrettyPrint(true);

  Preconditions.checkArgument(source == null ||
      Token.FUNCTION == source.getType());
  Preconditions.checkArgument(name != null);
  this.source = source;
  this.call = new ArrowType(registry, new Node(Token.PARAM_LIST), null);
  this.kind = Kind.INTERFACE;
  this.typeOfThis = new InstanceObjectType(registry, this);
}
 
Example 11
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 12
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 13
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 14
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 15
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 16
Source File: Nopol2017_0029_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}
 
Example 17
Source File: Nopol2017_0029_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a NAME 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.
 * @param parent The parent of the node n.
 * @return whether the node is typeable or not
 */
boolean visitName(NodeTraversal t, Node n, Node parent) {
  // At this stage, we need to determine whether this is a leaf
  // node in an expression (which therefore needs to have a type
  // assigned for it) versus some other decorative node that we
  // can safely ignore.  Function names, arguments (children of LP nodes) and
  // variable declarations are ignored.
  // TODO(user): remove this short-circuiting in favor of a
  // pre order traversal of the FUNCTION, CATCH, LP and VAR nodes.
  int parentNodeType = parent.getType();
  if (parentNodeType == Token.FUNCTION ||
      parentNodeType == Token.CATCH ||
      parentNodeType == Token.PARAM_LIST ||
      parentNodeType == Token.VAR) {
    return false;
  }

  JSType type = n.getJSType();
  if (type == null) {
    type = getNativeType(UNKNOWN_TYPE);
    Var var = t.getScope().getVar(n.getString());
    if (var != null) {
      JSType varType = var.getType();
      if (varType != null) {
        type = varType;
      }
    }
  }
  ensureTyped(t, n, type);
  return true;
}