From a665768638f7eca20a6842f072531146576c0bac Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Thu, 10 Jan 2019 22:10:21 -0800 Subject: [PATCH] Ignore unknown type in RESTRICTED_PROPERTY_WRITE. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=228831794 --- .../javascript/jscomp/ConformanceRules.java | 29 ++++++++++++------- .../jscomp/CheckConformanceTest.java | 7 +++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/com/google/javascript/jscomp/ConformanceRules.java b/src/com/google/javascript/jscomp/ConformanceRules.java index 03b90a002d8..85edc471ab2 100644 --- a/src/com/google/javascript/jscomp/ConformanceRules.java +++ b/src/com/google/javascript/jscomp/ConformanceRules.java @@ -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 { @@ -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( @@ -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; + } + } } } } @@ -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; diff --git a/test/com/google/javascript/jscomp/CheckConformanceTest.java b/test/com/google/javascript/jscomp/CheckConformanceTest.java index c727b371145..57cf54a02ae 100644 --- a/test/com/google/javascript/jscomp/CheckConformanceTest.java +++ b/test/com/google/javascript/jscomp/CheckConformanceTest.java @@ -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