Skip to content

Commit

Permalink
When printing enums in type annotations, use the enum name instead of…
Browse files Browse the repository at this point in the history
… the enumerated type, otherwise we lose type information.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=159149370
  • Loading branch information
dimvar authored and Tyler Breisacher committed Jun 16, 2017
1 parent 942fa60 commit 745e9e8
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/TypedCodeGenerator.java
Expand Up @@ -89,7 +89,7 @@ private String getTypeAnnotation(Node node) {
return getFunctionAnnotation(node); return getFunctionAnnotation(node);
} else if (type.isEnumObject()) { } else if (type.isEnumObject()) {
return "/** @enum {" return "/** @enum {"
+ type.toMaybeObjectType().getElementsType().toNonNullAnnotationString() + type.toMaybeObjectType().getEnumeratedTypeOfEnumObject().toNonNullAnnotationString()
+ "} */\n"; + "} */\n";
} else if (!type.isUnknownType() } else if (!type.isUnknownType()
&& !type.isBottom() && !type.isBottom()
Expand Down
6 changes: 0 additions & 6 deletions src/com/google/javascript/jscomp/newtypes/EnumType.java
Expand Up @@ -235,10 +235,4 @@ static boolean areSubtypes(JSType t1, JSType t2, SubtypeCache subSuperMap) {
public Collection<JSType> getSubtypesWithProperty(QualifiedName qname) { public Collection<JSType> getSubtypesWithProperty(QualifiedName qname) {
return declaredType.getSubtypesWithProperty(qname); return declaredType.getSubtypesWithProperty(qname);
} }

public String toString(ToStringContext ctx) {
return ctx.forAnnotation()
? this.declaredType.appendTo(new StringBuilder(), ctx).toString()
: this.toString();
}
} }
13 changes: 4 additions & 9 deletions src/com/google/javascript/jscomp/newtypes/JSType.java
Expand Up @@ -503,13 +503,8 @@ public final boolean isEnumObject() {
} }


@Override @Override
public final TypeI getElementsType() { public final TypeI getEnumeratedTypeOfEnumObject() {
ObjectType obj = getObjTypeIfSingletonObj(); return isEnumObject() ? getObjTypeIfSingletonObj().getEnumType().getEnumeratedType() : null;
if (obj != null) {
EnumType e = obj.getEnumType();
return e != null ? e.getEnumeratedType() : null;
}
return null;
} }


public final boolean isUnion() { public final boolean isUnion() {
Expand Down Expand Up @@ -1633,11 +1628,11 @@ StringBuilder appendTo(StringBuilder builder, ToStringContext ctx) {
} }
case ENUM_MASK: { case ENUM_MASK: {
if (getEnums().size() == 1) { if (getEnums().size() == 1) {
builder.append(Iterables.getOnlyElement(getEnums()).toString(ctx)); builder.append(Iterables.getOnlyElement(getEnums()).toString());
} else { } else {
Set<String> strReps = new TreeSet<>(); Set<String> strReps = new TreeSet<>();
for (EnumType e : getEnums()) { for (EnumType e : getEnums()) {
strReps.add(e.toString(ctx)); strReps.add(e.toString());
} }
PIPE_JOINER.appendTo(builder, strReps); PIPE_JOINER.appendTo(builder, strReps);
} }
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/rhino/ObjectTypeI.java
Expand Up @@ -157,5 +157,5 @@ public interface ObjectTypeI extends TypeI {
* If this type is an enum object, returns the declared type of the elements. * If this type is an enum object, returns the declared type of the elements.
* Otherwise returns null. * Otherwise returns null.
*/ */
TypeI getElementsType(); TypeI getEnumeratedTypeOfEnumObject();
} }
Expand Up @@ -148,7 +148,7 @@ public int hashCode() {
@Override @Override
StringBuilder appendTo(StringBuilder sb, boolean forAnnotations) { StringBuilder appendTo(StringBuilder sb, boolean forAnnotations) {
if (forAnnotations) { if (forAnnotations) {
return sb.append(this.primitiveType); return sb.append(getReferenceName());
} }
return sb.append(getReferenceName()).append("<").append(this.primitiveType).append(">"); return sb.append(getReferenceName()).append("<").append(this.primitiveType).append(">");
} }
Expand Down
9 changes: 7 additions & 2 deletions src/com/google/javascript/rhino/jstype/EnumType.java
Expand Up @@ -102,12 +102,17 @@ public boolean defineElement(String name, Node definingNode) {
return defineDeclaredProperty(name, elementsType, definingNode); return defineDeclaredProperty(name, elementsType, definingNode);
} }


/** Gets the elements' type. */ /** Gets the elements' type, which is a subtype of the enumerated type. */
@Override
public EnumElementType getElementsType() { public EnumElementType getElementsType() {
return elementsType; return elementsType;
} }


/** Gets the enumerated type. */
@Override
public JSType getEnumeratedTypeOfEnumObject() {
return elementsType.getPrimitiveType();
}

@Override @Override
public TernaryValue testForEquality(JSType that) { public TernaryValue testForEquality(JSType that) {
TernaryValue result = super.testForEquality(that); TernaryValue result = super.testForEquality(that);
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/rhino/jstype/ObjectType.java
Expand Up @@ -827,7 +827,7 @@ public Map<String, JSType> getPropertyTypeMap() {
} }


@Override @Override
public TypeI getElementsType() { public TypeI getEnumeratedTypeOfEnumObject() {
return null; return null;
} }
} }
32 changes: 31 additions & 1 deletion test/com/google/javascript/jscomp/CodePrinterTest.java
Expand Up @@ -1389,7 +1389,8 @@ public void testEnumAnnotation2() {
LINE_JOINER.join( LINE_JOINER.join(
"/** @const */ var goog = goog || {};", "/** @const */ var goog = goog || {};",
"/** @enum {string} */\ngoog.Enum = {FOO:\"x\", BAR:\"y\"};", "/** @enum {string} */\ngoog.Enum = {FOO:\"x\", BAR:\"y\"};",
"/** @type {{BAR: string=, FOO: string=}} */\ngoog.Enum2 = goog.x ? {} : goog.Enum;", "/** @type {{BAR: goog.Enum=, FOO: goog.Enum=}} */",
"goog.Enum2 = goog.x ? {} : goog.Enum;",
"")); ""));
} }


Expand All @@ -1399,6 +1400,35 @@ public void testEnumAnnotation3() {
"/** @enum {!Object} */\nvar Enum = {FOO:{}};\n"); "/** @enum {!Object} */\nvar Enum = {FOO:{}};\n");
} }


public void testEnumAnnotation4() {
assertTypeAnnotations(
LINE_JOINER.join(
"/** @enum {number} */ var E = {A:1, B:2};",
"function f(/** !E */ x) { return x; }"),
LINE_JOINER.join(
"/** @enum {number} */",
"var E = {A:1, B:2};",
"/**",
" * @param {E} x",
" * @return {?}",
" */",
"function f(x) {",
" return x;",
"}",
""),
LINE_JOINER.join(
"/** @enum {number} */",
"var E = {A:1, B:2};",
"/**",
" * @param {E} x",
" * @return {E}",
" */",
"function f(x) {",
" return x;",
"}",
""));
}

public void testClosureLibraryTypeAnnotationExamples() { public void testClosureLibraryTypeAnnotationExamples() {
assertTypeAnnotations( assertTypeAnnotations(
LINE_JOINER.join( LINE_JOINER.join(
Expand Down

0 comments on commit 745e9e8

Please sign in to comment.