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

The following examples show how to use com.google.javascript.rhino.Node#isUnscopedQualifiedName() . 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: 1_ClosureCodingConvention.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Understands several different inheritance patterns that occur in
 * Google code (various uses of {@code inherits} and {@code mixin}).
 */
@Override
public SubclassRelationship getClassesDefinedByCall(Node callNode) {
  Node callName = callNode.getFirstChild();
  SubclassType type = typeofClassDefiningName(callName);
  if (type != null) {
    Node subclass = null;
    Node superclass = callNode.getLastChild();

    // There are six possible syntaxes for a class-defining method:
    // SubClass.inherits(SuperClass)
    // goog.inherits(SubClass, SuperClass)
    // goog$inherits(SubClass, SuperClass)
    // SubClass.mixin(SuperClass.prototype)
    // goog.mixin(SubClass.prototype, SuperClass.prototype)
    // goog$mixin(SubClass.prototype, SuperClass.prototype)
    boolean isDeprecatedCall = callNode.getChildCount() == 2 &&
        callName.getType() == Token.GETPROP;
    if (isDeprecatedCall) {
      // SubClass.inherits(SuperClass)
      subclass = callName.getFirstChild();
    } else if (callNode.getChildCount() == 3) {
      // goog.inherits(SubClass, SuperClass)
      subclass = callName.getNext();
    } else {
      return null;
    }

    if (type == SubclassType.MIXIN) {
      // Only consider mixins that mix two prototypes as related to
      // inheritance.
      if (!endsWithPrototype(superclass)) {
        return null;
      }
      if (!isDeprecatedCall) {
        if (!endsWithPrototype(subclass)) {
          return null;
        }
        // Strip off the prototype from the name.
        subclass = subclass.getFirstChild();
      }
      superclass = superclass.getFirstChild();
    }

    // bail out if either of the side of the "inherits"
    // isn't a real class name. This prevents us from
    // doing something weird in cases like:
    // goog.inherits(MySubClass, cond ? SuperClass1 : BaseClass2)
    if (subclass != null &&
        subclass.isUnscopedQualifiedName() &&
        superclass.isUnscopedQualifiedName()) {
      return new SubclassRelationship(type, subclass, superclass);
    }
  }

  return null;
}
 
Example 2
Source File: 1_ClosureCodingConvention.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Understands several different inheritance patterns that occur in
 * Google code (various uses of {@code inherits} and {@code mixin}).
 */
@Override
public SubclassRelationship getClassesDefinedByCall(Node callNode) {
  Node callName = callNode.getFirstChild();
  SubclassType type = typeofClassDefiningName(callName);
  if (type != null) {
    Node subclass = null;
    Node superclass = callNode.getLastChild();

    // There are six possible syntaxes for a class-defining method:
    // SubClass.inherits(SuperClass)
    // goog.inherits(SubClass, SuperClass)
    // goog$inherits(SubClass, SuperClass)
    // SubClass.mixin(SuperClass.prototype)
    // goog.mixin(SubClass.prototype, SuperClass.prototype)
    // goog$mixin(SubClass.prototype, SuperClass.prototype)
    boolean isDeprecatedCall = callNode.getChildCount() == 2 &&
        callName.getType() == Token.GETPROP;
    if (isDeprecatedCall) {
      // SubClass.inherits(SuperClass)
      subclass = callName.getFirstChild();
    } else if (callNode.getChildCount() == 3) {
      // goog.inherits(SubClass, SuperClass)
      subclass = callName.getNext();
    } else {
      return null;
    }

    if (type == SubclassType.MIXIN) {
      // Only consider mixins that mix two prototypes as related to
      // inheritance.
      if (!endsWithPrototype(superclass)) {
        return null;
      }
      if (!isDeprecatedCall) {
        if (!endsWithPrototype(subclass)) {
          return null;
        }
        // Strip off the prototype from the name.
        subclass = subclass.getFirstChild();
      }
      superclass = superclass.getFirstChild();
    }

    // bail out if either of the side of the "inherits"
    // isn't a real class name. This prevents us from
    // doing something weird in cases like:
    // goog.inherits(MySubClass, cond ? SuperClass1 : BaseClass2)
    if (subclass != null &&
        subclass.isUnscopedQualifiedName() &&
        superclass.isUnscopedQualifiedName()) {
      return new SubclassRelationship(type, subclass, superclass);
    }
  }

  return null;
}
 
Example 3
Source File: 1_ClosureCodingConvention.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Understands several different inheritance patterns that occur in
 * Google code (various uses of {@code inherits} and {@code mixin}).
 */
