Java Code Examples for com.google.javascript.rhino.JSDocInfo#isDefine()

The following examples show how to use com.google.javascript.rhino.JSDocInfo#isDefine() . 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: Nopol2017_0010_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Verifies that a provide method call has exactly one argument,
 * and that it's a string literal and that the contents of the string are
 * valid JS tokens. Reports a compile error if it doesn't.
 *
 * @return Whether the argument checked out okay
 */
private boolean verifyDefine(NodeTraversal t,
    Node expr,
    Node methodName, Node args) {

  // Verify first arg
  Node arg = args;
  if (!verifyNotNull(t, methodName, arg) ||
      !verifyOfType(t, methodName, arg, Token.STRING)) {
    return false;
  }

  // Verify second arg
  arg = arg.getNext();
  if (!verifyNotNull(t, methodName, arg) ||
      !verifyIsLast(t, methodName, arg)) {
    return false;
  }

  String name = args.getString();
  for (String part : name.split("\\.")) {
    if (!NodeUtil.isValidQualifiedName(part)) {
      compiler.report(t.makeError(args, INVALID_DEFINE_NAME_ERROR, name));
      return false;
    }
  }

  JSDocInfo info = expr.getFirstChild().getJSDocInfo();
  if (info == null || !info.isDefine()) {
    compiler.report(t.makeError(expr, MISSING_DEFINE_ANNOTATION));
    return false;
  }
  return true;
}
 
Example 2
Source File: Nopol2017_0010_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Verifies that a provide method call has exactly one argument,
 * and that it's a string literal and that the contents of the string are
 * valid JS tokens. Reports a compile error if it doesn't.
 *
 * @return Whether the argument checked out okay
 */
private boolean verifyDefine(NodeTraversal t,
    Node expr,
    Node methodName, Node args) {

  // Verify first arg
  Node arg = args;
  if (!verifyNotNull(t, methodName, arg) ||
      !verifyOfType(t, methodName, arg, Token.STRING)) {
    return false;
  }

  // Verify second arg
  arg = arg.getNext();
  if (!verifyNotNull(t, methodName, arg) ||
      !verifyIsLast(t, methodName, arg)) {
    return false;
  }

  String name = args.getString();
  for (String part : name.split("\\.")) {
    if (!NodeUtil.isValidQualifiedName(part)) {
      compiler.report(t.makeError(args, INVALID_DEFINE_NAME_ERROR, name));
      return false;
    }
  }

  JSDocInfo info = expr.getFirstChild().getJSDocInfo();
  if (info == null || !info.isDefine()) {
    compiler.report(t.makeError(expr, MISSING_DEFINE_ANNOTATION));
    return false;
  }
  return true;
}
 
Example 3
Source File: Closure_113_ProcessClosurePrimitives_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Verifies that a provide method call has exactly one argument,
 * and that it's a string literal and that the contents of the string are
 * valid JS tokens. Reports a compile error if it doesn't.
 *
 * @return Whether the argument checked out okay
 */
private boolean verifyDefine(NodeTraversal t,
    Node expr,
    Node methodName, Node args) {

  // Verify first arg
  Node arg = args;
  if (!verifyNotNull(t, methodName, arg) ||
      !verifyOfType(t, methodName, arg, Token.STRING)) {
    return false;
  }

  // Verify second arg
  arg = arg.getNext();
  if (!verifyNotNull(t, methodName, arg) ||
      !verifyIsLast(t, methodName, arg)) {
    return false;
  }

  String name = args.getString();
  for (String part : name.split("\\.")) {
    if (!NodeUtil.isValidQualifiedName(part)) {
      compiler.report(t.makeError(args, INVALID_DEFINE_NAME_ERROR, name));
      return false;
    }
  }

  JSDocInfo info = expr.getFirstChild().getJSDocInfo();
  if (info == null || !info.isDefine()) {
    compiler.report(t.makeError(expr, MISSING_DEFINE_ANNOTATION));
    return false;
  }
  return true;
}
 
