Skip to content

Commit

Permalink
Don't collapse type unions of non-declared symbols in partial mode.
Browse files Browse the repository at this point in the history
Previously:
/** @type {Foo|Bar} */
const variable;

in partial mode where Foo and Bar weren't defined, resulted in variable having the type (Foo|null).  In partial mode, both Foo and Bar are part of the type and should be kept around.  See angular/clutz#625.

This CL fixes this by changing how Named, NoResolved Types are compared.  Instead of having all NoResolvedTypes be equivalent, Named, NoResolved Types are equivalent if their reference names are the same.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=179953060
  • Loading branch information
LucasSloan authored and blickly committed Jan 2, 2018
1 parent 3d26378 commit 381fa5e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/com/google/javascript/rhino/jstype/JSType.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.google.javascript.rhino.TypeI;
import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.Objects;

/**
* Represents JavaScript value types.<p>
Expand Down Expand Up @@ -711,7 +712,12 @@ boolean checkEquivalenceHelper(final JSType that, EquivalenceMethod eqMethod,
return true;
}
if (this.isNoResolvedType() && that.isNoResolvedType()) {
return true;
if (this.isNamedType() && that.isNamedType()) {
return Objects.equals(
((NamedType) this).getReferenceName(), ((NamedType) that).getReferenceName());
} else {
return true;
}
}

boolean thisUnknown = isUnknownType();
Expand Down
29 changes: 29 additions & 0 deletions test/com/google/javascript/jscomp/PartialCompilationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import com.google.common.collect.ImmutableList;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.NamedType;
import com.google.javascript.rhino.jstype.NoType;
import com.google.javascript.rhino.jstype.ObjectType;
import com.google.javascript.rhino.jstype.UnionType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import junit.framework.TestCase;
Expand Down Expand Up @@ -149,6 +153,31 @@ public void testUnresolvedGenerics() throws Exception {
assertThat(more.getReferenceName()).isEqualTo("More");
}

public void testUnresolvedUnions() throws Exception {
assertPartialCompilationSucceeds("/** @type {some.thing.Foo|some.thing.Bar} */", "var x;");
TypedVar x = compiler.getTopScope().getSlot("x");
assertThat(x.getType().isUnionType()).named("type %s", x.getType()).isTrue();
UnionType unionType = (UnionType) x.getType();

Collection<JSType> alternatives = unionType.getAlternates();
assertThat(alternatives).hasSize(3);

int nullTypeCount = 0;
List<String> namedTypes = new ArrayList<>();
for (JSType alternative : alternatives) {
assertThat(alternative.isNamedType() || alternative.isNullType()).isTrue();
if (alternative.isNamedType()) {
assertThat(alternative.isNoResolvedType()).isTrue();
namedTypes.add(((NamedType) alternative).getReferenceName());
}
if (alternative.isNullType()) {
nullTypeCount++;
}
}
assertThat(nullTypeCount).isEqualTo(1);
assertThat(namedTypes).containsExactly("some.thing.Foo", "some.thing.Bar");
}

public void testUnresolvedGenerics_defined() throws Exception {
assertPartialCompilationSucceeds(
"/** @param {!some.thing.Missing<string>} x */",
Expand Down

0 comments on commit 381fa5e

Please sign in to comment.