Skip to content

Commit

Permalink
[NTI] Convert RemoveSuperMethodsPass to TypeI and test in NTI.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=156584284
  • Loading branch information
shicks authored and Tyler Breisacher committed May 20, 2017
1 parent f087bae commit 5b59091
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 10 additions & 10 deletions src/com/google/javascript/jscomp/RemoveSuperMethodsPass.java
Expand Up @@ -16,9 +16,9 @@
package com.google.javascript.jscomp; package com.google.javascript.jscomp;


import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.FunctionTypeI;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.jstype.FunctionType; import com.google.javascript.rhino.TypeI;
import com.google.javascript.rhino.jstype.JSType;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;


Expand Down Expand Up @@ -127,17 +127,17 @@ private boolean functionNameMatches(String enclosingMethodName, Node call) {


// Check that call references the superclass // Check that call references the superclass
String calledClass = callNameSplittedByPrototypeMarker[0]; String calledClass = callNameSplittedByPrototypeMarker[0];
JSType subclassType = compiler.getTypeIRegistry().getType(enclosingClassName); TypeI subclassType = compiler.getTypeIRegistry().getType(enclosingClassName);
JSType calledClassType = compiler.getTypeIRegistry().getType(calledClass); TypeI calledClassType = compiler.getTypeIRegistry().getType(calledClass);
if (subclassType == null || calledClassType == null) { if (subclassType == null || calledClassType == null) {
return false; return false;
} }
if (subclassType.toObjectType() == null if (subclassType.toMaybeObjectType() == null
|| subclassType.toObjectType().getConstructor() == null) { || subclassType.toMaybeObjectType().getConstructor() == null) {
return false; return false;
} }
FunctionType superClassConstructor = FunctionTypeI superClassConstructor =
subclassType.toObjectType().getConstructor().getSuperClassConstructor(); subclassType.toMaybeObjectType().getSuperClassConstructor();
// TODO(moz): Investigate why this could be null // TODO(moz): Investigate why this could be null
if (superClassConstructor == null) { if (superClassConstructor == null) {
return false; return false;
Expand All @@ -147,13 +147,13 @@ private boolean functionNameMatches(String enclosingMethodName, Node call) {


private boolean returnMatches(Node call) { private boolean returnMatches(Node call) {
// no match if function being called does not have function type // no match if function being called does not have function type
JSType childType = call.getFirstChild().getJSType(); TypeI childType = call.getFirstChild().getTypeI();
if (childType == null || !childType.isFunctionType()) { if (childType == null || !childType.isFunctionType()) {
return false; return false;
} }
// no match if function being called has a return value, but result of the call is not part // no match if function being called has a return value, but result of the call is not part
// of return statement // of return statement
JSType returnType = childType.toMaybeFunctionType().getReturnType(); TypeI returnType = childType.toMaybeFunctionType().getReturnType();
if (returnType != null && !returnType.isVoidType() && !returnType.isUnknownType()) { if (returnType != null && !returnType.isVoidType() && !returnType.isUnknownType()) {
return call.getParent().isReturn(); return call.getParent().isReturn();
} }
Expand Down
Expand Up @@ -18,7 +18,7 @@
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;


/** Tests for {@link RemoveSuperMethodsPass} */ /** Tests for {@link RemoveSuperMethodsPass} */
public final class RemoveSuperMethodsPassTest extends CompilerTestCase { public final class RemoveSuperMethodsPassTest extends TypeICompilerTestCase {


private static final String BOILERPLATE = private static final String BOILERPLATE =
LINE_JOINER.join( LINE_JOINER.join(
Expand All @@ -37,15 +37,14 @@ public final class RemoveSuperMethodsPassTest extends CompilerTestCase {
"/** @constructor @extends {FooBase} */", "/** @constructor @extends {FooBase} */",
"var Foo = function() {}", "var Foo = function() {}",
"Foo.superClass_ = FooBase.prototype", "Foo.superClass_ = FooBase.prototype",
"var ns = {};", "/** @const */ var ns = {};",
"/** @constructor */ ns.FooBase = function() {};", "/** @constructor */ ns.FooBase = function() {};",
"ns.FooBase.prototype.bar = function() {};", "ns.FooBase.prototype.bar = function() {};",
"/** @constructor @extends {ns.FooBase} */ ns.Foo = function() {};", "/** @constructor @extends {ns.FooBase} */ ns.Foo = function() {};",
"ns.Foo.superClass_ = ns.FooBase.prototype"); "ns.Foo.superClass_ = ns.FooBase.prototype");


public RemoveSuperMethodsPassTest() { public RemoveSuperMethodsPassTest() {
super(DEFAULT_EXTERNS); super(DEFAULT_EXTERNS);
enableTypeCheck();
} }


@Override @Override
Expand All @@ -68,7 +67,7 @@ public void testOptimize_noArgs() {
"Foo.prototype.bar = function() { Foo.superClass_.bar.call(this); };")); "Foo.prototype.bar = function() { Foo.superClass_.bar.call(this); };"));
} }


public void testOptimize_baseClassName_noArgs() { public void testOptimize_noArgs_baseClassName() {
testOptimize( testOptimize(
LINE_JOINER.join( LINE_JOINER.join(
"/** @override */", "/** @override */",
Expand Down Expand Up @@ -160,13 +159,15 @@ public void testNoOptimize_methodNameMismatch() {
testNoOptimize( testNoOptimize(
LINE_JOINER.join( LINE_JOINER.join(
"Foo.prototype.baw = {", "Foo.prototype.baw = {",
" superClass_: { baz: /** @return {number} */ function(time, loc) {} }", " superClass_: { baz: /** @return {number} */ function(time, loc) { return 3; } }",
"};", "};",
"/** @override */", "/** @override */",
"Foo.prototype.baz = function(time, loc) {", "Foo.prototype.baz = function(time, loc) {",
" return Foo.prototype.baw.superClass_.baz.call(this, time, loc);", " return Foo.prototype.baw.superClass_.baz.call(this, time, loc);",
"};")); "};"));


ignoreWarnings(
NewTypeInference.INEXISTENT_PROPERTY, NewTypeInference.UNKNOWN_NAMESPACE_PROPERTY);
testNoOptimize( testNoOptimize(
LINE_JOINER.join( LINE_JOINER.join(
"Foo.prototype.baw = {};", "Foo.prototype.baw = {};",
Expand Down Expand Up @@ -201,6 +202,7 @@ public void testNoOptimize_moreThanOneStatement() {
} }


public void testNoOptimize_missingReturn() { public void testNoOptimize_missingReturn() {
ignoreWarnings(NewTypeInference.MISSING_RETURN_STATEMENT);
testNoOptimize( testNoOptimize(
LINE_JOINER.join( LINE_JOINER.join(
"/** @override */", "/** @override */",
Expand Down

0 comments on commit 5b59091

Please sign in to comment.