Skip to content

Commit 045dfa6

Browse files
committed
JS: Refactor AbstractDetector so only one method traverses exprs
1 parent 330b342 commit 045dfa6

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

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

+11-25
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,7 @@ protected boolean visitStatements(List<Statement> stmts) {
3838

3939
protected boolean visitStatement(Statement stmt) {
4040
if (stmt instanceof ExpressionStatement) {
41-
Expression e = stripParens(((ExpressionStatement) stmt).getExpression());
42-
43-
// check whether `e` is an iife; if so, recursively check its body
44-
45-
// strip off unary operators to handle `!function(){...}()`
46-
if (e instanceof UnaryExpression) e = ((UnaryExpression) e).getArgument();
47-
48-
if (e instanceof CallExpression && ((CallExpression) e).getArguments().isEmpty()) {
49-
Expression callee = stripParens(((CallExpression) e).getCallee());
50-
if (callee instanceof IFunction) {
51-
Node body = ((IFunction) callee).getBody();
52-
if (body instanceof BlockStatement)
53-
return visitStatements(((BlockStatement) body).getBody());
54-
}
55-
}
56-
57-
if (visitExpression(e)) return true;
58-
41+
return visitExpression(((ExpressionStatement) stmt).getExpression());
5942
} else if (stmt instanceof VariableDeclaration) {
6043
for (VariableDeclarator decl : ((VariableDeclaration) stmt).getDeclarations()) {
6144
Expression init = stripParens(decl.getInit());
@@ -77,17 +60,13 @@ protected boolean visitStatement(Statement stmt) {
7760
return false;
7861
}
7962

80-
private static Expression stripParens(Expression e) {
81-
if (e instanceof ParenthesizedExpression)
82-
return stripParens(((ParenthesizedExpression) e).getExpression());
83-
return e;
84-
}
85-
8663
/**
8764
* Recursively check {@code e} if it's a call or an assignment.
8865
*/
8966
protected boolean visitExpression(Expression e) {
90-
if (e instanceof CallExpression) {
67+
if (e instanceof ParenthesizedExpression) {
68+
return visitExpression(((ParenthesizedExpression) e).getExpression());
69+
} else if (e instanceof CallExpression) {
9170
CallExpression call = (CallExpression) e;
9271
Expression callee = call.getCallee();
9372
// recurse, to handle things like `foo()()`
@@ -102,6 +81,13 @@ protected boolean visitExpression(Expression e) {
10281
if (!"=".equals(assgn.getOperator())) return false;
10382

10483
return visitExpression(assgn.getRight());
84+
} else if (e instanceof UnaryExpression) {
85+
return visitExpression(((UnaryExpression) e).getArgument());
86+
} else if (e instanceof IFunction) {
87+
Node body = ((IFunction) e).getBody();
88+
if (body instanceof BlockStatement) {
89+
return visitStatement((BlockStatement) body);
90+
}
10591
}
10692
return false;
10793
}

0 commit comments

Comments
 (0)