Skip to content

Commit

Permalink
Fix a ClassCastException in InferJSDocInfo
Browse files Browse the repository at this point in the history
When a class node has an UnknownType rather than a FunctionType (this can happen when the class is immediately inside a CAST node to unknown, though I'm not positive that that's the root cause), trying to cast the type to FunctionType fails.  In general, casting JSType is almost always the wrong thing to do.  Instead, JSType.toMaybeFunctionType is much safer.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=206239547
  • Loading branch information
shicks authored and brad4d committed Jul 27, 2018
1 parent 40f42b0 commit eb3d355
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/InferJSDocInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {

final ObjectType owningType;
if (parent.isClassMembers()) {
FunctionType ctorType = (FunctionType) parent.getParent().getJSType();
FunctionType ctorType = JSType.toMaybeFunctionType(parent.getParent().getJSType());
if (ctorType == null) {
return;
}
Expand Down
14 changes: 14 additions & 0 deletions test/com/google/javascript/jscomp/InferJSDocInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,20 @@ public void testJSDocIsPropagatedToTypeFromObjectLiteralPrototype() {
assertEquals("Property d.", prototype.getOwnPropertyJSDocInfo("d").getBlockDescription());
}

public void testClassWithUnknownTypeDoesNotCrash() {
// Code that looks like this and @suppresses {checkTypes} is generated by tsickle.
ignoreWarnings(TypeCheck.CONFLICTING_EXTENDED_TYPE);
enableTypeCheck();
testSame(
lines(
"/** @param {?} base */",
"function mixin(base) {",
" return (/** @type {?} */ ((class extends base {",
" /** @param {...?} args */ constructor(...args) {}",
" })));",
"}"));
}

/** Returns the inferred type of the reference {@code name} anywhere in the AST. */
private JSType inferredTypeOfName(String name) {
return inferredTypeOfLocalName(name, getLastCompiler().getRoot());
Expand Down

0 comments on commit eb3d355

Please sign in to comment.