Skip to content

Commit

Permalink
Default to UNKNOWN when not being able to find JSType/Colors on Nodes
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 597055415
  • Loading branch information
Dustyn Loyda authored and copybara-github committed Jan 9, 2024
1 parent 484224f commit 24b5d78
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/com/google/javascript/jscomp/AstFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1546,12 +1546,16 @@ private static final class TypeOnNode implements Type {

@Override
public JSType getJSType(JSTypeRegistry registry) {
return checkNotNull(this.n.getJSType(), n);
JSType jstype = this.n.getJSType();
// TODO(b/149843534): crash instead of defaulting to unknown
return jstype != null ? jstype : registry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
}

@Override
public Color getColor(ColorRegistry registry) {
return checkNotNull(this.n.getColor(), n);
Color color = this.n.getColor();
// TODO(b/149843534): crash instead of defaulting to unknown
return color != null ? color : StandardColors.UNKNOWN;
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/com/google/javascript/jscomp/AstFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.javascript.jscomp;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.javascript.jscomp.AstFactory.type;
Expand Down Expand Up @@ -377,6 +378,36 @@ public void testCreateNameFromScope_crashesIfMissingVariable() {
assertThrows(Exception.class, () -> astFactory.createName(scope, "missing"));
}

@Test
public void testCreateGetPropFromScope_defaultsToUnknownJSTypeWhenNull() {
AstFactory astFactory = createTestAstFactory();
Node receiver = astFactory.createNameWithUnknownType("JQ");
Node typeTemplate = IR.name("JQ");

checkState(typeTemplate.getJSType() == null, "getJSType does not return null ");

Node x = astFactory.createGetProp(receiver, "$", type(typeTemplate));

assertNode(x).hasType(Token.GETPROP);
assertNode(x).matchesQualifiedName("JQ.$");
assertNode(x).hasJSTypeThat().isUnknown();
}

@Test
public void testCreateGetPropFromScope_defaultsToUnknownColorWhenNull() {
AstFactory astFactory = createTestAstFactoryWithColors();
Node receiver = astFactory.createNameWithUnknownType("JQ");
Node typeTemplate = IR.name("JQ");

checkState(typeTemplate.getColor() == null, "getColor does not return null ");

Node x = astFactory.createGetProp(receiver, "$", type(typeTemplate));

assertNode(x).hasType(Token.GETPROP);
assertNode(x).matchesQualifiedName("JQ.$");
assertNode(x).hasColorThat().isEqualTo(StandardColors.UNKNOWN);
}

@Test
public void testCreateThisReference() {
AstFactory astFactory = createTestAstFactory();
Expand Down

0 comments on commit 24b5d78

Please sign in to comment.