From 2e69b00a58b482f67398d7b5d705d7134937d1df Mon Sep 17 00:00:00 2001 From: sdh Date: Sun, 27 Aug 2017 18:39:14 -0700 Subject: [PATCH] Change TypeI#getTypeParameters() to return a Collection instead of Collection. 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 --- .../javascript/jscomp/TypedCodeGenerator.java | 14 ++++++++++++-- .../javascript/jscomp/newtypes/JSType.java | 19 ++++++++++++++++--- src/com/google/javascript/rhino/TypeI.java | 5 +++-- .../javascript/rhino/jstype/JSType.java | 6 +++--- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/com/google/javascript/jscomp/TypedCodeGenerator.java b/src/com/google/javascript/jscomp/TypedCodeGenerator.java index 4a8c9c5c7fa..e95f0c8b895 100644 --- a/src/com/google/javascript/jscomp/TypedCodeGenerator.java +++ b/src/com/google/javascript/jscomp/TypedCodeGenerator.java @@ -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; @@ -175,10 +177,14 @@ private String getFunctionAnnotation(Node fnNode) { } } - Collection typeParams = funType.getTypeParameters(); + Collection typeParams = funType.getTypeParameters(); if (!typeParams.isEmpty()) { sb.append(" * @template "); - Joiner.on(",").appendTo(sb, typeParams); + Joiner.on(",").appendTo(sb, Iterables.transform(typeParams, new Function() { + @Override public String apply(TypeI var) { + return formatTypeVar(var); + } + })); sb.append("\n"); } @@ -186,6 +192,10 @@ private String getFunctionAnnotation(Node fnNode) { 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) { diff --git a/src/com/google/javascript/jscomp/newtypes/JSType.java b/src/com/google/javascript/jscomp/newtypes/JSType.java index 7da2479e898..372282ee057 100644 --- a/src/com/google/javascript/jscomp/newtypes/JSType.java +++ b/src/com/google/javascript/jscomp/newtypes/JSType.java @@ -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; @@ -1920,13 +1922,24 @@ public final int getMaxArity() { } @Override - public final List getTypeParameters() { + public final List 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 transformTypeParamsToTypeVars(List names) { + return Lists.transform( + names, + new Function() { + @Override + public TypeI apply(String name) { + return JSType.fromTypeVar(commonTypes, name); + } + }); } @Override diff --git a/src/com/google/javascript/rhino/TypeI.java b/src/com/google/javascript/rhino/TypeI.java index 4702155c054..6e83cef8431 100644 --- a/src/com/google/javascript/rhino/TypeI.java +++ b/src/com/google/javascript/rhino/TypeI.java @@ -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 getTypeParameters(); + Collection getTypeParameters(); /** * Returns a string representation of this type, suitable for printing diff --git a/src/com/google/javascript/rhino/jstype/JSType.java b/src/com/google/javascript/rhino/jstype/JSType.java index 5a047c2a29f..a0ea93540c6 100644 --- a/src/com/google/javascript/rhino/jstype/JSType.java +++ b/src/com/google/javascript/rhino/jstype/JSType.java @@ -564,10 +564,10 @@ public TemplateTypeMap getTemplateTypeMap() { } @Override - public final ImmutableSet getTypeParameters() { - ImmutableSet.Builder params = ImmutableSet.builder(); + public final ImmutableSet getTypeParameters() { + ImmutableSet.Builder params = ImmutableSet.builder(); for (TemplateType type : getTemplateTypeMap().getTemplateKeys()) { - params.add(type.toString()); + params.add(type); } return params.build(); }