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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#differsFrom() . 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_25_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSTypeNative assertedType = assertionFunctionSpec.getAssertedType();
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(getNativeType(assertedType));
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  if (getJSType(callNode).differsFrom(narrowed)) {
    callNode.setJSType(narrowed);
  }
  return scope;
}
 
Example 2
Source File: Closure_25_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSTypeNative assertedType = assertionFunctionSpec.getAssertedType();
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(getNativeType(assertedType));
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  if (getJSType(callNode).differsFrom(narrowed)) {
    callNode.setJSType(narrowed);
  }
  return scope;
}
 
Example 3
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSType assertedType = assertionFunctionSpec.getAssertedType(
      callNode, registry);
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(assertedType);
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  callNode.setJSType(narrowed);
  return scope;
}
 
Example 4
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSType assertedType = assertionFunctionSpec.getAssertedType(
      callNode, registry);
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(assertedType);
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  callNode.setJSType(narrowed);
  return scope;
}
 
Example 5
Source File: TypeInference.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private FlowScope tightenTypesAfterAssertions(FlowScope scope,
    Node callNode) {
  Node left = callNode.getFirstChild();
  Node firstParam = left.getNext();
  AssertionFunctionSpec assertionFunctionSpec =
      assertionFunctionsMap.get(left.getQualifiedName());
  if (assertionFunctionSpec == null || firstParam == null) {
    return scope;
  }
  Node assertedNode = assertionFunctionSpec.getAssertedParam(firstParam);
  if (assertedNode == null) {
    return scope;
  }
  JSType assertedType = assertionFunctionSpec.getAssertedType(
      callNode, registry);
  String assertedNodeName = assertedNode.getQualifiedName();

  JSType narrowed;
  // Handle assertions that enforce expressions evaluate to true.
  if (assertedType == null) {
    // Handle arbitrary expressions within the assert.
    scope = reverseInterpreter.getPreciserScopeKnowingConditionOutcome(
        assertedNode, scope, true);
    // Build the result of the assertExpression
    narrowed = getJSType(assertedNode).restrictByNotNullOrUndefined();
  } else {
    // Handle assertions that enforce expressions are of a certain type.
    JSType type = getJSType(assertedNode);
    narrowed = type.getGreatestSubtype(assertedType);
    if (assertedNodeName != null && type.differsFrom(narrowed)) {
      scope = narrowScope(scope, assertedNode, narrowed);
    }
  }

  callNode.setJSType(narrowed);
  return scope;
}
 
Example 6
Source File: Closure_71_CheckAccessControls_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property is visible in the current context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t,
    Node getprop, Node parent) {
  ObjectType objectType =
      ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  if (objectType != null) {
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = t.inGlobalScope() &&
        parent.getType() == Token.ASSIGN &&
        parent.getFirstChild() == getprop;

    // Find the lowest property defined on a class with visibility
    // information.
    if (isOverride) {
      objectType = objectType.getImplicitPrototype();
    }
    JSDocInfo docInfo = null;
    for (; objectType != null;
         objectType = objectType.getImplicitPrototype()) {
      docInfo = objectType.getOwnPropertyJSDocInfo(propertyName);
      if (docInfo != null &&
          docInfo.getVisibility() != Visibility.INHERITED) {
        break;
      }
    }

    if (objectType == null) {
      // We couldn't find a visibility modifier; assume it's public.
      return;
    }

    boolean sameInput =
        t.getInput().getName().equals(docInfo.getSourceName());
    Visibility visibility = docInfo.getVisibility();
    JSType ownerType = normalizeClassType(objectType);
    if (isOverride) {
      // Check an ASSIGN statement that's trying to override a property
      // on a superclass.
      JSDocInfo overridingInfo = parent.getJSDocInfo();
      Visibility overridingVisibility = overridingInfo == null ?
          Visibility.INHERITED : overridingInfo.getVisibility();

      // Check that (a) the property *can* be overridden, and
      // (b) that the visibility of the override is the same as the
      // visibility of the original property.
      if (visibility == Visibility.PRIVATE && !sameInput) {
        compiler.report(
            t.makeError(getprop, PRIVATE_OVERRIDE,
                objectType.toString()));
      } else if (overridingVisibility != Visibility.INHERITED &&
          overridingVisibility != visibility) {
        compiler.report(
            t.makeError(getprop, VISIBILITY_MISMATCH,
                visibility.name(), objectType.toString(),
                overridingVisibility.name()));
      }
    } else {
      if (sameInput) {
        // private access is always allowed in the same file.
        return;
      } else if (visibility == Visibility.PRIVATE &&
          (currentClass == null || ownerType.differsFrom(currentClass))) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        // private access is not allowed outside the file from a different
        // enclosing class.
        compiler.report(
            t.makeError(getprop,
                BAD_PRIVATE_PROPERTY_ACCESS,
                propertyName,
                validator.getReadableJSTypeName(
                    getprop.getFirstChild(), true)));
      } else if (visibility == Visibility.PROTECTED) {
        // There are 3 types of legal accesses of a protected property:
        // 1) Accesses in the same file
        // 2) Overriding the property in a subclass
        // 3) Accessing the property from inside a subclass
        // The first two have already been checked for.
        if (currentClass == null || !currentClass.isSubtype(ownerType)) {
          compiler.report(
              t.makeError(getprop,  BAD_PROTECTED_PROPERTY_ACCESS,
                  propertyName,
                  validator.getReadableJSTypeName(
                      getprop.getFirstChild(), true)));
        }
      }
    }
  }
}
 
