Skip to content

Commit

Permalink
Minor JSTypeRegistry cleanup
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133434527
  • Loading branch information
concavelenz authored and blickly committed Sep 19, 2016
1 parent 0109a47 commit 78dcaf6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/com/google/javascript/rhino/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,16 @@ public boolean hasOneChild() {
return first != null && first.next == null;
}

/**
* Check for zero or one child more efficiently than by iterating over all the
* children as is done with Node.getChildCount().
*
* @return Whether the node has no children or exactly one child.
*/
public boolean hasZeroOrOneChild() {
return first == getLastChild();
}

/**
* Check for more than one child more efficiently than by iterating over all
* the children as is done with Node.getChildCount().
Expand Down
28 changes: 12 additions & 16 deletions src/com/google/javascript/rhino/jstype/JSTypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -1656,30 +1656,28 @@ private JSType createFromTypeNodesInternal(Node n, String sourceName,
// TODO(martinprobst): The new type syntax resolution should be separate.
// Remove the NAME case then.
case NAME:
JSType namedType = getType(scope, n.getString(), sourceName,
n.getLineno(), n.getCharno());
if ((namedType instanceof ObjectType) &&
!(namedType instanceof NamespaceType) &&
!(nonNullableTypeNames.contains(n.getString()))) {
JSType namedType = getType(scope, n.getString(), sourceName, n.getLineno(), n.getCharno());
if ((namedType instanceof ObjectType)
&& !(namedType instanceof NamespaceType)
&& !(nonNullableTypeNames.contains(n.getString()))) {
Node typeList = n.getFirstChild();
int nAllowedTypes = namedType.getTemplateTypeMap().numUnfilledTemplateKeys();
if (!namedType.isUnknownType() && typeList != null) {
// Templatized types.
ImmutableList.Builder<JSType> templateTypes =
ImmutableList.builder();
ImmutableList.Builder<JSType> templateTypes = ImmutableList.builder();

// Special case for Object, where Object.<X> implies Object.<?,X>.
// Special case for Object, where Object<X> implies Object<?,X>.
if ((n.getString().equals("Object") || n.getString().equals("window.Object"))
&& typeList.getFirstChild() == typeList.getLastChild()) {
&& typeList.hasZeroOrOneChild()) {
templateTypes.add(getNativeType(UNKNOWN_TYPE));
}

int nAllowedTypes = namedType.getTemplateTypeMap().numUnfilledTemplateKeys();
int templateNodeIndex = 0;
for (Node templateNode : typeList.getFirstChild().siblings()) {
for (Node templateNode : typeList.children()) {
// Don't parse more templatized type nodes than the type can
// accommodate. This is because some existing clients have
// template annotations on non-templatized classes, for instance:
// goog.structs.Set.<SomeType>
// goog.structs.Set<SomeType>
// The problem in these cases is that the previously-unparsed
// SomeType is not actually a valid type. To prevent these clients
// from seeing unknown type errors, we explicitly don't parse
Expand All @@ -1692,11 +1690,9 @@ private JSType createFromTypeNodesInternal(Node n, String sourceName,
sourceName, templateNode.getLineno(), templateNode.getCharno());
break;
}
templateTypes.add(createFromTypeNodesInternal(
templateNode, sourceName, scope));
templateTypes.add(createFromTypeNodesInternal(templateNode, sourceName, scope));
}
namedType = createTemplatizedType(
(ObjectType) namedType, templateTypes.build());
namedType = createTemplatizedType((ObjectType) namedType, templateTypes.build());
Preconditions.checkNotNull(namedType);
}
return createDefaultObjectUnion(namedType);
Expand Down

0 comments on commit 78dcaf6

Please sign in to comment.