Skip to content

Commit 6b27dca

Browse files
authored
Merge pull request #704 from asger-semmle/ts-binary-exprs
Approved by esben-semmle
2 parents 8b06b31 + 9440aab commit 6b27dca

File tree

6 files changed

+388
-10
lines changed

6 files changed

+388
-10
lines changed

change-notes/1.20/extractor-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
## Changes to code extraction
2020

2121
* The extractor now supports [Nullish Coalescing](https://github.com/tc39/proposal-nullish-coalescing) expressions.
22+
* The TypeScript extractor now handles the control-flow of logical operators and destructuring assignments more accurately.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Main {
4141
* such a way that it may produce different tuples for the same file under the same
4242
* {@link ExtractorConfig}.
4343
*/
44-
public static final String EXTRACTOR_VERSION = "2018-11-23";
44+
public static final String EXTRACTOR_VERSION = "2018-12-19";
4545

4646
public static final Pattern NEWLINE = Pattern.compile("\n");
4747

javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.semmle.js.ast.InvokeExpression;
6161
import com.semmle.js.ast.LabeledStatement;
6262
import com.semmle.js.ast.Literal;
63+
import com.semmle.js.ast.LogicalExpression;
6364
import com.semmle.js.ast.MemberDefinition;
6465
import com.semmle.js.ast.MemberExpression;
6566
import com.semmle.js.ast.DeclarationFlags;
@@ -825,8 +826,9 @@ private Node convertBinaryExpression(JsonObject node, SourceLocation loc) throws
825826
Expression left = convertChild(node, "left");
826827
Expression right = convertChild(node, "right");
827828
JsonObject operatorToken = node.get("operatorToken").getAsJsonObject();
828-
String operatorKind = getKind(operatorToken);
829-
if ("CommaToken".equals(operatorKind)) {
829+
String operator = getSourceLocation(operatorToken).getSource();
830+
switch (operator) {
831+
case ",":
830832
List<Expression> expressions = new ArrayList<Expression>();
831833
if (left instanceof SequenceExpression)
832834
expressions.addAll(((SequenceExpression) left).getExpressions());
@@ -837,10 +839,30 @@ private Node convertBinaryExpression(JsonObject node, SourceLocation loc) throws
837839
else
838840
expressions.add(right);
839841
return new SequenceExpression(loc, expressions);
840-
} else {
841-
String operator = getSourceLocation(operatorToken).getSource();
842-
if ("EqualsToken".equals(operatorKind))
843-
left = convertLValue(left);
842+
843+
case "||":
844+
case "&&":
845+
return new LogicalExpression(loc, operator, left, right);
846+
847+
case "=":
848+
left = convertLValue(left); // For plain assignments, the lhs can be a destructuring pattern.
849+
return new AssignmentExpression(loc, operator, left, right);
850+
851+
case "+=":
852+
case "-=":
853+
case "*=":
854+
case "**=":
855+
case "/=":
856+
case "%=":
857+
case "^=":
858+
case "&=":
859+
case "|=":
860+
case ">>=":
861+
case "<<=":
862+
case ">>>=":
863+
return new AssignmentExpression(loc, operator, convertLValue(left), right);
864+
865+
default:
844866
return new BinaryExpression(loc, operator, left, right);
845867
}
846868
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function f(x,y) {
2+
if (x || y) {}
3+
if (x && y) {}
4+
}

javascript/extractor/tests/ts/output/trap/exprs.ts.trap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ hasLocation(#20098,#20096)
326326
exit_cfg_node(#20099,#20004)
327327
hasLocation(#20099,#20094)
328328
successor(#20012,#20017)
329-
successor(#20033,#20037)
330-
successor(#20043,#20035)
329+
successor(#20033,#20043)
331330
successor(#20037,#20039)
332-
successor(#20041,#20043)
331+
successor(#20041,#20035)
333332
successor(#20039,#20041)
333+
successor(#20043,#20037)
334334
successor(#20035,#20099)
335335
successor(#20025,#20027)
336336
successor(#20027,#20029)

0 commit comments

Comments
 (0)