Skip to content

Commit b83c949

Browse files
authored
Merge pull request #4986 from erik-krogh/logInf
Approved by esbena
2 parents ee2d18a + a9a901d commit b83c949

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Added support for modern compound assignments (`||=`, `&&=`, and `??=`) in the type inference.

javascript/ql/src/Expressions/ImplicitOperandConversion.ql

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,7 @@ class NumericConversion extends ImplicitConversion {
130130
or
131131
parent instanceof ArithmeticExpr and not parent instanceof AddExpr
132132
or
133-
parent instanceof CompoundAssignExpr and
134-
not (
135-
parent instanceof AssignAddExpr or
136-
parent instanceof AssignLogOrExpr or
137-
parent instanceof AssignLogAndExpr or
138-
parent instanceof AssignNullishCoalescingExpr
139-
)
133+
parent.(CompoundAssignExpr).isNumeric()
140134
or
141135
parent instanceof UpdateExpr
142136
}

javascript/ql/src/semmle/javascript/Expr.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,18 @@ private class TCompoundAssignExpr =
19211921
*/
19221922
class CompoundAssignExpr extends TCompoundAssignExpr, Assignment {
19231923
override string getAPrimaryQlClass() { result = "CompoundAssignExpr" }
1924+
1925+
/**
1926+
* Holds if this compound assignment always returns a number value.
1927+
*/
1928+
predicate isNumeric() {
1929+
not (
1930+
this instanceof AssignAddExpr or
1931+
this instanceof AssignLogOrExpr or
1932+
this instanceof AssignLogAndExpr or
1933+
this instanceof AssignNullishCoalescingExpr
1934+
)
1935+
}
19241936
}
19251937

19261938
/**

javascript/ql/src/semmle/javascript/dataflow/internal/BasicExprTypeInference.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,18 @@ private class AnalyzedUpdateExpr extends DataFlow::AnalyzedValueNode {
388388
/**
389389
* Flow analysis for compound assignments.
390390
*/
391-
private class AnalyzedCompoundAssignExpr extends DataFlow::AnalyzedValueNode {
391+
private class AnalyzedCompoundNumericAssignExpr extends DataFlow::AnalyzedValueNode {
392392
override CompoundAssignExpr astNode;
393393

394+
AnalyzedCompoundNumericAssignExpr() { astNode.isNumeric() }
395+
394396
override AbstractValue getALocalValue() { result = abstractValueOfType(TTNumber()) }
395397
}
396398

397399
/**
398400
* Flow analysis for add-assign.
399401
*/
400-
private class AnalyzedAssignAddExpr extends AnalyzedCompoundAssignExpr {
402+
private class AnalyzedAssignAddExpr extends DataFlow::AnalyzedValueNode {
401403
override AssignAddExpr astNode;
402404

403405
override AbstractValue getALocalValue() {

javascript/ql/test/query-tests/LanguageFeatures/PropertyWriteOnPrimitive/tst.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ function g(b) {
2020
return;
2121
// OK: no types inferred for `z`, since this is dead code
2222
z.y = true;
23+
}
24+
25+
function h() {
26+
let tmp;
27+
let obj = (tmp ||= {});
28+
obj.p = 42;
2329
}

0 commit comments

Comments
 (0)