Skip to content

Commit c3ecf70

Browse files
committed
Handle error instead of throwing an exception when TypeExprKinds cannot be resolved.
1 parent 087c555 commit c3ecf70

File tree

4 files changed

+566
-3
lines changed

4 files changed

+566
-3
lines changed

javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,20 +405,31 @@ private Label visitAll(
405405
return res;
406406
}
407407

408+
/**
409+
* Handles cases where an expression or type expression kind cannot be determined.
410+
*/
411+
private void handleUnknownKind(Node nd, String nodeName) {
412+
String message = "Cannot determine kind for " + nodeName + ": " + nd.getClass().getSimpleName();
413+
additionalErrors.add(new ParseError(message, nd.getLoc().getStart()));
414+
}
415+
408416
@Override
409417
public Label visit(Expression nd, Context c) {
410418
int kind =
411419
c.isInsideType()
412420
? TypeExprKinds.getTypeExprKind(nd, c.idcontext)
413421
: ExprKinds.getExprKind(nd, c.idcontext);
422+
if(kind == -1) handleUnknownKind(nd, "expression");
414423
Label lbl = visit(nd, kind, c);
415424
emitStaticType(nd, lbl);
416425
return lbl;
417426
}
418427

419428
@Override
420429
public Label visit(TypeExpression nd, Context c) {
421-
Label lbl = visit(nd, TypeExprKinds.getTypeExprKind(nd, c.idcontext), c);
430+
int kind = TypeExprKinds.getTypeExprKind(nd, c.idcontext);
431+
if(kind == -1) handleUnknownKind(nd, "type expression");
432+
Label lbl = visit(nd, kind, c);
422433
emitStaticType(nd, lbl);
423434
return lbl;
424435
}

javascript/extractor/src/com/semmle/js/extractor/TypeExprKinds.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.semmle.util.exception.CatastrophicError;
3535

3636
public class TypeExprKinds {
37+
private static final int invalidTypeExpr = -1;
3738
private static final int simpleTypeAccess = 0;
3839
private static final int typeDecl = 1;
3940
private static final int keywordTypeExpr = 2;
@@ -273,8 +274,9 @@ public Integer visit(TemplateElement nd, Void c) {
273274
}
274275
},
275276
null);
276-
if (kind == null)
277-
throw new CatastrophicError("Unsupported type expression kind: " + type.getClass());
277+
if (kind == null) {
278+
return invalidTypeExpr;
279+
}
278280
return kind;
279281
}
280282
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Invalid extends (foo.bar), (foo).bar, foo[bar], foo?.bar, foo!.bar, foo(), import.meta {}
2+
interface Employee extends Person {
3+
position: string;
4+
salary: number;
5+
}

0 commit comments

Comments
 (0)