Skip to content

Commit

Permalink
[NTI] Convert TemplateAstMatcher to use TypeI.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129462971
  • Loading branch information
aravind-pg authored and Dimitris Vardoulakis committed Aug 5, 2016
1 parent 36e7bca commit 0514ae2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
26 changes: 12 additions & 14 deletions src/com/google/javascript/jscomp/TemplateAstMatcher.java
Expand Up @@ -23,9 +23,8 @@
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.TypeI;
import com.google.javascript.rhino.TypeIRegistry;
import com.google.javascript.rhino.jstype.JSType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -76,7 +75,7 @@ public final class TemplateAstMatcher {
private boolean isLooseMatch = false;

/**
* The strategy to use when matching the {@code JSType} of nodes.
* The strategy to use when matching the {@code TypeI} of nodes.
*/
private final TypeMatchingStrategy typeMatchingStrategy;

Expand Down Expand Up @@ -187,7 +186,7 @@ private Node initTemplate(Node templateFunctionNode) {
private void prepTemplatePlaceholders(Node fn) {
final List<String> locals = templateLocals;
final List<String> params = templateParams;
final Map<String, JSType> paramTypes = new HashMap<>();
final Map<String, TypeI> paramTypes = new HashMap<>();

// drop the function name so it isn't include in the name maps
String fnName = fn.getFirstChild().getString();
Expand All @@ -206,7 +205,7 @@ private void prepTemplatePlaceholders(Node fn) {
Preconditions.checkNotNull(expression,
"Missing JSDoc for parameter %s of template function %s",
name, fnName);
JSType type = expression.evaluate(null, typeRegistry);
TypeI type = typeRegistry.evaluateTypeExpressionInGlobalScope(expression);
Preconditions.checkNotNull(type);
params.add(name);
paramTypes.put(name, type);
Expand All @@ -224,7 +223,7 @@ public void visit(Node n) {
}

if (params.contains(name)) {
JSType type = paramTypes.get(name);
TypeI type = paramTypes.get(name);
replaceNodeInPlace(n,
createTemplateParameterNode(params.indexOf(name), type));
} else if (locals.contains(name)) {
Expand Down Expand Up @@ -270,12 +269,12 @@ private boolean isTemplateParameterNode(Node n) {
return (n.getType() == TEMPLATE_TYPE_PARAM);
}

private Node createTemplateParameterNode(int index, JSType type) {
private Node createTemplateParameterNode(int index, TypeI type) {
Preconditions.checkState(index >= 0);
Preconditions.checkNotNull(type);
Node n = Node.newNumber(index);
n.setType(TEMPLATE_TYPE_PARAM);
n.setJSType(type);
n.setTypeI(type);
return n;
}

Expand Down Expand Up @@ -376,7 +375,7 @@ private boolean matchesNode(Node template, Node ast) {

// Only the types need to match for the template parameters, which allows
// the template function to express arbitrary expressions.
JSType templateType = template.getJSType();
TypeI templateType = template.getTypeI();

Preconditions.checkNotNull(templateType, "null template parameter type.");

Expand All @@ -387,7 +386,7 @@ private boolean matchesNode(Node template, Node ast) {
return false;
}

MatchResult matchResult = typeMatchingStrategy.match(templateType, ast.getJSType());
MatchResult matchResult = typeMatchingStrategy.match(templateType, ast.getTypeI());
isLooseMatch = matchResult.isLooseMatch();
boolean isMatch = matchResult.isMatch();
if (isMatch && previousMatch == null) {
Expand Down Expand Up @@ -437,18 +436,17 @@ private boolean matchesNode(Node template, Node ast) {
return true;
}

private boolean isUnresolvedType(JSType type) {
private boolean isUnresolvedType(TypeI type) {
// TODO(mknichel): When types are used in templates that do not appear in the
// compilation unit being processed, the template type will be a named type
// that resolves to unknown instead of being a no resolved type. This should
// be fixed in the compiler such that it resolves to a no resolved type, and
// then this code can be simplified to use that.
if (type.isNoResolvedType()
|| (type.isNamedType() && type.isUnknownType())) {
if (type.isUnresolvedOrResolvedUnknown()) {
return true;
}
if (type.isUnionType()) {
for (JSType alternate : type.toMaybeUnionType().getAlternates()) {
for (TypeI alternate : type.getUnionMembers()) {
if (isUnresolvedType(alternate)) {
return true;
}
Expand Down
10 changes: 5 additions & 5 deletions src/com/google/javascript/jscomp/TypeMatchingStrategy.java
Expand Up @@ -16,10 +16,10 @@

package com.google.javascript.jscomp;

import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.TypeI;

/**
* The different strategies for matching the {@code JSType} of nodes.
* The different strategies for matching the {@code TypeI} of nodes.
*/
public enum TypeMatchingStrategy {

Expand Down Expand Up @@ -58,7 +58,7 @@ private TypeMatchingStrategy(
this.allowLooseMatches = allowLooseMatches;
}

public MatchResult match(JSType templateType, JSType type) {
public MatchResult match(TypeI templateType, TypeI type) {
if (templateType.isUnknownType()) {
// If the template type is '?', then any type is a match and this is not considered a loose
// match.
Expand All @@ -73,7 +73,7 @@ public MatchResult match(JSType templateType, JSType type) {
if (ignoreNullability) {
type = type.restrictByNotNullOrUndefined();
}
if (type.isSubtype(templateType)) {
if (type.isSubtypeOf(templateType)) {
return new MatchResult(true, false);
}
}
Expand All @@ -88,7 +88,7 @@ public MatchResult match(JSType templateType, JSType type) {
}

/**
* The result of comparing two different {@code JSType} instances.
* The result of comparing two different {@code TypeI} instances.
*/
public static class MatchResult {
private final boolean isMatch;
Expand Down
5 changes: 5 additions & 0 deletions src/com/google/javascript/jscomp/newtypes/JSType.java
Expand Up @@ -1544,6 +1544,11 @@ public boolean isNoResolvedType() {
throw new UnsupportedOperationException("isUnresolved not implemented yet");
}

@Override
public boolean isUnresolvedOrResolvedUnknown() {
return isUnknown();
}

@Override
public boolean isUnionType() {
throw new UnsupportedOperationException("isUnionType not implemented yet");
Expand Down
4 changes: 4 additions & 0 deletions src/com/google/javascript/rhino/TypeI.java
Expand Up @@ -58,6 +58,10 @@ public interface TypeI {

boolean isNoResolvedType();

// Hacky method to abstract away corner case handling of the way OTI
// represents unresolved types.
boolean isUnresolvedOrResolvedUnknown();

boolean isConstructor();

boolean isEquivalentTo(TypeI type);
Expand Down
6 changes: 5 additions & 1 deletion src/com/google/javascript/rhino/jstype/JSType.java
Expand Up @@ -48,7 +48,6 @@
import com.google.javascript.rhino.ErrorReporter;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.TypeI;

import java.io.Serializable;
import java.util.Comparator;
import java.util.IdentityHashMap;
Expand Down Expand Up @@ -163,6 +162,11 @@ public boolean isNoResolvedType() {
return false;
}

@Override
public final boolean isUnresolvedOrResolvedUnknown() {
return isNoResolvedType() || isNamedType() && isUnknownType();
}

public boolean isNoObjectType() {
return false;
}
Expand Down

0 comments on commit 0514ae2

Please sign in to comment.