Skip to content

Commit 7b0ebd3

Browse files
committed
use the context to determine whether or not a node is an operand of a binop
1 parent 737c747 commit 7b0ebd3

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,28 @@ private static class Context {
334334
private final Label parent;
335335
private final int childIndex;
336336
private final IdContext idcontext;
337+
private final boolean binopOperand;
337338

338339
public Context(Label parent, int childIndex, IdContext idcontext) {
340+
this(parent, childIndex, idcontext, false);
341+
}
342+
343+
public Context(Label parent, int childIndex, IdContext idcontext, boolean binopOperand) {
339344
this.parent = parent;
340345
this.childIndex = childIndex;
341346
this.idcontext = idcontext;
347+
this.binopOperand = binopOperand;
342348
}
343349

344350
/** True if the visited AST node occurs as part of a type annotation. */
345351
public boolean isInsideType() {
346352
return idcontext.isInsideType();
347353
}
354+
355+
/** True if the visited AST node occurs as one of the operands of a binary operation. */
356+
public boolean isBinopOperand() {
357+
return binopOperand;
358+
}
348359
}
349360

350361
private class V extends DefaultVisitor<Context, Label> {
@@ -360,16 +371,24 @@ public V(Platform platform, SourceType sourceType) {
360371
}
361372

362373
private Label visit(INode child, Label parent, int childIndex) {
363-
return visit(child, parent, childIndex, IdContext.VAR_BIND);
374+
return visit(child, parent, childIndex, IdContext.VAR_BIND, false);
364375
}
365376

366377
private Label visitAll(List<? extends INode> children, Label parent) {
367378
return visitAll(children, parent, IdContext.VAR_BIND, 0);
368379
}
369380

370381
private Label visit(INode child, Label parent, int childIndex, IdContext idContext) {
382+
return visit(child, parent, childIndex, idContext, false);
383+
}
384+
385+
private Label visit(INode child, Label parent, int childIndex, boolean binopOperand) {
386+
return visit(child, parent, childIndex, IdContext.VAR_BIND, binopOperand);
387+
}
388+
389+
private Label visit(INode child, Label parent, int childIndex, IdContext idContext, boolean binopOperand) {
371390
if (child == null) return null;
372-
return child.accept(this, new Context(parent, childIndex, idContext));
391+
return child.accept(this, new Context(parent, childIndex, idContext, binopOperand));
373392
}
374393

375394
private Label visitAll(
@@ -381,7 +400,7 @@ private Label visitAll(
381400
List<? extends INode> children, Label parent, IdContext idContext, int index, int step) {
382401
Label res = null;
383402
for (INode child : children) {
384-
res = visit(child, parent, index, idContext);
403+
res = visit(child, parent, index, idContext, false);
385404
index += step;
386405
}
387406
return res;
@@ -821,33 +840,33 @@ public Label visit(AssignmentExpression nd, Context c) {
821840
return key;
822841
}
823842

824-
// set to determine which BinaryExpression has been extracted as regexp
825-
private Set<Expression> extractedAsRegexp = new HashSet<>();
826-
827843
@Override
828844
public Label visit(BinaryExpression nd, Context c) {
829845
Label key = super.visit(nd, c);
830-
extractedAsRegexp.add(nd.getLeft());
831-
extractedAsRegexp.add(nd.getRight());
832-
visit(nd.getLeft(), key, 0);
833-
visit(nd.getRight(), key, 1);
834-
if (extractedAsRegexp.contains(nd)) {
835-
return key;
846+
visit(nd.getLeft(), key, 0, true);
847+
visit(nd.getRight(), key, 1, true);
848+
extractRegxpFromBinop(nd, c);
849+
return key;
850+
}
851+
852+
private void extractRegxpFromBinop(BinaryExpression nd, Context c) {
853+
if (c.isBinopOperand()) {
854+
return;
836855
}
837856
Pair<String, OffsetTranslation> concatResult = getStringConcatResult(nd);
838857
if (concatResult == null) {
839-
return key;
858+
return;
840859
}
841860
String rawString = concatResult.fst();
842861
if (rawString.length() > 1000 && !rawString.trim().isEmpty()) {
843-
return key;
862+
return;
844863
}
845864
OffsetTranslation offsets = concatResult.snd();
846865
Position start = nd.getLoc().getStart();
847866
com.semmle.util.locations.Position startPos = new com.semmle.util.locations.Position(start.getLine(), start.getColumn(), start.getOffset());
848867
SourceMap sourceMap = SourceMap.legacyWithStartPos(SourceMap.fromString(nd.getLoc().getSource()).offsetBy(0, offsets), startPos);
849868
regexpExtractor.extract(rawString, sourceMap, nd, true);
850-
return key;
869+
return;
851870
}
852871

853872
@Override

0 commit comments

Comments
 (0)