Skip to content

Commit

Permalink
[NTI] Bugfix for the banned-property conformance check.
Browse files Browse the repository at this point in the history
Fixes #2280 on github.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=147058204
  • Loading branch information
dimvar authored and blickly committed Feb 10, 2017
1 parent 7bd84ba commit 85764a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/com/google/javascript/jscomp/ConformanceRules.java
Expand Up @@ -37,7 +37,6 @@
import com.google.javascript.rhino.TypeI; import com.google.javascript.rhino.TypeI;
import com.google.javascript.rhino.TypeIRegistry; import com.google.javascript.rhino.TypeIRegistry;
import com.google.javascript.rhino.jstype.JSTypeNative; import com.google.javascript.rhino.jstype.JSTypeNative;

import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
Expand All @@ -46,7 +45,6 @@
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;

import javax.annotation.Nullable; import javax.annotation.Nullable;


/** /**
Expand Down Expand Up @@ -502,23 +500,26 @@ protected ConformanceResult checkConformance(NodeTraversal t, Node n) {
private ConformanceResult checkConformance(NodeTraversal t, Node n, Property prop) { private ConformanceResult checkConformance(NodeTraversal t, Node n, Property prop) {
if (isCandidatePropUse(n, prop)) { if (isCandidatePropUse(n, prop)) {
TypeIRegistry registry = t.getCompiler().getTypeIRegistry(); TypeIRegistry registry = t.getCompiler().getTypeIRegistry();
TypeI methodClassType = registry.getType(prop.type); TypeI typeWithBannedProp = registry.getType(prop.type);
Node lhs = n.getFirstChild(); Node lhs = n.getFirstChild();
if (methodClassType != null && lhs.getTypeI() != null) { if (typeWithBannedProp != null && lhs.getTypeI() != null) {
TypeI targetType = lhs.getTypeI().restrictByNotNullOrUndefined(); TypeI foundType = lhs.getTypeI().restrictByNotNullOrUndefined();
if (targetType.isSomeUnknownType() if (foundType.toMaybeObjectType() != null
|| targetType.isTypeVariable() && foundType.toMaybeObjectType().isGeneric()) {
|| targetType.isBottom() foundType = foundType.toMaybeObjectType().getRawType();
|| targetType.isTop() }
|| targetType.isEquivalentTo( if (foundType.isSomeUnknownType()
registry.getNativeType(JSTypeNative.OBJECT_TYPE))) { || foundType.isTypeVariable()
|| foundType.isBottom()
|| foundType.isTop()
|| foundType.isEquivalentTo(registry.getNativeType(JSTypeNative.OBJECT_TYPE))) {
if (reportLooseTypeViolations) { if (reportLooseTypeViolations) {
return ConformanceResult.POSSIBLE_VIOLATION_DUE_TO_LOOSE_TYPES; return ConformanceResult.POSSIBLE_VIOLATION_DUE_TO_LOOSE_TYPES;
} }
} else if (targetType.isSubtypeOf(methodClassType)) { } else if (foundType.isSubtypeOf(typeWithBannedProp)) {
return ConformanceResult.VIOLATION; return ConformanceResult.VIOLATION;
} else if (methodClassType.isSubtypeOf(targetType)) { } else if (typeWithBannedProp.isSubtypeOf(foundType)) {
if (matchesPrototype(methodClassType, targetType)) { if (matchesPrototype(typeWithBannedProp, foundType)) {
return ConformanceResult.VIOLATION; return ConformanceResult.VIOLATION;
} else if (reportLooseTypeViolations) { } else if (reportLooseTypeViolations) {
// Access of a banned property through a super class may be a violation // Access of a banned property through a super class may be a violation
Expand Down
11 changes: 11 additions & 0 deletions test/com/google/javascript/jscomp/CheckConformanceTest.java
Expand Up @@ -622,6 +622,17 @@ public void testBannedProperty4() {
CheckConformance.CONFORMANCE_POSSIBLE_VIOLATION); CheckConformance.CONFORMANCE_POSSIBLE_VIOLATION);
} }


public void testBannedProperty5() {
configuration = LINE_JOINER.join(
"requirement: {",
" type: BANNED_PROPERTY",
" value: 'Array.prototype.push'",
" error_message: 'banned Array.prototype.push'",
"}");

testConformance("[1, 2, 3].push(4);\n", CheckConformance.CONFORMANCE_VIOLATION);
}

public void testBannedPropertyWrite() { public void testBannedPropertyWrite() {
configuration = configuration =
"requirement: {\n" + "requirement: {\n" +
Expand Down

0 comments on commit 85764a1

Please sign in to comment.