@Override
public SubclassRelationship getClassesDefinedByCall(Node callNode) {
  Node callName = callNode.getFirstChild();
  SubclassType type = typeofClassDefiningName(callName);
  if (type != null) {
    Node subclass = null;
    Node superclass = callNode.getLastChild();

    // There are six possible syntaxes for a class-defining method:
    // SubClass.inherits(SuperClass)
    // goog.inherits(SubClass, SuperClass)
    // goog$inherits(SubClass, SuperClass)
    // SubClass.mixin(SuperClass.prototype)
    // goog.mixin(SubClass.prototype, SuperClass.prototype)
    // goog$mixin(SubClass.prototype, SuperClass.prototype)
    boolean isDeprecatedCall = callNode.getChildCount() == 2 &&
        callName.getType() == Token.GETPROP;
    if (isDeprecatedCall) {
      // SubClass.inherits(SuperClass)
      subclass = callName.getFirstChild();
    } else if (callNode.getChildCount() == 3) {
      // goog.inherits(SubClass, SuperClass)
      subclass = callName.getNext();
    } else {
      return null;
    }

    if (type == SubclassType.MIXIN) {
      // Only consider mixins that mix two prototypes as related to
      // inheritance.
      if (!endsWithPrototype(superclass)) {
        return null;
      }
      if (!isDeprecatedCall) {
        if (!endsWithPrototype(subclass)) {
          return null;
        }
        // Strip off the prototype from the name.
        subclass = subclass.getFirstChild();
      }
      superclass = superclass.getFirstChild();
    }

    // bail out if either of the side of the "inherits"
    // isn't a real class name. This prevents us from
    // doing something weird in cases like:
    // goog.inherits(MySubClass, cond ? SuperClass1 : BaseClass2)
    if (subclass != null &&
        subclass.isUnscopedQualifiedName() &&
        superclass.isUnscopedQualifiedName()) {
      return new SubclassRelationship(type, subclass, superclass);
    }
  }

  return null;
}
 
Example 4
Source File: Nopol2017_0027_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * JsDoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      return false;
    } else if (!scope.isDeclared(qName, false) &&
        n.isUnscopedQualifiedName()) {

      // Check if this is in a conditional block.
      // Functions assigned in conditional blocks are inferred.
      for (Node current = n.getParent();
           !(current.isScript() || current.isFunction());
           current = current.getParent()) {
        if (NodeUtil.isControlStructure(current)) {
          return true;
        }
      }

      // Check if this is assigned in an inner scope.
      // Functions assigned in inner scopes are inferred.
      AstFunctionContents contents =
          getFunctionAnalysisResults(scope.getRootNode());
      if (contents == null ||
          !contents.getEscapedQualifiedNames().contains(qName)) {
        return false;
      }
    }
  }
  return inferred;
}
 
Example 5
Source File: Nopol2017_0027_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * JsDoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      return false;
    } else if (!scope.isDeclared(qName, false) &&
        n.isUnscopedQualifiedName()) {

      // Check if this is in a conditional block.
      // Functions assigned in conditional blocks are inferred.
      for (Node current = n.getParent();
           !(current.isScript() || current.isFunction());
           current = current.getParent()) {
        if (NodeUtil.isControlStructure(current)) {
          return true;
        }
      }

      // Check if this is assigned in an inner scope.
      // Functions assigned in inner scopes are inferred.
      AstFunctionContents contents =
          getFunctionAnalysisResults(scope.getRootNode());
      if (contents == null ||
          !contents.getEscapedQualifiedNames().contains(qName)) {
        return false;
      }
    }
  }
  return inferred;
}
 
Example 6
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * JsDoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      return false;
    } else if (!scope.isDeclared(qName, false) &&
        n.isUnscopedQualifiedName()) {

      // Check if this is in a conditional block.
      // Functions assigned in conditional blocks are inferred.
      for (Node current = n.getParent();
           !(current.isScript() || current.isFunction());
           current = current.getParent()) {
        if (NodeUtil.isControlStructure(current)) {
          return true;
        }
      }

      // Check if this is assigned in an inner scope.
      // Functions assigned in inner scopes are inferred.
      AstFunctionContents contents =
          getFunctionAnalysisResults(scope.getRootNode());
      if (contents == null ||
          !contents.getEscapedQualifiedNames().contains(qName)) {
        return false;
      }
    }
  }
  return inferred;
}
 
Example 7
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * JsDoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      return false;
    } else if (!scope.isDeclared(qName, false) &&
        n.isUnscopedQualifiedName()) {

      // Check if this is in a conditional block.
      // Functions assigned in conditional blocks are inferred.
      for (Node current = n.getParent();
           !(current.isScript() || current.isFunction());
           current = current.getParent()) {
        if (NodeUtil.isControlStructure(current)) {
          return true;
        }
      }

      // Check if this is assigned in an inner scope.
      // Functions assigned in inner scopes are inferred.
      AstFunctionContents contents =
          getFunctionAnalysisResults(scope.getRootNode());
      if (contents == null ||
          !contents.getEscapedQualifiedNames().contains(qName)) {
        return false;
      }
    }
  }
  return inferred;
}
 
