Skip to content

Commit bdbc8f5

Browse files
committed
add support for OptionalUse in js/missing-await
1 parent 8b084ff commit bdbc8f5

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

javascript/ql/src/Expressions/MissingAwait.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ import javascript
1313
/**
1414
* Holds if `call` is a call to an `async` function.
1515
*/
16-
predicate isAsyncCall(DataFlow::CallNode call) {
16+
predicate isAsyncCall(DataFlow::CallNode call, boolean nullable) {
1717
// If a callee is known, and all known callees are async, assume all
1818
// possible callees are async.
19-
forex(Function callee | call.getACallee() = callee | callee.isAsync())
19+
forex(Function callee | call.getACallee() = callee | callee.isAsync()) and
20+
if call.asExpr() instanceof OptionalUse then nullable = true else nullable = false
2021
}
2122

2223
/**
2324
* Holds if `node` is always a promise.
2425
*/
2526
predicate isPromise(DataFlow::SourceNode node, boolean nullable) {
26-
isAsyncCall(node) and
27-
nullable = false
27+
isAsyncCall(node, nullable)
2828
or
29-
not isAsyncCall(node) and
29+
not isAsyncCall(node, _) and
3030
node.asExpr().getType() instanceof PromiseType and
3131
nullable = true
3232
}

javascript/ql/test/query-tests/Expressions/MissingAwait/MissingAwait.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
| tst.js:22:15:22:19 | thing | Missing await. The value 'thing' is always a promise. |
88
| tst.js:25:13:25:17 | thing | Missing await. The value 'thing' is always a promise. |
99
| tst.js:48:12:48:16 | thing | Missing await. The value 'thing' is always a promise. |
10+
| tst.js:71:16:71:25 | getThing() | Missing await. The call to 'getThing' always returns a promise. |

javascript/ql/test/query-tests/Expressions/MissingAwait/tst.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@ function useThingPossiblySync(b) {
6565
function useThingInVoid() {
6666
void getThing(); // OK
6767
}
68+
69+
function useThing() {
70+
if (random()) {
71+
return getThing() ?? null; // NOT OK
72+
} else {
73+
return getThing?.() ?? null; // OK
74+
}
75+
}

0 commit comments

Comments
 (0)