Skip to content

Commit

Permalink
Change TypeI#getTypeParameters() to return a Collection<TypeI> instea…
Browse files Browse the repository at this point in the history
…d of Collection<String>.

This is important for TypedCodeGenerator to ensure that typevars are formatted as written, rather than with NTI's uniquified names.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=166652159
  • Loading branch information
shicks authored and blickly committed Aug 28, 2017
1 parent 1776c15 commit 2e69b00
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
14 changes: 12 additions & 2 deletions src/com/google/javascript/jscomp/TypedCodeGenerator.java
Expand Up @@ -19,8 +19,10 @@
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.javascript.rhino.FunctionTypeI;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
Expand Down Expand Up @@ -175,17 +177,25 @@ private String getFunctionAnnotation(Node fnNode) {
}
}

Collection<String> typeParams = funType.getTypeParameters();
Collection<TypeI> typeParams = funType.getTypeParameters();
if (!typeParams.isEmpty()) {
sb.append(" * @template ");
Joiner.on(",").appendTo(sb, typeParams);
Joiner.on(",").appendTo(sb, Iterables.transform(typeParams, new Function<TypeI, String>() {
@Override public String apply(TypeI var) {
return formatTypeVar(var);
}
}));
sb.append("\n");
}

sb.append(" */\n");
return sb.toString();
}

private String formatTypeVar(TypeI var) {
return var.toAnnotationString(Nullability.IMPLICIT);
}

// TODO(dimvar): it's awkward that we print @constructor after the extends/implements;
// we should print it first, like users write it. Same for @interface and @record.
private void appendConstructorAnnotations(StringBuilder sb, FunctionTypeI funType) {
Expand Down
19 changes: 16 additions & 3 deletions src/com/google/javascript/jscomp/newtypes/JSType.java
Expand Up @@ -21,12 +21,14 @@
import static com.google.common.base.Preconditions.checkState;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.javascript.jscomp.NodeUtil;
Expand Down Expand Up @@ -1920,13 +1922,24 @@ public final int getMaxArity() {
}

@Override
public final List<String> getTypeParameters() {
public final List<TypeI> getTypeParameters() {
if (isFunctionType()) {
return this.getFunTypeIfSingletonObj().getTypeParameters();
return transformTypeParamsToTypeVars(this.getFunTypeIfSingletonObj().getTypeParameters());
}
NominalType nt = getNominalTypeIfSingletonObj();
checkNotNull(nt, this);
return nt.getTypeParameters();
return transformTypeParamsToTypeVars(nt.getTypeParameters());
}

private List<TypeI> transformTypeParamsToTypeVars(List<String> names) {
return Lists.transform(
names,
new Function<String, TypeI>() {
@Override
public TypeI apply(String name) {
return JSType.fromTypeVar(commonTypes, name);
}
});
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/rhino/TypeI.java
Expand Up @@ -200,9 +200,10 @@ public interface TypeI extends Serializable {
boolean isPartiallyInstantiated();

/**
* If this type is a generic nominal type or function, return the names of all type parameters.
* If this type is a generic nominal type or function, return the type parameters as type
* variables.
*/
Collection<String> getTypeParameters();
Collection<TypeI> getTypeParameters();

/**
* Returns a string representation of this type, suitable for printing
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/rhino/jstype/JSType.java
Expand Up @@ -564,10 +564,10 @@ public TemplateTypeMap getTemplateTypeMap() {
}

@Override
public final ImmutableSet<String> getTypeParameters() {
ImmutableSet.Builder<String> params = ImmutableSet.builder();
public final ImmutableSet<TypeI> getTypeParameters() {
ImmutableSet.Builder<TypeI> params = ImmutableSet.builder();
for (TemplateType type : getTemplateTypeMap().getTemplateKeys()) {
params.add(type.toString());
params.add(type);
}
return params.build();
}
Expand Down

0 comments on commit 2e69b00

Please sign in to comment.