Skip to content

Commit

Permalink
[NTI] Print namespace types more succinctly, and prefix constructor n…
Browse files Browse the repository at this point in the history
…amespaces with $.

E.g., a constructor type Foo is now printed as $Foo instead of Foo<|function(new:$Foo): undefined|>.
Makes pseudo names in property disambiguation much more readable.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=173929708
  • Loading branch information
dimvar authored and brad4d committed Oct 31, 2017
1 parent 81bcf43 commit 446b6d3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/com/google/javascript/jscomp/newtypes/ObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1704,14 +1704,17 @@ StringBuilder appendTo(StringBuilder builder, ToStringContext ctx) {
} else if (isDict()) {
builder.append("dict");
} else if (this.ns != null) {
if (this.fn != null && (this.fn.isUniqueConstructor() || this.fn.isInterfaceDefinition())) {
// Add $ to distinguish a constructor namespace from an instance type with the same name
builder.append("$");
}
builder.append(this.ns);
}
if (this.fn != null) {
} else if (this.fn != null) {
builder.append("<|");
fn.appendTo(builder, ctx);
builder.append("|>");
}
if (ns == null || !props.isEmpty()) {
if (ns == null) {
appendPropsTo(builder, ctx);
}
if (isLoose) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,11 @@ public void testStaticProperty() {
output = ""
+ "/** @constructor */ function Foo(){}"
+ "/** @constructor */ function Bar(){}"
+ "Foo.Foo__function_new_Foo___undefined__$a = 0;"
+ "Bar.Bar__function_new_Bar___undefined__$a = 0;";
+ "Foo.$Foo$a = 0;"
+ "Bar.$Bar$a = 0;";

testSets(js, output,
"{a=[[Bar<|function(new:Bar): undefined|>], [Foo<|function(new:Foo): undefined|>]]}");
"{a=[[$Bar], [$Foo]]}");
}

public void testSupertypeWithSameField() {
Expand Down Expand Up @@ -1552,8 +1552,8 @@ public void testConstructorsWithTypeErrorsAreNotDisambiguated() {
NewTypeInference.MISTYPED_ASSIGN_RHS,
LINE_JOINER.join(
"The right side in the assignment is not a subtype of the left side.",
"Expected : Bar<|function(new:Bar): ?|>",
"Found : Foo<|function(new:Foo): undefined|>",
"Expected : $Bar",
"Found : $Foo",
"More details:",
"Incompatible types for property prototype.",
"Expected : Bar.prototype",
Expand Down
4 changes: 2 additions & 2 deletions test/com/google/javascript/jscomp/TranspileAfterNTITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public void testExponent() {
assertType(nameX.getTypeI()).isNumber();
assertType(nameX.getFirstChild().getTypeI()).isNumber();
assertType(nameX.getFirstFirstChild().getTypeI())
.toStringIsEqualTo("Math.pow<|function(?,?): number|>{prototype: ?}");
.toStringIsEqualTo("Math.pow");
assertType(nameX.getFirstFirstChild().getFirstChild().getTypeI()).toStringIsEqualTo("Math");
assertType(nameX.getFirstChild().getSecondChild().getTypeI()).isNumber();
}
Expand All @@ -436,7 +436,7 @@ public void testAssignExponent() {
assertType(assign.getFirstChild().getTypeI()).isNumber();
assertType(assign.getSecondChild().getTypeI()).isNumber();
assertType(assign.getSecondChild().getFirstChild().getTypeI())
.toStringIsEqualTo("Math.pow<|function(?,?): number|>{prototype: ?}");
.toStringIsEqualTo("Math.pow");
assertType(assign.getSecondChild().getFirstFirstChild().getTypeI()).toStringIsEqualTo("Math");
assertType(assign.getSecondChild().getSecondChild().getTypeI()).isNumber();
}
Expand Down

0 comments on commit 446b6d3

Please sign in to comment.