Example 7
Source File: Closure_71_CheckAccessControls_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Determines whether the given property is visible in the current context.
 * @param t The current traversal.
 * @param getprop The getprop node.
 */
private void checkPropertyVisibility(NodeTraversal t,
    Node getprop, Node parent) {
  ObjectType objectType =
      ObjectType.cast(dereference(getprop.getFirstChild().getJSType()));
  String propertyName = getprop.getLastChild().getString();

  if (objectType != null) {
    // Is this a normal property access, or are we trying to override
    // an existing property?
    boolean isOverride = parent.getJSDocInfo() != null &&
        parent.getType() == Token.ASSIGN &&
        parent.getFirstChild() == getprop;

    // Find the lowest property defined on a class with visibility
    // information.
    if (isOverride) {
      objectType = objectType.getImplicitPrototype();
    }
    JSDocInfo docInfo = null;
    for (; objectType != null;
         objectType = objectType.getImplicitPrototype()) {
      docInfo = objectType.getOwnPropertyJSDocInfo(propertyName);
      if (docInfo != null &&
          docInfo.getVisibility() != Visibility.INHERITED) {
        break;
      }
    }

    if (objectType == null) {
      // We couldn't find a visibility modifier; assume it's public.
      return;
    }

    boolean sameInput =
        t.getInput().getName().equals(docInfo.getSourceName());
    Visibility visibility = docInfo.getVisibility();
    JSType ownerType = normalizeClassType(objectType);
    if (isOverride) {
      // Check an ASSIGN statement that's trying to override a property
      // on a superclass.
      JSDocInfo overridingInfo = parent.getJSDocInfo();
      Visibility overridingVisibility = overridingInfo == null ?
          Visibility.INHERITED : overridingInfo.getVisibility();

      // Check that (a) the property *can* be overridden, and
      // (b) that the visibility of the override is the same as the
      // visibility of the original property.
      if (visibility == Visibility.PRIVATE && !sameInput) {
        compiler.report(
            t.makeError(getprop, PRIVATE_OVERRIDE,
                objectType.toString()));
      } else if (overridingVisibility != Visibility.INHERITED &&
          overridingVisibility != visibility) {
        compiler.report(
            t.makeError(getprop, VISIBILITY_MISMATCH,
                visibility.name(), objectType.toString(),
                overridingVisibility.name()));
      }
    } else {
      if (sameInput) {
        // private access is always allowed in the same file.
        return;
      } else if (visibility == Visibility.PRIVATE &&
          (currentClass == null || ownerType.differsFrom(currentClass))) {
        if (docInfo.isConstructor() &&
            isValidPrivateConstructorAccess(parent)) {
          return;
        }

        // private access is not allowed outside the file from a different
        // enclosing class.
        compiler.report(
            t.makeError(getprop,
                BAD_PRIVATE_PROPERTY_ACCESS,
                propertyName,
                validator.getReadableJSTypeName(
                    getprop.getFirstChild(), true)));
      } else if (visibility == Visibility.PROTECTED) {
        // There are 3 types of legal accesses of a protected property:
        // 1) Accesses in the same file
        // 2) Overriding the property in a subclass
        // 3) Accessing the property from inside a subclass
        // The first two have already been checked for.
        if (currentClass == null || !currentClass.isSubtype(ownerType)) {
          compiler.report(
              t.makeError(getprop,  BAD_PROTECTED_PROPERTY_ACCESS,
                  propertyName,
                  validator.getReadableJSTypeName(
                      getprop.getFirstChild(), true)));
        }
      }
    }
  }
}