Skip to content

Commit 2e024c3

Browse files
committed
fix that type inference assumed every compound-assignment have type number
1 parent 2f459d9 commit 2e024c3

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,15 @@ private class AnalyzedUpdateExpr extends DataFlow::AnalyzedValueNode {
391391
private class AnalyzedCompoundAssignExpr extends DataFlow::AnalyzedValueNode {
392392
override CompoundAssignExpr astNode;
393393

394+
AnalyzedCompoundAssignExpr() { 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)