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

The following examples show how to use com.google.javascript.rhino.JSDocInfo#isConstructor() . 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: Closure_130_CollapseProperties_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Warns about any references to "this" in the given FUNCTION. The function
 * is getting collapsed, so the references will change.
 */
private void checkForHosedThisReferences(Node function, JSDocInfo docInfo,
    final Name name) {
  // A function is getting collapsed. Make sure that if it refers to
  // "this", it must be a constructor or documented with @this.
  if (docInfo == null ||
      (!docInfo.isConstructor() && !docInfo.hasThisType())) {
    NodeTraversal.traverse(compiler, function.getLastChild(),
        new NodeTraversal.AbstractShallowCallback() {
          @Override
          public void visit(NodeTraversal t, Node n, Node parent) {
            if (n.isThis()) {
              compiler.report(
                  JSError.make(name.getDeclaration().getSourceName(), n,
                      UNSAFE_THIS, name.getFullName()));
            }
          }
        });
  }
}
 
Example 2
Source File: Closure_89_CollapseProperties_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Warns about any references to "this" in the given FUNCTION. The function
 * is getting collapsed, so the references will change.
 */
private void checkForHosedThisReferences(Node function, JSDocInfo docInfo,
    final Name name) {
  // A function is getting collapsed. Make sure that if it refers to
  // "this", it must be a constructor or documented with @this.
  if (docInfo == null ||
      (!docInfo.isConstructor() && !docInfo.hasThisType())) {
    NodeTraversal.traverse(compiler, function.getLastChild(),
        new NodeTraversal.AbstractShallowCallback() {
          public void visit(NodeTraversal t, Node n, Node parent) {
            if (n.getType() == Token.THIS) {
              compiler.report(
                  JSError.make(name.declaration.sourceName, n,
                      UNSAFE_THIS, name.fullName()));
            }
          }
        });
  }
}
 
Example 3
Source File: CheckProvides.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void visitFunctionNode(Node n, Node parent) {
  Node name = null;
  JSDocInfo info = parent.getJSDocInfo();
  if (info != null && info.isConstructor()) {
    name = parent.getFirstChild();
  } else {
    // look to the child, maybe it's a named function
    info = n.getJSDocInfo();
    if (info != null && info.isConstructor()) {
      name = n.getFirstChild();
    }
  }
  if (name != null && name.isQualifiedName()) {
    String qualifiedName = name.getQualifiedName();
    if (!this.convention.isPrivate(qualifiedName)) {
      Visibility visibility = info.getVisibility();
      if (!visibility.equals(JSDocInfo.Visibility.PRIVATE)) {
        ctors.put(qualifiedName, name);
      }
    }
  }
}
 
Example 4
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Determines whether the given name is visible in the current context.
 * @param t The current traversal.
 * @param name The name node.
 */
private void checkNameVisibility(NodeTraversal t, Node name, Node parent) {
  Var var = t.getScope().getVar(name.getString());
  if (var != null) {
    JSDocInfo docInfo = var.getJSDocInfo();
    if (docInfo != null) {
      // If a name is private, make sure that we're in the same file.
      Visibility visibility = docInfo.getVisibility();
      if (visibility == Visibility.PRIVATE &&
          !t.getInput().getName().equals(docInfo.getSourceName())) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        compiler.report(
            t.makeError(name, BAD_PRIVATE_GLOBAL_ACCESS,
                name.getString(), docInfo.getSourceName()));
      }
    }
  }
}
 
Example 5
Source File: Closure_89_GlobalNamespace_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether a set operation is a constructor or enumeration
 * declaration. The set operation may either be an assignment to a name,
 * a variable declaration, or an object literal key mapping.
 *
 * @param n The node that represents the name being set
 * @param parent Parent node of {@code n} (an ASSIGN, VAR, or OBJLIT node)
 * @return Whether the set operation is either a constructor or enum
 *     declaration
 */
private boolean isConstructorOrEnumDeclaration(Node n, Node parent) {
  // NOTE(nicksantos): This does not handle named constructors
  // function a() {}
  // For legacy reasons, we should not fix this, because we do not
  // know who's depending on the current behavior.

  JSDocInfo info;
  int valueNodeType;
  switch (parent.getType()) {
    case Token.ASSIGN:
      info = parent.getJSDocInfo();
      valueNodeType = n.getNext().getType();
      break;
    case Token.VAR:
      info = n.getJSDocInfo();
      if (info == null) {
        info = parent.getJSDocInfo();
      }
      Node valueNode = n.getFirstChild();
      valueNodeType = valueNode != null ? valueNode.getType() : Token.VOID;
      break;
    default:
      return false;
  }
  // Heed the annotations only if they're sensibly used.
  return info != null &&
         (info.isConstructor() && valueNodeType == Token.FUNCTION ||
          info.hasEnumParameterType() && valueNodeType == Token.OBJECTLIT);
}
 
Example 6
Source File: Closure_41_FunctionTypeBuilder_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Determines whether the given jsdoc info declares a function type.
 */
