fix that type inference assumed every compound-assignment have type number

This commit is contained in:
Erik Krogh Kristensen
2021-01-20 15:26:39 +01:00
parent 2f459d9a72
commit 2e024c3c61
4 changed files with 22 additions and 8 deletions

View File

@@ -130,13 +130,7 @@ class NumericConversion extends ImplicitConversion {
or
parent instanceof ArithmeticExpr and not parent instanceof AddExpr
or
parent instanceof CompoundAssignExpr and
not (
parent instanceof AssignAddExpr or
parent instanceof AssignLogOrExpr or
parent instanceof AssignLogAndExpr or
parent instanceof AssignNullishCoalescingExpr
)
parent.(CompoundAssignExpr).isNumeric()
or
parent instanceof UpdateExpr
}

View File

@@ -1921,6 +1921,18 @@ private class TCompoundAssignExpr =
*/
class CompoundAssignExpr extends TCompoundAssignExpr, Assignment {
override string getAPrimaryQlClass() { result = "CompoundAssignExpr" }
/**
* Holds if this compound assignment always returns a number value.
*/
predicate isNumeric() {
not (
this instanceof AssignAddExpr or
this instanceof AssignLogOrExpr or
this instanceof AssignLogAndExpr or
this instanceof AssignNullishCoalescingExpr
)
}
}
/**

View File

@@ -391,13 +391,15 @@ private class AnalyzedUpdateExpr extends DataFlow::AnalyzedValueNode {
private class AnalyzedCompoundAssignExpr extends DataFlow::AnalyzedValueNode {
override CompoundAssignExpr astNode;
AnalyzedCompoundAssignExpr() { astNode.isNumeric() }
override AbstractValue getALocalValue() { result = abstractValueOfType(TTNumber()) }
}
/**
* Flow analysis for add-assign.
*/
private class AnalyzedAssignAddExpr extends AnalyzedCompoundAssignExpr {
private class AnalyzedAssignAddExpr extends DataFlow::AnalyzedValueNode {
override AssignAddExpr astNode;
override AbstractValue getALocalValue() {

View File

@@ -21,3 +21,9 @@ function g(b) {
// OK: no types inferred for `z`, since this is dead code
z.y = true;
}
function h() {
let tmp;
let obj = (tmp ||= {});
obj.p = 42;
}