Skip to content

Commit

Permalink
Ignore unknown type in RESTRICTED_PROPERTY_WRITE.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=228831794
  • Loading branch information
vrana authored and EatingW committed Jan 11, 2019
1 parent 48c9a0c commit a665768
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/com/google/javascript/jscomp/ConformanceRules.java
Expand Up @@ -703,6 +703,10 @@ static boolean isCallTarget(Node n) {
&& parent.getFirstChild() == n;
}

static boolean isLooseType(JSType type) {
return type.isUnknownType() || type.isUnresolved() || type.isAllType();
}

static JSType evaluateTypeString(
AbstractCompiler compiler, String expression)
throws InvalidRequirementSpec {
Expand Down Expand Up @@ -969,9 +973,7 @@ private ConformanceResult checkConformance(
Node lhs = isCallInvocation ? n.getFirstFirstChild() : n.getFirstChild();
if (methodClassType != null && lhs.getJSType() != null) {
JSType targetType = lhs.getJSType().restrictByNotNullOrUndefined();
if (targetType.isUnknownType()
|| targetType.isUnresolved()
|| targetType.isAllType()
if (ConformanceUtil.isLooseType(targetType)
|| targetType.isEquivalentTo(registry.getNativeType(JSTypeNative.OBJECT_TYPE))) {
if (reportLooseTypeViolations
&& !ConformanceUtil.validateCall(
Expand Down Expand Up @@ -1038,18 +1040,25 @@ private static class Restriction {
@Override
protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
if (n.isGetProp() && NodeUtil.isLhsOfAssign(n)) {
JSTypeRegistry registry = t.getCompiler().getTypeRegistry();
JSType rhsType = n.getNext().getJSType();
JSType targetType = n.getFirstChild().getJSType();
if (rhsType != null && targetType != null) {
JSType targetNotNullType = null;
for (Restriction r : restrictions) {
if (n.getLastChild().getString() == r.property) { // Both strings are interned.
if (targetNotNullType == null) {
targetNotNullType = targetType.restrictByNotNullOrUndefined();
}
if (targetNotNullType.isSubtypeOf(r.type) && !rhsType.isSubtypeOf(r.restrictedType)) {
return ConformanceResult.VIOLATION;
if (!rhsType.isSubtypeOf(r.restrictedType)) {
if (ConformanceUtil.isLooseType(targetType)) {
if (reportLooseTypeViolations) {
return ConformanceResult.POSSIBLE_VIOLATION_DUE_TO_LOOSE_TYPES;
}
} else {
if (targetNotNullType == null) {
targetNotNullType = targetType.restrictByNotNullOrUndefined();
}
if (targetNotNullType.isSubtypeOf(r.type)) {
return ConformanceResult.VIOLATION;
}
}
}
}
}
Expand Down Expand Up @@ -1670,7 +1679,7 @@ private ConformanceResult checkCreateElement(Node n) {
return ConformanceResult.VIOLATION;
}
JSType type = srcObj.getJSType();
if (type == null || type.isUnknownType() || type.isUnresolved() || type.isAllType()) {
if (type == null || ConformanceUtil.isLooseType(type)) {
return reportLooseTypeViolations
? ConformanceResult.POSSIBLE_VIOLATION_DUE_TO_LOOSE_TYPES
: ConformanceResult.CONFORMANCE;
Expand Down
7 changes: 7 additions & 0 deletions test/com/google/javascript/jscomp/CheckConformanceTest.java
Expand Up @@ -1045,6 +1045,13 @@ public void testRestrictedPropertyWrite() {

testWarning(code + "b.x = 'a'", CheckConformance.CONFORMANCE_VIOLATION);
testNoWarning(code + "b.x = 1");
testNoWarning(code + "var a = {}; a.x = 'a'");
testWarning(
code + "/** @type {?} */ var a = {}; a.x = 'a'",
CheckConformance.CONFORMANCE_POSSIBLE_VIOLATION);
testWarning(
code + "/** @type {*} */ var a = {}; a.x = 'a'",
CheckConformance.CONFORMANCE_POSSIBLE_VIOLATION);
}

@Test
Expand Down

0 comments on commit a665768

Please sign in to comment.