Example 8
Source File: Closure_57_ClosureCodingConvention_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Understands several different inheritance patterns that occur in
 * Google code (various uses of {@code inherits} and {@code mixin}).
 */
@Override
public SubclassRelationship getClassesDefinedByCall(Node callNode) {
  Node callName = callNode.getFirstChild();
  SubclassType type = typeofClassDefiningName(callName);
  if (type != null) {
    Node subclass = null;
    Node superclass = callNode.getLastChild();

    // There are six possible syntaxes for a class-defining method:
    // SubClass.inherits(SuperClass)
    // goog.inherits(SubClass, SuperClass)
    // goog$inherits(SubClass, SuperClass)
    // SubClass.mixin(SuperClass.prototype)
    // goog.mixin(SubClass.prototype, SuperClass.prototype)
    // goog$mixin(SubClass.prototype, SuperClass.prototype)
    boolean isDeprecatedCall = callNode.getChildCount() == 2 &&
        callName.getType() == Token.GETPROP;
    if (isDeprecatedCall) {
      // SubClass.inherits(SuperClass)
      subclass = callName.getFirstChild();
    } else if (callNode.getChildCount() == 3) {
      // goog.inherits(SubClass, SuperClass)
      subclass = callName.getNext();
    } else {
      return null;
    }

    if (type == SubclassType.MIXIN) {
      // Only consider mixins that mix two prototypes as related to
      // inheritance.
      if (!endsWithPrototype(superclass)) {
        return null;
      }
      if (!isDeprecatedCall) {
        if (!endsWithPrototype(subclass)) {
          return null;
        }
        // Strip off the prototype from the name.
        subclass = subclass.getFirstChild();
      }
      superclass = superclass.getFirstChild();
    }

    // bail out if either of the side of the "inherits"
    // isn't a real class name. This prevents us from
    // doing something weird in cases like:
    // goog.inherits(MySubClass, cond ? SuperClass1 : BaseClass2)
    if (subclass != null &&
        subclass.isUnscopedQualifiedName() &&
        superclass.isUnscopedQualifiedName()) {
      return new SubclassRelationship(type, subclass, superclass);
    }
  }

  return null;
}
 
Example 9
Source File: Closure_57_ClosureCodingConvention_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Understands several different inheritance patterns that occur in
 * Google code (various uses of {@code inherits} and {@code mixin}).
 */
@Override
public SubclassRelationship getClassesDefinedByCall(Node callNode) {
  Node callName = callNode.getFirstChild();
  SubclassType type = typeofClassDefiningName(callName);
  if (type != null) {
    Node subclass = null;
    Node superclass = callNode.getLastChild();

    // There are six possible syntaxes for a class-defining method:
    // SubClass.inherits(SuperClass)
    // goog.inherits(SubClass, SuperClass)
    // goog$inherits(SubClass, SuperClass)
    // SubClass.mixin(SuperClass.prototype)
    // goog.mixin(SubClass.prototype, SuperClass.prototype)
    // goog$mixin(SubClass.prototype, SuperClass.prototype)
    boolean isDeprecatedCall = callNode.getChildCount() == 2 &&
        callName.getType() == Token.GETPROP;
    if (isDeprecatedCall) {
      // SubClass.inherits(SuperClass)
      subclass = callName.getFirstChild();
    } else if (callNode.getChildCount() == 3) {
      // goog.inherits(SubClass, SuperClass)
      subclass = callName.getNext();
    } else {
      return null;
    }

    if (type == SubclassType.MIXIN) {
      // Only consider mixins that mix two prototypes as related to
      // inheritance.
      if (!endsWithPrototype(superclass)) {
        return null;
      }
      if (!isDeprecatedCall) {
        if (!endsWithPrototype(subclass)) {
          return null;
        }
        // Strip off the prototype from the name.
        subclass = subclass.getFirstChild();
      }
      superclass = superclass.getFirstChild();
    }

    // bail out if either of the side of the "inherits"
    // isn't a real class name. This prevents us from
    // doing something weird in cases like:
    // goog.inherits(MySubClass, cond ? SuperClass1 : BaseClass2)
    if (subclass != null &&
        subclass.isUnscopedQualifiedName() &&
        superclass.isUnscopedQualifiedName()) {
      return new SubclassRelationship(type, subclass, superclass);
    }
  }

  return null;
}
 
