From 0514ae246771e609635a680e9b5d6ca6b9f22f32 Mon Sep 17 00:00:00 2001 From: aravindpg Date: Fri, 5 Aug 2016 11:25:42 -0700 Subject: [PATCH] [NTI] Convert TemplateAstMatcher to use TypeI. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=129462971 --- .../javascript/jscomp/TemplateAstMatcher.java | 26 +++++++++---------- .../jscomp/TypeMatchingStrategy.java | 10 +++---- .../javascript/jscomp/newtypes/JSType.java | 5 ++++ src/com/google/javascript/rhino/TypeI.java | 4 +++ .../javascript/rhino/jstype/JSType.java | 6 ++++- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/com/google/javascript/jscomp/TemplateAstMatcher.java b/src/com/google/javascript/jscomp/TemplateAstMatcher.java index 800f772d682..ad8dd6493e7 100644 --- a/src/com/google/javascript/jscomp/TemplateAstMatcher.java +++ b/src/com/google/javascript/jscomp/TemplateAstMatcher.java @@ -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; @@ -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; @@ -187,7 +186,7 @@ private Node initTemplate(Node templateFunctionNode) { private void prepTemplatePlaceholders(Node fn) { final List locals = templateLocals; final List params = templateParams; - final Map paramTypes = new HashMap<>(); + final Map paramTypes = new HashMap<>(); // drop the function name so it isn't include in the name maps String fnName = fn.getFirstChild().getString(); @@ -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); @@ -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)) { @@ -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; } @@ -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."); @@ -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) { @@ -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; } diff --git a/src/com/google/javascript/jscomp/TypeMatchingStrategy.java b/src/com/google/javascript/jscomp/TypeMatchingStrategy.java index d45b85d72b6..4eda177af77 100644 --- a/src/com/google/javascript/jscomp/TypeMatchingStrategy.java +++ b/src/com/google/javascript/jscomp/TypeMatchingStrategy.java @@ -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 { @@ -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. @@ -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); } } @@ -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; diff --git a/src/com/google/javascript/jscomp/newtypes/JSType.java b/src/com/google/javascript/jscomp/newtypes/JSType.java index b787b8bad9a..b6ec5fc0f07 100644 --- a/src/com/google/javascript/jscomp/newtypes/JSType.java +++ b/src/com/google/javascript/jscomp/newtypes/JSType.java @@ -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"); diff --git a/src/com/google/javascript/rhino/TypeI.java b/src/com/google/javascript/rhino/TypeI.java index 91d79790785..d47caab0ce0 100644 --- a/src/com/google/javascript/rhino/TypeI.java +++ b/src/com/google/javascript/rhino/TypeI.java @@ -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); diff --git a/src/com/google/javascript/rhino/jstype/JSType.java b/src/com/google/javascript/rhino/jstype/JSType.java index 22fb1341ed1..b96d2aec120 100644 --- a/src/com/google/javascript/rhino/jstype/JSType.java +++ b/src/com/google/javascript/rhino/jstype/JSType.java @@ -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; @@ -163,6 +162,11 @@ public boolean isNoResolvedType() { return false; } + @Override + public final boolean isUnresolvedOrResolvedUnknown() { + return isNoResolvedType() || isNamedType() && isUnknownType(); + } + public boolean isNoObjectType() { return false; }