Java Code Examples for com.google.javascript.rhino.jstype.JSType#isOrdinaryFunction()

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isOrdinaryFunction() . 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: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
private boolean isOrdinaryFunction(JSType ftype) {
  // Closure represents top-level functions as classes because they might be new-able.
  // This happens through externs es3.js which has Function marked as constructor.
  // See https://github.com/angular/closure-to-dts/issues/90
  boolean ordinaryFunctionAppearingAsClass =
      ftype.isConstructor() && "Function".equals(ftype.getDisplayName());
  return ftype.isOrdinaryFunction() || ordinaryFunctionAppearingAsClass;
}
 
Example 2
Source File: Nopol2017_0051_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 3
Source File: Nopol2017_0051_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 4
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 5
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 6
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 7
Source File: Closure_66_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 8
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 9
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits an ASSIGN node for cases such as
 * <pre>
 * interface.property2.property = ...;
 * </pre>
 */
private void visitInterfaceGetprop(NodeTraversal t, Node assign, Node object,
    String property, Node lvalue, Node rvalue) {

  JSType rvalueType = getJSType(rvalue);

  // Only 2 values are allowed for methods:
  //    goog.abstractMethod
  //    function () {};
  // or for properties, no assignment such as:
  //    InterfaceFoo.prototype.foobar;

  String abstractMethodName =
      compiler.getCodingConvention().getAbstractMethodName();
  if (!rvalueType.isOrdinaryFunction() &&
      !(rvalue.isQualifiedName() &&
        rvalue.getQualifiedName().equals(abstractMethodName))) {
    // This is bad i18n style but we don't localize our compiler errors.
    String abstractMethodMessage = (abstractMethodName != null)
       ? ", or " + abstractMethodName
       : "";
    compiler.report(
        t.makeError(object, INVALID_INTERFACE_MEMBER_DECLARATION,
            abstractMethodMessage));
  }

  if (assign.getLastChild().getType() == Token.FUNCTION
      && !NodeUtil.isEmptyBlock(assign.getLastChild().getLastChild())) {
    compiler.report(
        t.makeError(object, INTERFACE_FUNCTION_NOT_EMPTY,
            abstractMethodName));
  }
}
 
Example 10
Source File: AbstractClosureVisitor.java    From jsinterop-generator with Apache License 2.0 4 votes vote down vote up
protected void acceptMember(StaticTypedSlot member, boolean isStatic) {
  JSType propertyType = member.getType();
  // Check if this member is an extension of the API of a type provided by an external third party
  // library. If it's the case, use the extension class if it has already been created.
  boolean extensionTypePushed = maybePushCurrentExtensionType(member);

  // We are visiting field or function belonging to a Type or a namespace.
  // For correctly converting functions, we have to differentiate 2 cases:
  //   /**
  //    * @type {function(Event):boolean}
  //    */
  //    Foo.prototype.callback;
  //
  //   /**
  //    * @param {Event} evt
  //    * @return {boolean}
  //    */
  //    Foo.prototype.callbackFunction = function(evt) {};
  //
  // The type of both symbols are function type but the first one need to be converted as a Field
  // with type a JsFunction interface. The second symbol need to to be converted as a Method.
  // The only way to differentiate these two cases are to test if the function type has a name or
  // not.
  if (propertyType.isOrdinaryFunction() && !isAnonymousFunctionType(propertyType)) {
    FunctionType method = toFunctionType(propertyType);
    if (visitMethod(method, isStatic)) {
      // can contain anonymous RecordType/FunctionType to visit
      acceptType(method.getReturnType());
      acceptParameters(method);
    }
    endVisitMethod(method);
  } else {
    if (visitField(member, isStatic)) {
      acceptType(propertyType);
    }
  }

  if (extensionTypePushed) {
    popCurrentJavaType();
  }
}
 
Example 11
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private void processSymbol(Symbol symbol) {
  final JSType globalThis =
      compiler.getTypeRegistry().getNativeObjectType(JSTypeNative.GLOBAL_THIS);

  if (typeRegistry.isType(symbol.getName())) {
    return;
  }

  JSType type = null;
  boolean isFromCompilerRegistry = false;

  if (symbol.getReferencedSymbol() != null
      && typeRegistry.isType(symbol.getReferencedSymbol())) {
    type = typeRegistry.getType(symbol.getReferencedSymbol()).getType();
  }

  if (type == null) {
    type = compiler.getTypeRegistry().getGlobalType(symbol.getName());
    isFromCompilerRegistry = type != null;
  }

  if (type == null) {
    type = globalThis;
    for (String part : Splitter.on('.').split(symbol.getName())) {
      type = type.findPropertyType(part);
      if (type == null) {
        return;
      }
    }
  }

  if (type != null) {
    if (type.isInstanceType()
        && (isFromCompilerRegistry
            || shouldConvertToConstructor(type)
            || shouldConvertToConstructor(symbol))) {
      type = type.toObjectType().getConstructor();
    } else if (type.isEnumElementType()) {
      type = type.toMaybeEnumElementType().getEnumType();
    }
  }

  if (type == null) {
    log.warning("skipping " + symbol.getName() + "; no type");
    return;
  } else if (globalThis.equals(type)) {
    log.warning("skipping " + symbol.getName() + "; references global this");
    return;
  } else if (type.isOrdinaryFunction() && !typeRegistry.isProvided(symbol.getName())) {
    log.info("skipping " + symbol.getName() + "; is ordinary function");
    return;
  }
  collectTypes(symbol.getName(), type, symbol.getNode(), symbol.getJSDocInfo());
}