Skip to content

Commit 72c4ef4

Browse files
author
Esben Sparre Andreasen
committed
JS: fixup optional chaining on CallWithNonLocalAnalyzedReturnFlow
1 parent 41b4535 commit 72c4ef4

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ abstract class CallWithNonLocalAnalyzedReturnFlow extends DataFlow::AnalyzedValu
157157

158158
override AbstractValue getAValue() {
159159
result = getACallee().getAReturnValue()
160+
or
161+
// special case from the local layer (could be more precise if it is inferred that the callee is not `null`/`undefined`)
162+
astNode instanceof OptionalChainRoot and
163+
result = TAbstractUndefined()
160164
}
161165
}
162166

javascript/ql/test/library-tests/TypeInference/OptionalChaining/test.expected

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
| tst.js:7:14:7:26 | (unknown)?.() | file://:0:0:0:0 | undefined |
1010
| tst.js:9:13:11:5 | unknown ... ;\\n } | file://:0:0:0:0 | undefined |
1111
| tst.js:9:13:11:5 | unknown ... ;\\n } | tst.js:9:33:11:5 | anonymous function |
12-
| tst.js:12:14:12:16 | f() | file://:0:0:0:0 | indefinite value (call) |
13-
| tst.js:13:14:13:18 | f?.() | file://:0:0:0:0 | indefinite value (call) |
12+
| tst.js:12:14:12:16 | f() | file://:0:0:0:0 | non-zero value |
13+
| tst.js:13:14:13:18 | f?.() | file://:0:0:0:0 | non-zero value |
1414
| tst.js:13:14:13:18 | f?.() | file://:0:0:0:0 | undefined |
15+
| tst.js:15:13:17:5 | functio ... ;\\n } | tst.js:15:13:17:5 | function g |
16+
| tst.js:18:14:18:16 | g() | file://:0:0:0:0 | non-zero value |
17+
| tst.js:19:15:19:19 | g?.() | file://:0:0:0:0 | non-zero value |
18+
| tst.js:19:15:19:19 | g?.() | file://:0:0:0:0 | undefined |
19+
| tst.js:21:13:21:21 | undefined | file://:0:0:0:0 | undefined |
20+
| tst.js:22:15:22:17 | h() | file://:0:0:0:0 | indefinite value (call) |
21+
| tst.js:23:15:23:19 | h?.() | file://:0:0:0:0 | indefinite value (call) |
22+
| tst.js:23:15:23:19 | h?.() | file://:0:0:0:0 | undefined |

javascript/ql/test/library-tests/TypeInference/OptionalChaining/tst.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@
88

99
var f = unknown? undefined: function(){
1010
return 42;
11-
}
11+
};
1212
var v7 = f();
1313
var v8 = f?.();
14+
15+
var g = function(){
16+
return 42;
17+
};
18+
var v9 = g();
19+
var v10 = g?.();
20+
21+
var h = undefined;
22+
var v11 = h();
23+
var v12 = h?.();
1424
});
1525
// semmle-extractor-options: --experimental

0 commit comments

Comments
 (0)