static boolean isFunctionTypeDeclaration(JSDocInfo info) {
  return info.getParameterCount() > 0 ||
      info.hasReturnType() ||
      info.hasThisType() ||
      info.isConstructor() ||
      info.isInterface();
}
 
Example 7
Source File: FunctionTypeBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determines whether the given JsDoc info declares a function type.
 */
static boolean isFunctionTypeDeclaration(JSDocInfo info) {
  return info.getParameterCount() > 0 ||
      info.hasReturnType() ||
      info.hasThisType() ||
      info.isConstructor() ||
      info.isInterface();
}
 
Example 8
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            return rValue.getJSType();
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 9
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            return rValue.getJSType();
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 10
Source File: Closure_54_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.getType() == Token.FUNCTION &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.getType() == Token.OBJECTLIT) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            return rValue.getJSType();
          } else if (rValue.getType() == Token.OR) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.getType() == Token.NAME
                && lValue.getType() == Token.NAME
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 11
Source File: Closure_70_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.getType() == Token.FUNCTION &&
      shouldUseFunctionLiteralType(
          (FunctionType) rValue.getJSType(), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.getType() == Token.OBJECTLIT) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            return rValue.getJSType();
          } else if (rValue.getType() == Token.OR) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.getType() == Token.NAME
                && lValue.getType() == Token.NAME
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 12
Source File: Closure_100_CheckGlobalThis_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Since this pass reports errors only when a global {@code this} keyword
 * is encountered, there is no reason to traverse non global contexts.
 */
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {

  if (n.getType() == Token.FUNCTION) {
    // Don't traverse functions that are constructors or have the @this
    // annotation.
    JSDocInfo jsDoc = getFunctionJsDocInfo(n);
    if (jsDoc != null && (jsDoc.isConstructor() || jsDoc.hasThisType())) {
      return false;
    }

    // Don't traverse functions unless they would normally
    // be able to have a @this annotation associated with them. e.g.,
    // var a = function() { }; // or
    // function a() {} // or
    // a.x = function() {};
  }

  if (parent != null && parent.getType() == Token.ASSIGN) {
    Node lhs = parent.getFirstChild();
    Node rhs = lhs.getNext();
    
    if (n == lhs) {
      // Always traverse the left side of the assignment. To handle
      // nested assignments properly (e.g., (a = this).property = c;),
      // assignLhsChild should not be overridden.
      if (assignLhsChild == null) {
        assignLhsChild = lhs;
      }
    } else {
      // Only traverse the right side if it's not an assignment to a prototype
      // property or subproperty.
      if (lhs.getType() == Token.GETPROP) {
        if (lhs.getLastChild().getString().equals("prototype")) {
          return false;
        }
        String leftName = lhs.getQualifiedName();
        if (leftName != null && leftName.contains(".prototype.")) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 13
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private boolean shouldConvertToConstructor(JSType type) {
  JSDocInfo info = type.getJSDocInfo();
  return info != null && (info.isInterface() || info.isConstructor());
}
 
Example 14
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
JSType getDeclaredTypeInAnnotation(String sourceName,
    Node node, JSDocInfo info) {
  JSType jsType = null;
  Node objNode = node.getType() == Token.GETPROP ?
      node.getFirstChild() : null;
  if (info != null) {
    if (info.hasType()) {
      jsType = info.getType().evaluate(scope, typeRegistry);
    } else if (FunctionTypeBuilder.isFunctionTypeDeclaration(info)) {
      String fnName = node.getQualifiedName();

      // constructors are often handled separately.
      if (info.isConstructor() && typeRegistry.getType(fnName) != null) {
        return null;
      }

      FunctionTypeBuilder builder =
          new FunctionTypeBuilder(
              fnName, compiler, node, sourceName, scope)
          .inferTemplateTypeName(info)
          .inferReturnType(info)
          .inferParameterTypes(info)
          .inferInheritance(info);

      // Infer the context type.
      boolean searchedForThisType = false;
      if (objNode != null) {
        if (objNode.getType() == Token.GETPROP &&
            objNode.getLastChild().getString().equals("prototype")) {
          builder.inferThisType(info, objNode.getFirstChild());
          searchedForThisType = true;
        } else if (objNode.getType() == Token.THIS) {
          builder.inferThisType(info, objNode.getJSType());
          searchedForThisType = true;
        }
      }

      if (!searchedForThisType) {
        builder.inferThisType(info, (Node) null);
      }

      jsType = builder.buildAndRegister();
    }
  }
  return jsType;
}
 
Example 15
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          JSDocInfo rValueInfo = rValue.getJSDocInfo();
          if (rValueInfo != null && rValueInfo.hasType()) {
            // If rValue has a type-cast, we use the type in the type-cast.
            return rValueInfo.getType().evaluate(scope, typeRegistry);
          } else if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            // If rValue's type was already computed during scope creation,
            // then we can safely use that.
            return rValue.getJSType();
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 16
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null && !rValue.getJSType().isUnknownType()) {
            // If rValue has a type-cast, we use the type in the type-cast.
            // If rValue's type was already computed during scope creation,
            // then we can safely use that.
            return rValue.getJSType();
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 17
Source File: Nopol2017_0027_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null && !rValue.getJSType().isUnknownType()) {
            // If rValue has a type-cast, we use the type in the type-cast.
            // If rValue's type was already computed during scope creation,
            // then we can safely use that.
            return rValue.getJSType();
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 18
Source File: Closure_95_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
JSType getDeclaredTypeInAnnotation(String sourceName,
    Node node, JSDocInfo info) {
  JSType jsType = null;
  Node objNode = node.getType() == Token.GETPROP ?
      node.getFirstChild() : null;
  if (info != null) {
    if (info.hasType()) {
      jsType = info.getType().evaluate(scope, typeRegistry);
    } else if (FunctionTypeBuilder.isFunctionTypeDeclaration(info)) {
      String fnName = node.getQualifiedName();

      // constructors are often handled separately.
      if (info.isConstructor() && typeRegistry.getType(fnName) != null) {
        return null;
      }

      FunctionTypeBuilder builder =
          new FunctionTypeBuilder(
              fnName, compiler, node, sourceName, scope)
          .inferTemplateTypeName(info)
          .inferReturnType(info)
          .inferParameterTypes(info)
          .inferInheritance(info);

      // Infer the context type.
      boolean searchedForThisType = false;
      if (objNode != null) {
        if (objNode.getType() == Token.GETPROP &&
            objNode.getLastChild().getString().equals("prototype")) {
          builder.inferThisType(info, objNode.getFirstChild());
          searchedForThisType = true;
        } else if (objNode.getType() == Token.THIS) {
          builder.inferThisType(info, objNode.getJSType());
          searchedForThisType = true;
        }
      }

      if (!searchedForThisType) {
        builder.inferThisType(info, (Node) null);
      }

      jsType = builder.buildAndRegister();
    }
  }
  return jsType;
}
 
Example 19
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Look for a type declaration on a property assignment
 * (in an ASSIGN or an object literal key).
 *
 * @param info The doc info for this property.
 * @param lValue The l-value node.
 * @param rValue The node that {@code n} is being initialized to,
 *     or {@code null} if this is a stub declaration.
 */
private JSType getDeclaredType(String sourceName, JSDocInfo info,
    Node lValue, @Nullable Node rValue) {
  if (info != null && info.hasType()) {
    return getDeclaredTypeInAnnotation(sourceName, lValue, info);
  } else if (rValue != null && rValue.isFunction() &&
      shouldUseFunctionLiteralType(
          JSType.toMaybeFunctionType(rValue.getJSType()), info, lValue)) {
    return rValue.getJSType();
  } else if (info != null) {
    if (info.hasEnumParameterType()) {
      if (rValue != null && rValue.isObjectLit()) {
        return rValue.getJSType();
      } else {
        return createEnumTypeFromNodes(
            rValue, lValue.getQualifiedName(), info, lValue);
      }
    } else if (info.isConstructor() || info.isInterface()) {
      return createFunctionTypeFromNodes(
          rValue, lValue.getQualifiedName(), info, lValue);
    } else {
      // Check if this is constant, and if it has a known type.
      if (info.isConstant()) {
        JSType knownType = null;
        if (rValue != null) {
          if (rValue.getJSType() != null
              && !rValue.getJSType().isUnknownType()) {
            return rValue.getJSType();
          } else if (rValue.isOr()) {
            // Check for a very specific JS idiom:
            // var x = x || TYPE;
            // This is used by Closure's base namespace for esoteric
            // reasons.
            Node firstClause = rValue.getFirstChild();
            Node secondClause = firstClause.getNext();
            boolean namesMatch = firstClause.isName()
                && lValue.isName()
                && firstClause.getString().equals(lValue.getString());
            if (namesMatch && secondClause.getJSType() != null
                && !secondClause.getJSType().isUnknownType()) {
              return secondClause.getJSType();
            }
          }
        }
      }
    }
  }

  return getDeclaredTypeInAnnotation(sourceName, lValue, info);
}
 
Example 20
Source File: Nopol2017_0014_t.java    From coming with MIT License 3 votes vote down vote up
/**
 * Determines whether a set operation is a constructor or enumeration
 * or interface declaration. The set operation may either be an assignment
 * to a name, a variable declaration, or an object literal key mapping.
 *
 * @param n The node that represents the name being set
 * @param parent Parent node of {@code n} (an ASSIGN, VAR, or OBJLIT node)
 * @return Whether the set operation is either a constructor or enum
 *     declaration
 */
private boolean isTypeDeclaration(Node n, Node parent) {
  Node valueNode = NodeUtil.getRValueOfLValue(n);
  JSDocInfo info = NodeUtil.getBestJSDocInfo(n);
  // Heed the annotations only if they're sensibly used.
  return info != null && valueNode != null &&
         (info.isConstructor() && valueNode.isFunction() ||
          info.isInterface() && valueNode.isFunction() ||
          info.hasEnumParameterType() && valueNode.isObjectLit());
}