Example 4
Source File: Closure_113_ProcessClosurePrimitives_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Verifies that a provide method call has exactly one argument,
 * and that it's a string literal and that the contents of the string are
 * valid JS tokens. Reports a compile error if it doesn't.
 *
 * @return Whether the argument checked out okay
 */
private boolean verifyDefine(NodeTraversal t,
    Node expr,
    Node methodName, Node args) {

  // Verify first arg
  Node arg = args;
  if (!verifyNotNull(t, methodName, arg) ||
      !verifyOfType(t, methodName, arg, Token.STRING)) {
    return false;
  }

  // Verify second arg
  arg = arg.getNext();
  if (!verifyNotNull(t, methodName, arg) ||
      !verifyIsLast(t, methodName, arg)) {
    return false;
  }

  String name = args.getString();
  for (String part : name.split("\\.")) {
    if (!NodeUtil.isValidQualifiedName(part)) {
      compiler.report(t.makeError(args, INVALID_DEFINE_NAME_ERROR, name));
      return false;
    }
  }

  JSDocInfo info = expr.getFirstChild().getJSDocInfo();
  if (info == null || !info.isDefine()) {
    compiler.report(t.makeError(expr, MISSING_DEFINE_ANNOTATION));
    return false;
  }
  return true;
}
 
Example 5
Source File: Closure_122_IRFactory_t.java    From coming with MIT License 4 votes vote down vote up
private void validateTypeAnnotations(JSDocInfo info, AstNode node) {
  if (info.hasType()) {
    boolean valid = false;
    switch (node.getType()) {
      // Casts are valid
      case com.google.javascript.rhino.head.Token.LP:
        valid = node instanceof ParenthesizedExpression;
        break;
      // Variable declarations are valid
      case com.google.javascript.rhino.head.Token.VAR:
        valid = true;
        break;
      // Function declarations are valid
      case com.google.javascript.rhino.head.Token.FUNCTION:
        FunctionNode fnNode = (FunctionNode) node;
        valid = fnNode.getFunctionType() == FunctionNode.FUNCTION_STATEMENT;
        break;
      // Object literal properties, catch declarations and variable
      // initializers are valid.
      case com.google.javascript.rhino.head.Token.NAME:
        AstNode parent = node.getParent();
        valid = parent instanceof ObjectProperty
            || parent instanceof CatchClause
            || parent instanceof FunctionNode
            || (parent instanceof VariableInitializer &&
                node == ((VariableInitializer) parent).getTarget());
        break;
      // Object literal properties are valid
      case com.google.javascript.rhino.head.Token.GET:
      case com.google.javascript.rhino.head.Token.SET:
      case com.google.javascript.rhino.head.Token.NUMBER:
      case com.google.javascript.rhino.head.Token.STRING:
        valid = node.getParent() instanceof ObjectProperty;
        break;

      // Property assignments are valid, if at the root of an expression.
      case com.google.javascript.rhino.head.Token.ASSIGN:
        if (node instanceof Assignment) {
          valid = isExprStmt(node.getParent())
              && isPropAccess(((Assignment) node).getLeft());
        }
        break;

      // Property definitions are valid, if at the root of an expression.
      case com.google.javascript.rhino.head.Token.GETPROP:
      case com.google.javascript.rhino.head.Token.GETELEM:
        valid = isExprStmt(node.getParent());
        break;

      case com.google.javascript.rhino.head.Token.CALL:
        valid = info.isDefine();
        break;
    }
    if (!valid) {
      errorReporter.warning(MISPLACED_TYPE_ANNOTATION,
          sourceName,
          node.getLineno(), "", 0);
    }
  }
}
 