Example 10
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * JsDoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      return false;
    } else if (!scope.isDeclared(qName, false) &&
        n.isUnscopedQualifiedName()) {

      // Check if this is in a conditional block.
      // Functions assigned in conditional blocks are inferred.
      for (Node current = n.getParent();
           !(current.isScript() || current.isFunction());
           current = current.getParent()) {
        if (NodeUtil.isControlStructure(current)) {
          return true;
        }
      }

      // Check if this is assigned in an inner scope.
      // Functions assigned in inner scopes are inferred.
      AstFunctionContents contents =
          getFunctionAnalysisResults(scope.getRootNode());
      if (contents == null ||
          !contents.getEscapedQualifiedNames().contains(qName)) {
        return false;
      }
    }
  }
  return inferred;
}
 
Example 11
Source File: ClosureCodingConvention.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Understands several different inheritance patterns that occur in
 * Google code (various uses of {@code inherits} and {@code mixin}).
 */
@Override
public SubclassRelationship getClassesDefinedByCall(Node callNode) {
  SubclassRelationship relationship =
      super.getClassesDefinedByCall(callNode);
  if (relationship != null) return relationship;

  Node callName = callNode.getFirstChild();
  SubclassType type = typeofClassDefiningName(callName);
  if (type != null) {
    Node subclass = null;
    Node superclass = callNode.getLastChild();

    // There are six possible syntaxes for a class-defining method:
    // SubClass.inherits(SuperClass)
    // goog.inherits(SubClass, SuperClass)
    // goog$inherits(SubClass, SuperClass)
    // SubClass.mixin(SuperClass.prototype)
    // goog.mixin(SubClass.prototype, SuperClass.prototype)
    // goog$mixin(SubClass.prototype, SuperClass.prototype)
    boolean isDeprecatedCall = callNode.getChildCount() == 2 &&
        callName.isGetProp();
    if (isDeprecatedCall) {
      // SubClass.inherits(SuperClass)
      subclass = callName.getFirstChild();
    } else if (callNode.getChildCount() == 3) {
      // goog.inherits(SubClass, SuperClass)
      subclass = callName.getNext();
    } else {
      return null;
    }

    if (type == SubclassType.MIXIN) {
      // Only consider mixins that mix two prototypes as related to
      // inheritance.
      if (!endsWithPrototype(superclass)) {
        return null;
      }
      if (!isDeprecatedCall) {
        if (!endsWithPrototype(subclass)) {
          return null;
        }
        // Strip off the prototype from the name.
        subclass = subclass.getFirstChild();
      }
      superclass = superclass.getFirstChild();
    }

    // bail out if either of the side of the "inherits"
    // isn't a real class name. This prevents us from
    // doing something weird in cases like:
    // goog.inherits(MySubClass, cond ? SuperClass1 : BaseClass2)
    if (subclass != null &&
        subclass.isUnscopedQualifiedName() &&
        superclass.isUnscopedQualifiedName()) {
      return new SubclassRelationship(type, subclass, superclass);
    }
  }

  return null;
}
 
Example 12
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 3 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * jsdoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      inferred = false;
    } else if (!scope.isDeclared(qName, false) &&
               n.isUnscopedQualifiedName()) {
      inferred = false;
    }
  }
  return inferred;
}
 
Example 13
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Determines whether a qualified name is inferred.
 * NOTE(nicksantos): Determining whether a property is declared or not
 * is really really obnoxious.
 *
 * The problem is that there are two (equally valid) coding styles:
 *
 * (function() {
 *   /* The authoritative definition of goog.bar. /
 *   goog.bar = function() {};
 * })();
 *
 * function f() {
 *   goog.bar();
 *   /* Reset goog.bar to a no-op. /
 *   goog.bar = function() {};
 * }
 *
 * In a dynamic language with first-class functions, it's very difficult
 * to know which one the user intended without looking at lots of
 * contextual information (the second example demonstrates a small case
 * of this, but there are some really pathological cases as well).
 *
 * The current algorithm checks if either the declaration has
 * jsdoc type information, or @const with a known type,
 * or a function literal with a name we haven't seen before.
 */
private boolean isQualifiedNameInferred(
    String qName, Node n, JSDocInfo info,
    Node rhsValue, JSType valueType) {
  if (valueType == null) {
    return true;
  }

  boolean inferred = true;
  if (info != null) {
    inferred = !(info.hasType()
        || info.hasEnumParameterType()
        || (info.isConstant() && valueType != null
            && !valueType.isUnknownType())
        || FunctionTypeBuilder.isFunctionTypeDeclaration(info));
  }

  if (inferred && rhsValue != null && rhsValue.isFunction()) {
    if (info != null) {
      inferred = false;
    } else if (!scope.isDeclared(qName, false) &&
               n.isUnscopedQualifiedName()) {
      inferred = false;
    }
  }
  return inferred;
}