Example 6
Source File: Closure_122_IRFactory_s.java    From coming with MIT License 4 votes vote down vote up
private void validateTypeAnnotations(JSDocInfo info, AstNode node) {
  if (info.hasType()) {
    boolean valid = false;
    switch (node.getType()) {
      // Casts are valid
      case com.google.javascript.rhino.head.Token.LP:
        valid = node instanceof ParenthesizedExpression;
        break;
      // Variable declarations are valid
      case com.google.javascript.rhino.head.Token.VAR:
        valid = true;
        break;
      // Function declarations are valid
      case com.google.javascript.rhino.head.Token.FUNCTION:
        FunctionNode fnNode = (FunctionNode) node;
        valid = fnNode.getFunctionType() == FunctionNode.FUNCTION_STATEMENT;
        break;
      // Object literal properties, catch declarations and variable
      // initializers are valid.
      case com.google.javascript.rhino.head.Token.NAME:
        AstNode parent = node.getParent();
        valid = parent instanceof ObjectProperty
            || parent instanceof CatchClause
            || parent instanceof FunctionNode
            || (parent instanceof VariableInitializer &&
                node == ((VariableInitializer) parent).getTarget());
        break;
      // Object literal properties are valid
      case com.google.javascript.rhino.head.Token.GET:
      case com.google.javascript.rhino.head.Token.SET:
      case com.google.javascript.rhino.head.Token.NUMBER:
      case com.google.javascript.rhino.head.Token.STRING:
        valid = node.getParent() instanceof ObjectProperty;
        break;

      // Property assignments are valid, if at the root of an expression.
      case com.google.javascript.rhino.head.Token.ASSIGN:
        if (node instanceof Assignment) {
          valid = isExprStmt(node.getParent())
              && isPropAccess(((Assignment) node).getLeft());
        }
        break;

      // Property definitions are valid, if at the root of an expression.
      case com.google.javascript.rhino.head.Token.GETPROP:
      case com.google.javascript.rhino.head.Token.GETELEM:
        valid = isExprStmt(node.getParent());
        break;

      case com.google.javascript.rhino.head.Token.CALL:
        valid = info.isDefine();
        break;
    }
    if (!valid) {
      errorReporter.warning(MISPLACED_TYPE_ANNOTATION,
          sourceName,
          node.getLineno(), "", 0);
    }
  }
}
 
Example 7
Source File: Scope.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code true} if the variable is declared as a define.
 * A variable is a define if it is annotated by {@code @define}.
 */
public boolean isDefine() {
  JSDocInfo info = getJSDocInfo();
  return info != null && info.isDefine();
}
 
Example 8
Source File: ProcessDefines.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Finds all defines, and creates a {@link DefineInfo} data structure for
 * each one.
 * @return A map of {@link DefineInfo} structures, keyed by name.
 */
private Map<String, DefineInfo> collectDefines(Node root,
    GlobalNamespace namespace) {
  // Find all the global names with a @define annotation
  List<Name> allDefines = Lists.newArrayList();
  for (Name name : namespace.getNameIndex().values()) {
    Ref decl = name.getDeclaration();
    if (name.docInfo != null && name.docInfo.isDefine()) {
      // Process defines should not depend on check types being enabled,
      // so we look for the JSDoc instead of the inferred type.
      if (isValidDefineType(name.docInfo.getType())) {
        allDefines.add(name);
      } else {
        JSError error = JSError.make(
            decl.getSourceName(),
            decl.node, INVALID_DEFINE_TYPE_ERROR);
        compiler.report(error);
      }
    } else {
      for (Ref ref : name.getRefs()) {
        if (ref == decl) {
          // Declarations were handled above.
          continue;
        }

        Node n = ref.node;
        Node parent = ref.node.getParent();
        JSDocInfo info = n.getJSDocInfo();
        if (info == null &&
            parent.isVar() && parent.hasOneChild()) {
          info = parent.getJSDocInfo();
        }

        if (info != null && info.isDefine()) {
          allDefines.add(name);
          break;
        }
      }
    }
  }

  CollectDefines pass = new CollectDefines(compiler, allDefines);
  NodeTraversal.traverse(compiler, root, pass);
  return pass.getAllDefines();
}